using UnityEngine; namespace ECM2.Walkthrough.Ex42 { /// /// This example shows how to extend a Character (through composition) to perform a sprint ability. /// This one use the new simulation OnBeforeSimulationUpdate event (introduced in v1.4), /// to easily modify the character's state within Character's simulation loop. /// public class SprintAbility : MonoBehaviour { [Space(15.0f)] public float maxSprintSpeed = 10.0f; private Character _character; private bool _isSprinting; private bool _sprintInputPressed; private float _cachedMaxWalkSpeed; /// /// Request the character to start to sprint. /// public void Sprint() { _sprintInputPressed = true; } /// /// Request the character to stop sprinting. /// public void StopSprinting() { _sprintInputPressed = false; } /// /// Return true if the character is sprinting, false otherwise. /// public bool IsSprinting() { return _isSprinting; } /// /// Determines if the character, is able to sprint in its current state. /// private bool CanSprint() { return _character.IsWalking() && !_character.IsCrouched(); } /// /// Handles sprint input and adjusts character speed accordingly. /// private void CheckSprintInput() { if (!_isSprinting && _sprintInputPressed && CanSprint()) { _isSprinting = true; _cachedMaxWalkSpeed = _character.maxWalkSpeed; _character.maxWalkSpeed = maxSprintSpeed; } else if (_isSprinting && (!_sprintInputPressed || !CanSprint())) { _isSprinting = false; _character.maxWalkSpeed = _cachedMaxWalkSpeed; } } private void OnBeforeSimulationUpdated(float deltaTime) { // Handle sprinting CheckSprintInput(); } private void Awake() { // Cache character _character = GetComponent(); } private void OnEnable() { // Subscribe to Character BeforeSimulationUpdated event _character.BeforeSimulationUpdated += OnBeforeSimulationUpdated; } private void OnDisable() { // Un-Subscribe from Character BeforeSimulationUpdated event _character.BeforeSimulationUpdated -= OnBeforeSimulationUpdated; } } }