/// <summary> Handles the ground check result described by result. </summary> /// <param name="result"> The result. </param> /// <returns> True if it succeeds, false if it fails. </returns> private bool HandleGroundCheckResult(GroundCheckResult result) { isGrounded = result.RaycastHitAnything && result.IsGrounded; if (isGrounded) { groundHit = result.GroundHit; Debug.DrawRay(groundHit.point, groundHit.normal * 10); } return(result.RaycastHitAnything); }
/// <summary> Fixed update. </summary> public virtual void FixedUpdate() { float distance = Vector3.Distance(lastPos, transform.position); Animator.SetMovementDistance(distance); CheckGrounding(); Vector3 moveDelta = IntendedVelocity * Time.fixedDeltaTime; if (moveDelta.sqrMagnitude > 0.001) { if (isGrounded == false) { GroundCheckResult groundCheckResult = CheckGroundSimple(); if (groundCheckResult.RaycastHitAnything && groundCheckResult.IsGrounded) { Rb.MovePosition(groundCheckResult.GroundHit.point); CheckGrounding(); } } if (isGrounded) { moveDelta = Vector3.ProjectOnPlane(moveDelta, groundHit.normal); } Rb.velocity += moveDelta; Debug.DrawRay(Rb.position, moveDelta * 100, Color.red); } else { if (isGrounded == false) { Debug.Log(moveDelta); } Rb.velocity += moveDelta; Debug.DrawRay(Rb.position, moveDelta * 100, Color.green); } if (IntendedVelocity.x0z().IsApproximatelyVectorZero() == false) { lookRotation = Quaternion.LookRotation(IntendedVelocity.x0z().normalized); } Rb.MoveRotation(lookRotation); Animator.SetGrounding(isGrounded); lastPos = transform.position; }