private void UpdatePosition() { Vector3 movementVec = CurrentMovementState.CalculateMovement(this); movementVec += CurrentJumpState.CalculateMovement(this); PlayerMovementController.Move(movementVec); }
/// <summary> /// (Called by KinematicCharacterMotor during its update cycle) /// This is called after the character has finished its movement update /// </summary> public override void AfterCharacterUpdate(float deltaTime) { CurrentMovementState.AfterCharacterUpdate(deltaTime); // Reset root motion deltas RootMotionPositionDelta = Vector3.zero; RootMotionRotationDelta = Quaternion.identity; }
/// <summary> /// Handles movement state transitions and enter/exit callbacks /// </summary> public void TransitionToState(MyMovementState newState) { newState.AssignedCharacterController = this; newState.KinematicCharacterMotor = this.KinematicCharacterMotor; if (CurrentMovementState != null) { CurrentMovementState.OnStateExit(newState); } MyMovementState prevState = CurrentMovementState; CurrentMovementState = newState; CurrentMovementState.OnStateEnter(prevState); }
private bool IsGrounded() { float offsetCenter = 0.3f; float offsetHeight = 0.5f; float distance = 0.6f; Vector3 tempV = Vector3.zero; Vector3 v = new Vector3(transform.position.x, m_collider.bounds.min.y + offsetHeight, transform.position.z); for (int i = 0; i < 4; i++) { // Define ray switch (i) { case (0): tempV = new Vector3(transform.forward.x * offsetCenter, 0, transform.forward.z * offsetCenter); break; case (1): tempV = -tempV; break; case (2): tempV = new Vector3(transform.right.x * offsetCenter, 0, transform.right.z * offsetCenter); break; case (3): tempV = -tempV; break; default: break; } // Shoot ray if (Physics.Raycast(tempV + v, Vector3.down, out m_rayHit, distance, m_StandableLayers)) { if (!CurrentMovementState.Equals(MovementState.Sliding)) { m_rigidbody.MovePosition(new Vector3(m_rigidbody.position.x, m_rayHit.point.y + m_collider.bounds.size.y / 2, m_rigidbody.position.z)); } return(true); } } return(false); }
// Finds out what state the player is currently in private void DetermineState() { if (!CurrentMovementState.Equals(MovementState.Climbing)) { if (IsGrounded()) { if (!IsSliding()) { if (m_inputManager.MovingCharacter()) { CurrentMovementState = MovementState.Moving; } else { CurrentMovementState = MovementState.Idle; } } else { CurrentMovementState = MovementState.Sliding; } m_controllerClimbing.State = ControllerClimbing.ClimbingState.NotClimbing; m_isGrounded = true; UsingGravity = false; } else { m_isGrounded = false; UsingGravity = true; if (m_rigidbody.velocity.y > 0) { CurrentMovementState = MovementState.Jumping; } else { CurrentMovementState = MovementState.Falling; } } } }
/// <summary> /// (Called by KinematicCharacterMotor during its update cycle) /// This is where you tell your character what its rotation should be right now. /// This is the ONLY place where you should set the character's rotation /// </summary> public override void UpdateRotation(ref Quaternion currentRotation, float deltaTime) { CurrentMovementState.UpdateRotation(ref currentRotation, deltaTime); }
/// <summary> /// (Called by KinematicCharacterMotor during its update cycle) /// This is called before the character begins its movement update /// </summary> public override void BeforeCharacterUpdate(float deltaTime) { CurrentMovementState.BeforeCharacterUpdate(deltaTime); }
public override void OnMovementHit(Collider hitCollider, Vector3 hitNormal, Vector3 hitPoint, bool isStableOnHit) { CurrentMovementState.OnMovementHit(hitCollider, hitNormal, hitPoint, isStableOnHit); }
public override bool MustUpdateGrounding() { return(CurrentMovementState.MustUpdateGrounding()); }
public override bool IsColliderValidForCollisions(Collider coll) { return(CurrentMovementState.IsColliderValidForCollisions(coll)); }
public override bool CanBeStableOnCollider(Collider coll) { return(CurrentMovementState.CanBeStableOnCollider(coll)); }
/// <summary> /// (Called by KinematicCharacterMotor during its update cycle) /// This is where you tell your character what its velocity should be right now. /// This is the ONLY place where you can set the character's velocity /// </summary> public override void UpdateVelocity(ref Vector3 currentVelocity, float deltaTime) { CurrentMovementState.UpdateVelocity(ref currentVelocity, deltaTime); }