2
\$\begingroup\$

I am making a space combat game, in which the player can press q and e to roll their ship. I did this by adding torque to the rigid body when the player press q or e. However, after they do so, they keep on rotating. How do i make the angular velocity slowly go back to zero?

I tried using angular drag, but that does not work.

public float TurnSpeed;

public Rigidbody rb;

float currentturn;

Vector3 turn;

float turnEnabler;

bool iszero;

public float dampener;

// Update is called once per frame
void Update()
{

    rb.AddTorque(new Vector3(0, 0, turnEnabler));

    if (Input.GetKey("e"))
    {
        turnEnabler -= TurnSpeed;
    }

    if (Input.GetKey("q"))
    {
        turnEnabler += TurnSpeed;
    }

    rb.angularDrag = dampener;


}

}

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

As Tom mentioned, after your second if call, you might have a third if statement such as:

else if(!Input.GetKey("e") && !Input.GetKey("q")){turnEnabler /= 2;}

Note that checking for 'q' again is redundant but might be helpful for completeness or readability if desired.

Also, since dividing by 2 will take a long time to really get to zero, you can (if you're bored) throw in something along the lines of:

if(turnEnabler <= 0.01 && turnEnabler >= -0.01){turnEnabler = 0.00}

\$\endgroup\$
1
\$\begingroup\$

From your example, at the end of Update, you can do something like

turnEnabler /= 2;

This would halve the rotating speed every frame, until it eventually reaches 0.

Depending on what kind of behavior you want, you can also try similar changes to the turnEnabler variable, like adding a fixed number every frame.

\$\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.