how to rotate game object anti-clockwise as (-Time.deltaTime) does not work

I want the object to keep on moving by every frame (using the Time.deltaTime) by clockwise & anti-clockwise rotation.

The player will put the angle they want the object to rotate. Lets say, the angle is 70. After the object is rotate to 70 degree, then it should rotate -70 degree.

public class Rotate : MonoBehaviour {
   
    public enum Direction
    { NONE,RIGHT, LEFT,FORWARD,BACKWARD,DOWN,UP };
   
    public bool Enabled=false;

    public GameObject kiub;
   
    [System.Serializable]
    public class Rotation {
       
        public bool RotateEnabled = false;
        public Direction RotateDirection;
        public float RotateSpeed=10;
        public float Angle;
    }
    public Rotation ObjectRotation;

    [System.Serializable]
    public class Movement {
        public bool MovementEnabled = false;
        public Vector3 MovementAxis;
        public float MovementUnit=0.0001f;
    }
    public Movement ObjectMovement;

    void Update () {
       
        if (Enabled) {
            if (ObjectRotation.RotateEnabled) {
                if (ObjectRotation.RotateDirection == Direction.UP) {
                    if(kiub.transform.rotation.eulerAngles.y <= ObjectRotation.Angle)
                    {
                        transform.Rotate (Vector3.up * Time.deltaTime * ObjectRotation.RotateSpeed);
                    }
                    if(kiub.transform.rotation.eulerAngles.y >= (ObjectRotation.Angle + 0.001))
                    {
                        transform.Rotate (Vector3.up * (-Time.deltaTime) * ObjectRotation.RotateSpeed);
                    }
                }
            }
        }
    }
}

So far, transform.Rotate (Vector3.up * Time.deltaTime * ObjectRotation.RotateSpeed); does work. and it work perfectly the way i want it. BUT, when i want it to rotate the other way around by using transform.Rotate (Vector3.up * (-Time.deltaTime) * ObjectRotation.RotateSpeed);, it only rotate to 359 only. no matter how much the value of the ObjectRotation.Angle is.

Please help me for this. Thank you so much. i really dont have any idea why it happen that way, and how to fix it. Please. Thank you.

By the way, i get the idea of (-Time.deltaTime) from Rotating Objects and Sprites in Unity3D using CS (C#) – John Stejskal : Software and Game Developer

Not sure if it changes anything but instead of doing -Time.deltaTime, try -Vector3.up, which is the actual direction?

1 Like

erm…no. it does not work. it still rotate to 359 only. :frowning:

store the forward vector at the start of the rotation, check the angle between the current rotation and that stored value.

you can’t really do direct comparisons with euler values as you have in the if. 0, 360, -360, 720, -720… all the same angle, how is that going to evaluate with “<=”…

Rotation works a bit funky in Unity which is why you can’t use -Time.deltatime.

In the following tutorial series there is talk about a possible solution for your problem, though in his case it is for a fps camera, but I am sure the same principles apply: https://www.youtube.com/playlist?list=PLbghT7MmckI4su9oKnoLuoRYLUvKlek7j

I believe it is either part 2 or part 3 where he explains it. I’d look it up myself, but am currently at work so I can’t take the time to sift through the videos, sorry.

Couldn’t you just put a * -1 in your equation at instead of trying to do -Time? (Time.deltaTime * -1)

Why would that solve anything?

I went over the tutorials, I think this is the part that will be interesting for you (starts at ~15:40)