How to use Time.deltaTime when specifically rotating only 90 degrees

Hi all,

Currently I have an object that after I provide input it rotates 90 degrees in different ways on a global rotation. Unfortunately when I moved it over to iOS I realised that I had not implemented a Time.deltaTime multiplier, so the rotation was slower than it should have been.

Here is the code, its probably terrible.

public class CubeController : MonoBehaviour {

    public GameObject cube = null;

    public float xRot = 0f;
    public float yRot = 0f;
    public float zRot = 0f;

    public bool imRotating = false;

    public int speed = 0;

    //Use this for initialization
    void Start () {

        GameObject.Find("SwipeController").GetComponent<SwipeControl>().SetMethodToCall(MyCallbackMethod);
  
    }
  
    //Update is called once per frame
    void Update() {

        Transform theCube = cube.GetComponent<Transform>();

        if (xRot != 0 || yRot != 0 || zRot != 0)
        {
            imRotating = true;
        }
        else
        {
            imRotating = false;
        }

      

        //xRot
        if (xRot > 0)
        {
            theCube.Rotate(-speed, 0, 0, Space.World);
            xRot -= speed;
        }

        if (xRot < 0)
        {
            theCube.Rotate(speed, 0, 0, Space.World);
            xRot += speed;
        }

        //zRot
        if (zRot > 0)
        {
            theCube.Rotate(0, 0, -speed, Space.World);
            zRot -= speed;
        }

        if (zRot < 0)
        {
            theCube.Rotate(0, 0, speed, Space.World);
            zRot += speed;
        }

        //yRot
        if (yRot > 0)
        {
            theCube.Rotate(0, -speed, 0, Space.World);
            yRot -= speed;
        }

        if (yRot < 0)
        {
            theCube.Rotate(0, speed, 0, Space.World);
            yRot+=speed;
        }

      
  
    }

    public void RotateTopLeft()
    {
        if (imRotating == false)
        {
            zRot = 90f;
        }
        else
        {
            print("I can't rotate, I'm still rotating");
        }
    }

    public void RotateTopRight()
    {
        if (imRotating == false)
        {
            zRot = -90f;
        }
        else
        {
            print("I can't rotate, I'm still rotating");
        }
    }

    public void RotateBottomLeft()
    {
        if (imRotating == false)
        {
            xRot = -90f;
        }
        else
        {
            print("I can't rotate, I'm still rotating");
        }
    }

    public void RotateBottomRight()
    {
        if (imRotating == false)
        {
            xRot = 90f;
        }
        else
        {
            print("I can't rotate, I'm still rotating");
        }
    }

In summary, I need the object to rotate to 90 degrees on world rotation on different axis, but I need the speed of the rotation to be consistent over different frame rates. I’ve tried multiplying the speed by Time.deltaTime but it doesn’t seem to work with my script. Any suggestions?

Cheers.

The documentation for Transform.Rotate uses lots of examples using Time.deltaTime:

Also try looking into Quaternion.Slerp for smooth rotation rather than Transform.Rotate

(You’ll probably want to look into Slerp more than just the documentation, though, search around and you’ll find many topics about it)

Does quaternion support world rotation though?

Well, Slerp just interpolates between two rotations. So you’d just have to make sure the rotation you turn toward is relative, which… seems like a lot of work, but there’s probably an easy way to do it. If it’s working how you want with Transform.Rotate using Time.deltaTime, there’s no reason to change it. I just was very happy to learn Slerp lol.

Yeah, issue is say I used this: transform.Rotate(Vector3.up * Time.deltaTime, Space.World);

Because it’s no longer attached to the xRot/zRot/yRot variable in my code before, I’m unsure of how to get it to stop on exactly 90 degrees past its last position.

Have you tried just multiplying your “speed” variable by Time.deltaTime every time you use it…?

As for how to get it to stop on exact 90s, I’m not sure how you’re accomplishing that now. Is “speed” always set to a factor of 90 so that it will always force xRot/zRot/yRot to end on 90 eventually?

You’ll probably have to add and change a bit of code, to check that they’ve reached at least the target, then set them to exactly the target (since it will almost never land on an exact number).

1 Like