/// <summary> /// Called 60times per second fixed, handles all processing /// </summary> private void FixedUpdate() { if (isFlying()) { // Nullify horizontalSpeed if you loose anchor _animator.SetFloat("horizontalSpeed", 0f); float currentMagnitude = (float)Math.Round(_rigidbody.velocity.sqrMagnitude); // Get a reference to the input of vertical and horizontal force float horizontalForce = Input.GetAxis("Horizontal"); float verticalForce = Input.GetAxis("Vertical"); if (currentMagnitude > maximumVelocity) { // Calculate the difference in velocity from now to cap float brakeSpeed = maximumVelocity - _rigidbody.velocity.sqrMagnitude; // Figure out the amount needed to slow current velocity to excepted speed Vector2 normalisedVelocity = _rigidbody.velocity.normalized; Vector2 brakeVelocity = normalisedVelocity * brakeSpeed; // Apply the ammount needed to break _rigidbody.AddForce(brakeVelocity); } else if (_isBoostAllowed(currentMagnitude, horizontalForce, verticalForce)) { // Start boosting _animator.SetBool("boosting", true); // Get the direction based on keys down Vector2 direction = new Vector2(horizontalForce, verticalForce); // Calculate the new velocity using the fage drag Vector2 newVelocity = direction * boostForce; // Add the force to boost and use energy _rigidbody.AddForce(newVelocity); _energyManager.useEnergy(boostCost); } else if (!_isTakingOff) { // Stop boosting _animator.SetBool("boosting", false); } } }