private Vector3 GetPosInputVector(LocalSpaceShipInput input)
 {
     if (input.airbrakes)
     {
         return(transform.worldToLocalMatrix.MultiplyVector(-currentVelocity).normalized);
     }
     else
     {
         return(input.inputDirection);
     }
 }
    private void UpdateInput(LocalSpaceShipInput input)
    {
        Vector3 localPosInput = GetPosInputVector(input);

        Vector3 engineInput = GetEngineInput(localPosInput);

        FireEngines(engineInput);

        Vector3 targetMomentum = input.targetAngularMomentum * maxAngularMomentum;

        angularMomentum = UpdateAngularMomentum(transform.localToWorldMatrix.MultiplyVector(targetMomentum));
    }
    private void Update()
    {
        currentVelocity = rigidbody.velocity;
        angularMomentum = rigidbody.angularVelocity * momentOfInertia;

        LocalSpaceShipInput localInput = GetInput();

        UpdateInput(localInput);

        rigidbody.velocity        = currentVelocity;
        rigidbody.angularVelocity = angularMomentum / momentOfInertia;
    }
    private LocalSpaceShipInput GetInput()
    {
        GamepadPoll poll = gamepad.Poll();

        Vector3 positionInput = Vector3.zero;
        Vector3 rotationInput = Vector3.zero;

        positionInput   += poll.LeftStick.ToVector3("x0y");
        positionInput.y += poll.RightTrigger;
        positionInput.y -= poll.LeftTrigger;

        rotationInput += poll.RightStick.ToVector3("-yx0");
        if (poll.GetButton(Gamepad.Button.RightShoulder))
        {
            rotationInput.z -= 1f;
        }
        if (poll.GetButton(Gamepad.Button.LeftShoulder))
        {
            rotationInput.z += 1f;
        }

        if (positionInput.sqrMagnitude > 1f)
        {
            positionInput = positionInput.normalized;
        }
        if (rotationInput.sqrMagnitude > 1f)
        {
            rotationInput = rotationInput.normalized;
        }

        bool airbrakes = poll.GetButton(Gamepad.Button.A);

        LocalSpaceShipInput localInput = new LocalSpaceShipInput(positionInput, rotationInput, airbrakes);

        return(localInput);
    }