protected virtual void OnEnable() { Interest = MovementInterest.CharacterControllerUntilAirborne; MatchCharacterControllerWithSource(true); MatchRigidbodyAndColliderWithCharacterController(); RememberCurrentPositions(); }
/// <summary> /// Positions, sizes and controls all variables necessary to make a body representation follow the given <see cref="BodyRepresentationFacade.source"/>. /// </summary> public virtual void Process() { if (!isActiveAndEnabled) { return; } if (Interest != MovementInterest.CharacterController && facade.offset != null) { Vector3 position = facade.offset.transform.position; position.y = rigidbody.position.y - characterController.skinWidth; Vector3 previousPosition = facade.offset.transform.position; facade.offset.transform.position = position; facade.source.transform.position += facade.offset.transform.position - previousPosition; } Vector3 previousCharacterControllerPosition; // Handle walking down stairs/slopes and physics affecting the Rigidbody in general. Vector3 rigidbodyPhysicsMovement = rigidbody.position - previousRigidbodyPosition; if (Interest == MovementInterest.Rigidbody || Interest == MovementInterest.RigidbodyUntilGrounded) { previousCharacterControllerPosition = characterController.transform.position; characterController.Move(rigidbodyPhysicsMovement); if (facade.offset != null) { Vector3 movement = characterController.transform.position - previousCharacterControllerPosition; facade.offset.transform.position += movement; facade.source.transform.position += movement; } } // Position the CharacterController and handle moving the source relative to the offset. previousCharacterControllerPosition = characterController.transform.position; MatchCharacterControllerWithSource(false); Vector3 characterControllerSourceMovement = characterController.transform.position - previousCharacterControllerPosition; bool isGrounded = CheckIfCharacterControllerIsGrounded(); // Allow moving the Rigidbody via physics. if (Interest == MovementInterest.CharacterControllerUntilAirborne && !isGrounded) { Interest = MovementInterest.RigidbodyUntilGrounded; } else if (Interest == MovementInterest.RigidbodyUntilGrounded && isGrounded && rigidbodyPhysicsMovement.sqrMagnitude <= 1E-06F && rigidbodySetFrameCount > 0 && rigidbodySetFrameCount + 1 < Time.frameCount) { Interest = MovementInterest.CharacterControllerUntilAirborne; } // Handle walking up stairs/slopes via the CharacterController. if (isGrounded && facade.offset != null && characterControllerSourceMovement.y > 0f) { facade.offset.transform.position += Vector3.up * characterControllerSourceMovement.y; } MatchRigidbodyAndColliderWithCharacterController(); RememberCurrentPositions(); EmitIsGroundedChangedEvent(isGrounded); }