From 2d65cb95ee22ba577a53358e12479a0e4c84b2e7 Mon Sep 17 00:00:00 2001 From: dpiltzVFS Date: Mon, 27 Oct 2025 16:47:32 -0700 Subject: [PATCH 1/6] Added a new global RaycastHit variable Need to do this so that I can access the hit info in the CharacterMovement3D class from a derived case I made which handles audio stuff - needed to know if the ray hit a terrain collider. --- Runtime/CharacterMovement3D.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Runtime/CharacterMovement3D.cs b/Runtime/CharacterMovement3D.cs index e3a2537..42e3ee0 100644 --- a/Runtime/CharacterMovement3D.cs +++ b/Runtime/CharacterMovement3D.cs @@ -54,6 +54,7 @@ public class CharacterMovement3D : CharacterMovementBase public Vector3 SplineLookDirection { get; protected set; } public bool HasPath => NavMeshAgent.hasPath; public bool HasCompletePath => NavMeshAgent.hasPath && Vector3.Distance(NavMeshAgent.path.corners[NavMeshAgent.path.corners.Length - 1], NavMeshAgent.destination) < StoppingDistance; + public RaycastHit GroundCheckRaycastHitInfo; protected virtual void OnValidate() { @@ -300,6 +301,8 @@ protected virtual bool CheckGrounded() GroundNormal = Vector3.up; SurfaceVelocity = Vector3.zero; + GroundCheckRaycastHitInfo = hitInfo; + // if ground wasn't hit, character is not grounded if (!hit) return false; From 1bebb180f19c371cabd6250c0dcc8e06d7d96dcd Mon Sep 17 00:00:00 2001 From: dpiltzVFS Date: Wed, 29 Oct 2025 13:59:14 -0700 Subject: [PATCH 2/6] Changed thing so that the OnFootstep unity event now includes a raycast hit generated by the groundcheck method and a float for the speed value --- Runtime/CharacterMovement3D.cs | 2 +- Runtime/CharacterMovementBase.cs | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Runtime/CharacterMovement3D.cs b/Runtime/CharacterMovement3D.cs index 42e3ee0..5da53ae 100644 --- a/Runtime/CharacterMovement3D.cs +++ b/Runtime/CharacterMovement3D.cs @@ -301,7 +301,7 @@ protected virtual bool CheckGrounded() GroundNormal = Vector3.up; SurfaceVelocity = Vector3.zero; - GroundCheckRaycastHitInfo = hitInfo; + GroundHitInfo = hitInfo; // if ground wasn't hit, character is not grounded if (!hit) return false; diff --git a/Runtime/CharacterMovementBase.cs b/Runtime/CharacterMovementBase.cs index ced1ebb..c57363e 100644 --- a/Runtime/CharacterMovementBase.cs +++ b/Runtime/CharacterMovementBase.cs @@ -39,7 +39,7 @@ public abstract class CharacterMovementBase : MonoBehaviour [field: Header("Events")] [field: SerializeField] protected float MinGroundedVelocity { get; set; } = 5f; public UnityEvent OnGrounded; - public UnityEvent OnFootstep; + public UnityEvent OnFootstep; // public properties public float MoveSpeedMultiplier { get; set; } = 1f; @@ -57,6 +57,7 @@ public abstract class CharacterMovementBase : MonoBehaviour public bool HasTurnInput { get; protected set; } public bool IsGrounded { get; protected set; } public GameObject SurfaceObject { get; protected set; } + public RaycastHit GroundHitInfo { get; protected set; } public Vector3 SurfaceVelocity { get; protected set; } public bool CanMove { get; set; } = true; public bool CanTurn { get; set; } = true; @@ -72,7 +73,7 @@ public virtual void SetLookDirection(Vector3 direction) { } public virtual void SetLookPosition(Vector3 position) { } public virtual void FootstepAnimEvent(AnimationEvent animationEvent) { - if (animationEvent.animatorClipInfo.weight > 0.5f && IsGrounded && NormalizedSpeed > 0.05f) OnFootstep.Invoke(SurfaceObject); + if (animationEvent.animatorClipInfo.weight > 0.5f && IsGrounded && NormalizedSpeed > 0.05f) OnFootstep.Invoke(GroundHitInfo, NormalizedSpeed); } } } \ No newline at end of file From 2436bea621c380936fdfecb0e32ebd679e89aa7b Mon Sep 17 00:00:00 2001 From: dpiltzVFS Date: Fri, 5 Dec 2025 10:03:32 -0800 Subject: [PATCH 3/6] Added a "OnJump" unity event --- Runtime/CharacterMovement3D.cs | 1 + Runtime/CharacterMovementBase.cs | 1 + 2 files changed, 2 insertions(+) diff --git a/Runtime/CharacterMovement3D.cs b/Runtime/CharacterMovement3D.cs index 5da53ae..73ec9ba 100644 --- a/Runtime/CharacterMovement3D.cs +++ b/Runtime/CharacterMovement3D.cs @@ -148,6 +148,7 @@ public override void Jump() float jumpVelocity = Mathf.Sqrt(2f * -Gravity * JumpHeight); // override current y velocity but maintain x/z velocity Velocity = new Vector3(Velocity.x, jumpVelocity, Velocity.z); + OnJump.Invoke(); } // path to destination using navmesh diff --git a/Runtime/CharacterMovementBase.cs b/Runtime/CharacterMovementBase.cs index c57363e..b847456 100644 --- a/Runtime/CharacterMovementBase.cs +++ b/Runtime/CharacterMovementBase.cs @@ -40,6 +40,7 @@ public abstract class CharacterMovementBase : MonoBehaviour [field: SerializeField] protected float MinGroundedVelocity { get; set; } = 5f; public UnityEvent OnGrounded; public UnityEvent OnFootstep; + public UnityEvent OnJump; // public properties public float MoveSpeedMultiplier { get; set; } = 1f; From bbbadae8a82688fd7bd34ccde1022d62597c5ac8 Mon Sep 17 00:00:00 2001 From: dpiltzVFS Date: Fri, 5 Dec 2025 14:54:28 -0800 Subject: [PATCH 4/6] Got OnLand and OnGrounded unity event to include the GroundHitInfo and Normalized speed info. Also now calling checkGrounded when the character lands --- Runtime/CharacterMovement2D.cs | 2 +- Runtime/CharacterMovement3D.cs | 5 +++-- Runtime/CharacterMovementBase.cs | 9 ++++++--- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Runtime/CharacterMovement2D.cs b/Runtime/CharacterMovement2D.cs index 73fc633..3571df4 100644 --- a/Runtime/CharacterMovement2D.cs +++ b/Runtime/CharacterMovement2D.cs @@ -192,7 +192,7 @@ private void OnCollisionEnter2D(Collision2D collision) if (Mathf.Abs(collision.relativeVelocity.y) < MinGroundedVelocity) return; if (Vector3.Distance(point, transform.position) < landingCollisionMaxDistance) { - OnGrounded.Invoke(collision.gameObject); + OnGrounded.Invoke(GroundHitInfo, NormalizedSpeed); } } diff --git a/Runtime/CharacterMovement3D.cs b/Runtime/CharacterMovement3D.cs index 73ec9ba..a669004 100644 --- a/Runtime/CharacterMovement3D.cs +++ b/Runtime/CharacterMovement3D.cs @@ -148,7 +148,7 @@ public override void Jump() float jumpVelocity = Mathf.Sqrt(2f * -Gravity * JumpHeight); // override current y velocity but maintain x/z velocity Velocity = new Vector3(Velocity.x, jumpVelocity, Velocity.z); - OnJump.Invoke(); + OnJump.Invoke(GroundHitInfo, NormalizedSpeed); } // path to destination using navmesh @@ -413,7 +413,8 @@ protected virtual void OnCollisionEnter(Collision collision) if(Mathf.Abs(collision.relativeVelocity.y) < MinGroundedVelocity) return; if(Vector3.Distance(point, transform.position) < landingCollisionMaxDistance) { - OnGrounded.Invoke(collision.gameObject); + CheckGrounded(); + OnGrounded.Invoke(GroundHitInfo, NormalizedSpeed); } } diff --git a/Runtime/CharacterMovementBase.cs b/Runtime/CharacterMovementBase.cs index b847456..b99d955 100644 --- a/Runtime/CharacterMovementBase.cs +++ b/Runtime/CharacterMovementBase.cs @@ -38,9 +38,9 @@ public abstract class CharacterMovementBase : MonoBehaviour [field: Header("Events")] [field: SerializeField] protected float MinGroundedVelocity { get; set; } = 5f; - public UnityEvent OnGrounded; + public UnityEvent OnGrounded; public UnityEvent OnFootstep; - public UnityEvent OnJump; + public UnityEvent OnJump; // public properties public float MoveSpeedMultiplier { get; set; } = 1f; @@ -68,7 +68,10 @@ public abstract class CharacterMovementBase : MonoBehaviour // methods public virtual void TryJump() { } - public virtual void Jump() { } + public virtual void Jump() + { + OnJump.Invoke(GroundHitInfo, NormalizedSpeed); + } public virtual void SetMoveInput(Vector3 input) { } public virtual void SetLookDirection(Vector3 direction) { } public virtual void SetLookPosition(Vector3 position) { } From a769a03a874d35fbf24c79b5c3f2a75d3965fbbd Mon Sep 17 00:00:00 2001 From: dpiltzVFS Date: Fri, 5 Dec 2025 14:55:18 -0800 Subject: [PATCH 5/6] need to call check grounded for 2d as well --- Runtime/CharacterMovement2D.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Runtime/CharacterMovement2D.cs b/Runtime/CharacterMovement2D.cs index 3571df4..d8cbc3d 100644 --- a/Runtime/CharacterMovement2D.cs +++ b/Runtime/CharacterMovement2D.cs @@ -192,6 +192,7 @@ private void OnCollisionEnter2D(Collision2D collision) if (Mathf.Abs(collision.relativeVelocity.y) < MinGroundedVelocity) return; if (Vector3.Distance(point, transform.position) < landingCollisionMaxDistance) { + CheckGrounded(); OnGrounded.Invoke(GroundHitInfo, NormalizedSpeed); } } From cb6b17372fb57535ddd332034ddc8b79f2ca4d33 Mon Sep 17 00:00:00 2001 From: dpiltzVFS Date: Sun, 7 Dec 2025 11:08:09 -0800 Subject: [PATCH 6/6] Got things working for 2d footstep support --- Runtime/CharacterMovement2D.cs | 6 ++++-- Runtime/CharacterMovement3D.cs | 14 +++++++++++--- Runtime/CharacterMovementBase.cs | 14 ++++++++------ 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/Runtime/CharacterMovement2D.cs b/Runtime/CharacterMovement2D.cs index d8cbc3d..5f76305 100644 --- a/Runtime/CharacterMovement2D.cs +++ b/Runtime/CharacterMovement2D.cs @@ -91,6 +91,7 @@ public override void Jump() float jumpVelocity = Mathf.Sqrt(2f * -Gravity * JumpHeight); // override current y velocity but maintain x/z velocity Velocity = new Vector3(Velocity.x, jumpVelocity, Velocity.z); + OnJump.Invoke(SurfaceObject, OverTerrain, NormalizedSpeed); } protected virtual void FixedUpdate() @@ -148,12 +149,13 @@ protected virtual bool CheckGrounded() if(hit.collider != null && hit.collider != CapsuleCollider) { groundHit = hit; + SurfaceObject = hit.collider.gameObject; continue; } } // set default ground surface normal and SurfaceVelocity - GroundNormal = Vector3.up; + GroundNormal = Vector3.up; SurfaceVelocity = Vector3.zero; // if ground wasn't hit, character is not grounded @@ -193,7 +195,7 @@ private void OnCollisionEnter2D(Collision2D collision) if (Vector3.Distance(point, transform.position) < landingCollisionMaxDistance) { CheckGrounded(); - OnGrounded.Invoke(GroundHitInfo, NormalizedSpeed); + OnGrounded.Invoke(SurfaceObject, OverTerrain, NormalizedSpeed); } } diff --git a/Runtime/CharacterMovement3D.cs b/Runtime/CharacterMovement3D.cs index a669004..89e134e 100644 --- a/Runtime/CharacterMovement3D.cs +++ b/Runtime/CharacterMovement3D.cs @@ -148,7 +148,7 @@ public override void Jump() float jumpVelocity = Mathf.Sqrt(2f * -Gravity * JumpHeight); // override current y velocity but maintain x/z velocity Velocity = new Vector3(Velocity.x, jumpVelocity, Velocity.z); - OnJump.Invoke(GroundHitInfo, NormalizedSpeed); + OnJump.Invoke(SurfaceObject, OverTerrain, NormalizedSpeed); } // path to destination using navmesh @@ -302,7 +302,14 @@ protected virtual bool CheckGrounded() GroundNormal = Vector3.up; SurfaceVelocity = Vector3.zero; - GroundHitInfo = hitInfo; + if (hit) + { + if (hitInfo.collider.GeometryHolder.Type == UnityEngine.LowLevelPhysics.GeometryType.Terrain) + { + OverTerrain = true; + } + else OverTerrain = false; + } // if ground wasn't hit, character is not grounded if (!hit) return false; @@ -414,7 +421,7 @@ protected virtual void OnCollisionEnter(Collision collision) if(Vector3.Distance(point, transform.position) < landingCollisionMaxDistance) { CheckGrounded(); - OnGrounded.Invoke(GroundHitInfo, NormalizedSpeed); + OnGrounded.Invoke(SurfaceObject, OverTerrain, NormalizedSpeed); } } @@ -423,6 +430,7 @@ protected virtual void OnDrawGizmosSelected() Gizmos.color = IsGrounded ? Color.green : Color.red; Gizmos.DrawRay(GroundCheckStart, -transform.up * GroundCheckDistance); + if(EnableAvoidance) { Gizmos.color = Color.yellow; diff --git a/Runtime/CharacterMovementBase.cs b/Runtime/CharacterMovementBase.cs index b99d955..519b7bf 100644 --- a/Runtime/CharacterMovementBase.cs +++ b/Runtime/CharacterMovementBase.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; +using UnityEngine.LowLevelPhysics; namespace CharacterMovement { @@ -38,9 +39,9 @@ public abstract class CharacterMovementBase : MonoBehaviour [field: Header("Events")] [field: SerializeField] protected float MinGroundedVelocity { get; set; } = 5f; - public UnityEvent OnGrounded; - public UnityEvent OnFootstep; - public UnityEvent OnJump; + public UnityEvent OnGrounded; + public UnityEvent OnFootstep; + public UnityEvent OnJump; // public properties public float MoveSpeedMultiplier { get; set; } = 1f; @@ -58,7 +59,8 @@ public abstract class CharacterMovementBase : MonoBehaviour public bool HasTurnInput { get; protected set; } public bool IsGrounded { get; protected set; } public GameObject SurfaceObject { get; protected set; } - public RaycastHit GroundHitInfo { get; protected set; } + public GeometryType HitColliderType { get; protected set; } + public bool OverTerrain { get; protected set; } public Vector3 SurfaceVelocity { get; protected set; } public bool CanMove { get; set; } = true; public bool CanTurn { get; set; } = true; @@ -70,14 +72,14 @@ public abstract class CharacterMovementBase : MonoBehaviour public virtual void TryJump() { } public virtual void Jump() { - OnJump.Invoke(GroundHitInfo, NormalizedSpeed); + OnJump.Invoke(SurfaceObject, OverTerrain, NormalizedSpeed); } public virtual void SetMoveInput(Vector3 input) { } public virtual void SetLookDirection(Vector3 direction) { } public virtual void SetLookPosition(Vector3 position) { } public virtual void FootstepAnimEvent(AnimationEvent animationEvent) { - if (animationEvent.animatorClipInfo.weight > 0.5f && IsGrounded && NormalizedSpeed > 0.05f) OnFootstep.Invoke(GroundHitInfo, NormalizedSpeed); + if (animationEvent.animatorClipInfo.weight > 0.5f && IsGrounded && NormalizedSpeed > 0.05f) OnFootstep.Invoke(SurfaceObject, OverTerrain, NormalizedSpeed); } } } \ No newline at end of file