From d8e9a15a36f5285e4c635f0e1044c5bae1cf4f2f Mon Sep 17 00:00:00 2001 From: miepzerino <o.skotnik@gmail.com> Date: Thu, 14 Dec 2023 22:57:28 +0000 Subject: [PATCH] Added more touching directions --- Assets/Characters/Player/Player.prefab | 40 +++++++++++++++++++- Assets/Scripts/PlayerController.cs | 2 + Assets/Scripts/AnimationStrings.cs | 3 + Assets/Scripts/TouchingDirections.cs | 37 ++++++++++++++++++ 4 files changed, 80 insertions(+), 2 deletions(-) diff --git a/Assets/Characters/Player/Player.prefab b/Assets/Characters/Player/Player.prefab index 61b138b..6cad5be 100644 --- a/Assets/Characters/Player/Player.prefab +++ b/Assets/Characters/Player/Player.prefab @@ -122,6 +122,7 @@ - component: {fileID: 3884295854780712968} - component: {fileID: 2451348752795735853} - component: {fileID: 5338311196462064651} + - component: {fileID: 3829651782493779324} m_Layer: 0 m_Name: Player m_TagString: Untagged @@ -378,8 +379,8 @@ adaptiveTiling: 0 m_AutoTiling: 0 serializedVersion: 2 - m_Size: {x: 0.88, y: 0.95} - m_EdgeRadius: 0 + m_Size: {x: 0.7205616, y: 0.7178567} + m_EdgeRadius: 0.12 --- !u!95 &2451348752795735853 Animator: serializedVersion: 5 @@ -430,3 +431,38 @@ groundDistance: 0.05 rotorGO: {fileID: 1519978218308964847} _isGrounded: 1 +--- !u!70 &3829651782493779324 +CapsuleCollider2D: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 3345854317100013954} + m_Enabled: 0 + m_Density: 1 + m_Material: {fileID: 0} + m_IncludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_ExcludeLayers: + serializedVersion: 2 + m_Bits: 0 + m_LayerOverridePriority: 0 + m_ForceSendLayers: + serializedVersion: 2 + m_Bits: 4294967295 + m_ForceReceiveLayers: + serializedVersion: 2 + m_Bits: 4294967295 + m_ContactCaptureLayers: + serializedVersion: 2 + m_Bits: 4294967295 + m_CallbackLayers: + serializedVersion: 2 + m_Bits: 4294967295 + m_IsTrigger: 0 + m_UsedByEffector: 0 + m_UsedByComposite: 0 + m_Offset: {x: 0, y: 0} + m_Size: {x: 0.829981, y: 0.93356335} + m_Direction: 0 diff --git a/Assets/Scripts/AnimationStrings.cs b/Assets/Scripts/AnimationStrings.cs index 0ed6a81..ba13bc5 100644 --- a/Assets/Scripts/AnimationStrings.cs +++ b/Assets/Scripts/AnimationStrings.cs @@ -9,5 +9,8 @@ internal static string IsFlying = "IsFlying"; internal static string IsGrounded = "IsGrounded"; + + public static string IsAtWall = "IsAtWall"; + public static string IsAtCeiling = "IsAtCeiling"; } } \ No newline at end of file diff --git a/Assets/Scripts/PlayerController.cs b/Assets/Scripts/PlayerController.cs index c193afd..b2d1868 100644 --- a/Assets/Scripts/PlayerController.cs +++ b/Assets/Scripts/PlayerController.cs @@ -14,6 +14,7 @@ Animator animator; Animator animator_rotor; public GameObject rotorGO; + TouchingDirections touchingDirections; [SerializeField] private bool _isMoving; @@ -60,6 +61,7 @@ rb = GetComponent<Rigidbody2D>(); animator = GetComponent<Animator>(); animator_rotor = rotorGO.GetComponent<Animator>(); + touchingDirections = GetComponent<TouchingDirections>(); } // Start is called before the first frame update diff --git a/Assets/Scripts/TouchingDirections.cs b/Assets/Scripts/TouchingDirections.cs index cbf8a60..01e3ab7 100644 --- a/Assets/Scripts/TouchingDirections.cs +++ b/Assets/Scripts/TouchingDirections.cs @@ -6,6 +6,10 @@ { public ContactFilter2D castFilter; public float groundDistance = 0.05f; + public float wallDistance = 0.2f; + public float ceilingDistance = 0.05f; + + private Vector2 wallCheckDirection => gameObject.transform.localScale.x > 0 ? Vector2.right : Vector2.left; Rigidbody2D rb; public GameObject rotorGO; @@ -15,6 +19,8 @@ Animator animator_rotor; RaycastHit2D[] groundHits = new RaycastHit2D[5]; + RaycastHit2D[] wallHits = new RaycastHit2D[5]; + RaycastHit2D[] ceilingHits = new RaycastHit2D[5]; [SerializeField] private bool _isGrounded; @@ -29,6 +35,35 @@ animator_rotor.SetBool(AnimationStrings.Player.IsGrounded, value); } } + + [SerializeField] + private bool _isAtWall; + + public bool IsAtWall + { + get { return _isAtWall; } + set + { + _isAtWall = value; + animator.SetBool(AnimationStrings.Player.IsAtWall, value); + animator_rotor.SetBool(AnimationStrings.Player.IsAtWall, value); + } + } + + [SerializeField] + private bool _isAtCeiling; + + public bool IsAtCeiling + { + get { return _isAtCeiling; } + set + { + _isAtCeiling = value; + animator.SetBool(AnimationStrings.Player.IsAtCeiling, value); + animator_rotor.SetBool(AnimationStrings.Player.IsAtCeiling, value); + } + } + void Awake() @@ -47,5 +82,7 @@ void FixedUpdate() { IsGrounded = touchingCol.Cast(Vector2.down, castFilter, groundHits, groundDistance) > 0; + IsAtWall = touchingCol.Cast(wallCheckDirection, castFilter, wallHits, wallDistance) > 0; + IsAtCeiling = touchingCol.Cast(Vector2.up, castFilter, ceilingHits, ceilingDistance) > 0; } } -- Gitblit v1.9.3