using UnityEngine; [RequireComponent(typeof(PlayerControl))] [RequireComponent(typeof(Rigidbody))] public class PlayerMovement : MonoBehaviour { private static readonly float BoostAccel = 10; public float movementSpeed = 5; public float movementSpeedOnGrabbing = 3; public float boostFalloffLerp = 0.025f; private PlayerControl _playerControl; private Rigidbody _rigidbody; private float _currentBoost; private bool CanBoost => _currentBoost <= 0.1f; private void Awake() { _playerControl = GetComponent(); _rigidbody = GetComponent(); } private void Update() { if (_playerControl.boosting && CanBoost) { _currentBoost = BoostAccel; } } private void FixedUpdate() { FixedUpdateMovement(); FixedUpdateBoostFalloff(); } private void FixedUpdateMovement() { var velocity = _rigidbody.linearVelocity; var speed = _playerControl.grabbing ? movementSpeedOnGrabbing : movementSpeed + _currentBoost; var direction = new Vector3( _playerControl.movementHorizontal, 0, _playerControl.movementVertical) .normalized; var playerVelocity = speed * direction + velocity.y * Vector3.up; _rigidbody.linearVelocity = playerVelocity; } private void FixedUpdateBoostFalloff() { _currentBoost = Mathf.Lerp(_currentBoost, 0, boostFalloffLerp); } }