How to: object rotated when key down but still continue to rotate tthe direction when key released?

Sorry about the massive title, don;t know how to get the concept out in a shorter way.
Basically I have a “rocket” that when the space key is pressed it pushed the object up but then rotates it to one side, when key is released and pressed again it will do the same but rotate it to the other side.
What i want is to have the object continue to rotate that way continuously when the key isn’t pressed, currently it rotates the object but when i release it the rotation stops.
I’ve tried adding some AddForce methods but I’m horrible at code… This is what i have ATM:

public float boostForce = 15;
    public bool tiltedLeft = true;


    void Update ()
    {
        if (Input.GetButtonUp ("Jump"))
        {
            tiltedLeft = !tiltedLeft;
        }
    }

   
    void FixedUpdate ()
    {
        if (Input.GetButton ("Jump")) {
            Rigidbody rb;
            rb = gameObject.GetComponent<Rigidbody> ();
            rb.AddForce (transform.up * boostForce);
            if (tiltedLeft == true)
            {
                transform.Rotate (0, 0, 40 * Time.deltaTime);
            }
            if (tiltedLeft == false)
            {
                transform.Rotate (0, 0, -40 * Time.deltaTime);
           
            }

        }

    }

I think your fixed update needs to be more like…

    void FixedUpdate ()
    {
        if (Input.GetButton ("Jump")) {
            Rigidbody rb;
            rb = gameObject.GetComponent<Rigidbody> ();
            rb.AddForce (transform.up * boostForce);
        }

            if (tiltedLeft == true)
            {
                transform.Rotate (0, 0, 40 * Time.deltaTime);
            }
            if (tiltedLeft == false)
            {
                transform.Rotate (0, 0, -40 * Time.deltaTime);
         
            }

    }

Then it will rotate when jump is not pressed. You may need to alter update as well, something like…

    void Update ()
    {
        if (Input.GetButtonUp ("Jump"))
        {
            tiltedLeft = false;
        }
        if (Input.GetButtonDown ("Jump"))
        {
            tiltedLeft = true;
        }
    }

So that it switches states. I am not sure that the code here is right as I am at work and don’t have access to unity to test it, but I think it should be something along those lines.

I would also probably move everything out of fixed update into just update and get the rigid body reference in the start() method, just to save a few processor cycles.

1 Like

in order to have code execute without input it needs to be outside the

if(Input.whatever...
{
// not here
}
// here

(was going to say more but Dan covered it :smile: )

The issue i have now is that when the button is released the object rotates the opposite way to what it was rotating at, i wanted it to continue rotation the same direction at that pace and then only rotate the other way when the button is pressed again.

also, cheers for the help, really appreciate it!

Ah, sorry, I thought you meant it should roate one way with the button down and the other way when it was released. In that case if you have changed the update function you might want to put the if statement back to…

        if (Input.GetButtonUp ("Jump"))
        {
            tiltedLeft = !tiltedLeft;
        }

yeah I have that back in there, still getting the issue, when i did the first thing it was continuously rotating the ship the same way when the button was released, now this way it rotates both directions but after release it rotates the wrong way, Its has to be something to do with the bool tiltedLeft because the code is reading when tiltedLeft is true rotate this way and when its false rotate the other way… i just cant get it to wait till the button is pressed again.