diff --git a/Runtime/CharacterMovement2D.cs b/Runtime/CharacterMovement2D.cs index 73fc633..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 @@ -192,7 +194,8 @@ private void OnCollisionEnter2D(Collision2D collision) if (Mathf.Abs(collision.relativeVelocity.y) < MinGroundedVelocity) return; if (Vector3.Distance(point, transform.position) < landingCollisionMaxDistance) { - OnGrounded.Invoke(collision.gameObject); + CheckGrounded(); + OnGrounded.Invoke(SurfaceObject, OverTerrain, NormalizedSpeed); } } diff --git a/Runtime/CharacterMovement3D.cs b/Runtime/CharacterMovement3D.cs index e3a2537..89e134e 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() { @@ -147,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(SurfaceObject, OverTerrain, NormalizedSpeed); } // path to destination using navmesh @@ -300,6 +302,15 @@ protected virtual bool CheckGrounded() GroundNormal = Vector3.up; SurfaceVelocity = Vector3.zero; + 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; @@ -409,7 +420,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(SurfaceObject, OverTerrain, NormalizedSpeed); } } @@ -418,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 ced1ebb..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,8 +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 OnGrounded; + public UnityEvent OnFootstep; + public UnityEvent OnJump; // public properties public float MoveSpeedMultiplier { get; set; } = 1f; @@ -57,6 +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 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; @@ -66,13 +70,16 @@ public abstract class CharacterMovementBase : MonoBehaviour // methods public virtual void TryJump() { } - public virtual void Jump() { } + public virtual void Jump() + { + 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(SurfaceObject); + if (animationEvent.animatorClipInfo.weight > 0.5f && IsGrounded && NormalizedSpeed > 0.05f) OnFootstep.Invoke(SurfaceObject, OverTerrain, NormalizedSpeed); } } } \ No newline at end of file