0
\$\begingroup\$

I have a game that the character spins when you press either of the triggers on the xbox remote. Here's the code.

if (gamepadState.IsButtonDown(Buttons.RightTrigger))
        RotationAngle += spinSpeed;

if (gamepadState.IsButtonDown(Buttons.LeftTrigger))
        RotationAngle -= spinSpeed;

what i want to happen is when neither of the triggers are being pressed he rotates back to a rotation angle of 0. I know How to make him go back to 0 when neither triggers are pulled but its too abrupt. How can i make the sprite actually rotate back to an angle of 0?

\$\endgroup\$
2
\$\begingroup\$

Try something like this:

private bool isTurning;

public override void Update(GameTime gameTime)
{
    isTurning = false;
    if (gamepadState.IsButtonDown(Buttons.RightTrigger))
    {
        RotationAngle += spinSpeed;
        isTurning = true;
    }

    if (gamepadState.IsButtonDown(Buttons.LeftTrigger))
    {
        RotationAngle -= spinSpeed;
        isTurning = true;
    }

    if (!isTurning && RotationAngle != 0)
    {
        if (RotationAngle > 0)
        {
            RotationAngle -= spinSpeed;
            if (RotationAngle < 0)
                RotationAngle = 0;
        }
        else if (RotationAngle < 0)
        {
            RotationAngle += spinSpeed;
            if (RotationAngle > 0)
                RotationAngle = 0;
        }
    }
}
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.