How to keep an object rotated after using Transform.Rotate() C#

I’m attempting to rotate a 3D object using Transform.Rotate(), and it does rotate, but it returns to it’s original rotation afterwards.

Here’s my code:

if (Input.GetKey(KeyCode.F))
{
    if (inWarp == false)
    {
        transform.Rotate(0, 180, 0);
    }
}
}

It rotates the object, but only until the “F” key is released. Is there a way I can make this rotation permanent?

I’m fairly sure that you at some other point (probably in Update()) manually set the transform (as in transform.rotation = )
transform.Rotate should apply an Rotation to your GameObject which would 1. be permanent and 2. add to the previous rotation. I just tested both. So if everything would be implemented correctly, your GameObject should be rotating constantly and stick to the last rotation you gave it.
One additional Note: If you are not trying to achieve a flip of your GameObject, use Time.deltaTime! (transform.Rotate(0, 180 * Time.deltaTime, 0));

1 Like

This is the script for the object:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Drive_Ship : MonoBehaviour
{
    private bool inWarp = false;
    private bool warp1 = false;
    private bool warp2 = false;
    private bool warp3 = false;
    private bool warp4 = false;
    private bool warp5 = false;
    private bool warp6 = false;
    private bool warp7 = false;
    private bool warp8 = false;
    private bool warp9 = false;
    private bool warp10 = false;
    private bool hasWarped = false;

    private int spee = 10;

    public float normalSpeed = 25f;
    public float accelerationSpeed = 45f;
    public Transform cameraPosition;
    public Camera mainCamera;
    public Transform spaceshipRoot;
    public float rotationSpeed = 2.0f;
    public float cameraSmooth = 4f;
    public RectTransform crosshairTexture;

    float speed;
    //Rigidbody r;
    Quaternion lookRotation;
    float rotationZ = 0;
    float mouseXSmooth = 0;
    float mouseYSmooth = 0;
    Vector3 defaultShipRotation;

    // Start is called before the first frame update
    void Start()
    {
        //r = GetComponent<Rigidbody>();
        //r.useGravity = false;
        lookRotation = transform.rotation;
        defaultShipRotation = spaceshipRoot.localEulerAngles;
        rotationZ = defaultShipRotation.z;

        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    void FixedUpdate()
    {
        //Set moveDirection to the vertical axis (up and down keys) * speed
        Vector3 moveDirection = new Vector3(0, 0, speed);
        //Transform the vector3 to local space
        moveDirection = transform.TransformDirection(moveDirection);
        //Set the velocity, so you can move

        /*Camera follow
        mainCamera.transform.position = Vector3.Lerp(mainCamera.transform.position, cameraPosition.position, Time.deltaTime * cameraSmooth);
        mainCamera.transform.rotation = Quaternion.Lerp(mainCamera.transform.rotation, cameraPosition.rotation, Time.deltaTime * cameraSmooth);
        */
        //Rotation
        float rotationZTmp = 0;
        if (Input.GetKey(KeyCode.A))
        {
            if (inWarp == false)
            {
                rotationZTmp = 1;
            }
        }
        else if (Input.GetKey(KeyCode.D))
        {
            if (inWarp == false)
            {
                rotationZTmp = -1;
            }
        }

        mouseXSmooth = Mathf.Lerp(mouseXSmooth, Input.GetAxis("Horizontal") * rotationSpeed, Time.deltaTime * cameraSmooth);
        mouseYSmooth = Mathf.Lerp(mouseYSmooth, Input.GetAxis("Vertical") * rotationSpeed, Time.deltaTime * cameraSmooth);
        Quaternion localRotation = Quaternion.Euler(-mouseYSmooth, mouseXSmooth, rotationZTmp * rotationSpeed);
        lookRotation = lookRotation * localRotation;

        transform.rotation = lookRotation;

        rotationZ -= mouseXSmooth;
        rotationZ = Mathf.Clamp(rotationZ, -40, 40);
        spaceshipRoot.transform.localEulerAngles = new Vector3(defaultShipRotation.x, defaultShipRotation.y, rotationZ);
        rotationZ = Mathf.Lerp(rotationZ, defaultShipRotation.z, Time.deltaTime * cameraSmooth);

        //Update crosshair texture
        if (crosshairTexture)
        {
            crosshairTexture.position = mainCamera.WorldToScreenPoint(transform.position + transform.forward * 100);
        }

        if (Input.GetKey(KeyCode.Alpha1))
        {
            hasWarped = true;
            inWarp = true;
            warp1 = true;

            spee = 5000;
            Debug.Log("Warp 1: Initiate");
        }
        else if (Input.GetKey(KeyCode.Alpha2))
        {
            hasWarped = true;
            inWarp = true;
            warp1 = false;
            warp2 = true;

            spee = 10000;
            Debug.Log("Warp 2: Initiate");
        }
        else if (Input.GetKey(KeyCode.Alpha3))
        {
            hasWarped = true;
            inWarp = true;
            warp1 = false;
            warp2 = false;
            warp3 = true;

            spee = 15000;
            Debug.Log("Warp 3: Initiate");
        }
        else if (Input.GetKey(KeyCode.Alpha4))
        {
            hasWarped = true;
            inWarp = true;
            warp1 = false;
            warp2 = false;
            warp3 = false;
            warp4 = true;

            spee = 23000;
            Debug.Log("Warp 4: Initiate");
        }
        else if (Input.GetKey(KeyCode.Alpha5))
        {
            hasWarped = true;
            inWarp = true;
            warp1 = false;
            warp2 = false;
            warp3 = false;
            warp4 = false;
            warp5 = true;

            spee = 38000;
            Debug.Log("Warp 5: Initiate");
        }
        else if (Input.GetKey(KeyCode.Alpha6))
        {
            hasWarped = true;
            inWarp = true;
            warp1 = false;
            warp2 = false;
            warp3 = false;
            warp4 = false;
            warp5 = false;
            warp6 = true;

            spee = 52000;
            Debug.Log("Warp 6: Initiate");
        }
        else if (Input.GetKey(KeyCode.Alpha7))
        {
            hasWarped = true;
            inWarp = true;
            warp1 = false;
            warp2 = false;
            warp3 = false;
            warp4 = false;
            warp5 = false;
            warp6 = false;
            warp7 = true;

            spee = 69000;
            Debug.Log("Warp 7: Initiate");
        }
        else if (Input.GetKey(KeyCode.Alpha8))
        {
            hasWarped = true;
            inWarp = true;
            warp1 = false;
            warp2 = false;
            warp3 = false;
            warp4 = false;
            warp5 = false;
            warp6 = false;
            warp7 = false;
            warp8 = true;

            spee = 82000;
            Debug.Log("Warp 8: Initiate");
        }
        else if (Input.GetKey(KeyCode.Alpha9))
        {
            hasWarped = true;
            inWarp = true;
            warp1 = false;
            warp2 = false;
            warp3 = false;
            warp4 = false;
            warp5 = false;
            warp6 = false;
            warp7 = false;
            warp8 = false;
            warp9 = true;

            spee = 123000;
            Debug.Log("Warp 9: Initiate");
        }
        else if (Input.GetKey(KeyCode.Alpha0))
        {
            hasWarped = true;
            inWarp = true;
            warp1 = false;
            warp2 = false;
            warp3 = false;
            warp4 = false;
            warp5 = false;
            warp6 = false;
            warp7 = false;
            warp8 = false;
            warp9 = false;
            warp10 = true;

            spee = 183000;
            Debug.Log("Warp 10: Initiate");
        }
        else if (Input.GetKey(KeyCode.Alpha1) == false && Input.GetKey(KeyCode.Alpha2) == false && Input.GetKey(KeyCode.Alpha3) == false && Input.GetKey(KeyCode.Alpha4) == false && Input.GetKey(KeyCode.Alpha5) == false && Input.GetKey(KeyCode.Alpha6) == false && Input.GetKey(KeyCode.Alpha7) == false && Input.GetKey(KeyCode.Alpha8) == false && Input.GetKey(KeyCode.Alpha9) == false && Input.GetKey(KeyCode.Alpha0) == false && hasWarped == true)
        {
            inWarp = false;
            warp1 = false;
            warp2 = false;
            warp3 = false;
            warp4 = false;
            warp5 = false;
            warp6 = false;
            warp7 = false;
            warp8 = false;
            warp9 = false;
            warp10 = false;
            spee = 100;
            Debug.Log("Exiting Warp");
        }

        if (Input.GetKey(KeyCode.F))
        {
            if (inWarp == false)
            {
                transform.rotation = Quaternion.Euler(0, 180, 0);
            }
        }

        if (Input.GetKey(KeyCode.A))
        {
            if (inWarp == false)
            {
                transform.position += -transform.right * spee;// * Time.deltaTime;
            }
        }
        else if (Input.GetKey(KeyCode.D))
        {
            if (inWarp == false)
            {
                transform.position += transform.right * spee;// * Time.deltaTime;
            }
        }

        if (Input.GetKey(KeyCode.W))
        {
            transform.position += transform.forward * spee * Time.deltaTime;
        }
        else if (Input.GetKey(KeyCode.S))
        {
            if (inWarp == false)
            {
                transform.position += -transform.forward * spee * Time.deltaTime;
            }
        }
    }
}

The code is very messy, sorry!

I’m not sure what the problem is, but maybe it has something to do with the movement code above crosshairTexture?

In Line 86 you set the rotation to something based on input, and then in line 255 you set it to something else if the button is pressed. As soon as you stop pushing the button, only line 86 will trigger, that’s why it “jumps back”.
You should consider multiplying rotations instead of setting them. So something like transform.rotation *= Quaternion.Euler(0f, 180f * Time.deltaTime, 0f);

1 Like

Multiplying them worked :slight_smile: Thank you!