Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions Runtime/CharacterMovement2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

Expand Down
15 changes: 14 additions & 1 deletion Runtime/CharacterMovement3D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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;
Expand Down
15 changes: 11 additions & 4 deletions Runtime/CharacterMovementBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.LowLevelPhysics;

namespace CharacterMovement
{
Expand Down Expand Up @@ -38,8 +39,9 @@ public abstract class CharacterMovementBase : MonoBehaviour

[field: Header("Events")]
[field: SerializeField] protected float MinGroundedVelocity { get; set; } = 5f;
public UnityEvent<GameObject> OnGrounded;
public UnityEvent<GameObject> OnFootstep;
public UnityEvent<GameObject, bool, float> OnGrounded;
public UnityEvent<GameObject, bool, float> OnFootstep;
public UnityEvent<GameObject, bool, float> OnJump;

// public properties
public float MoveSpeedMultiplier { get; set; } = 1f;
Expand All @@ -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;
Expand All @@ -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);
}
}
}