Rotations problem..

I want to rotate a cube smoothly with 45 degress each time, so if I click on the cube it snaps to 45 degrees and then to 90 degrees and so on till 360. What the problem is it goes not exactly to 45 degrees but 43.4 on the rotation and I want that it goes exactly to 45.

Comparing your words to your code, I'm confused by what you want to accomplish. Do you want an immediate rotation to 45 degrees or, or do you want a quick snap (i.e. rotation takes a few frames)? Which axis do you want to rotate around? What do you want to happen when the object reaches 360 degrees...stop and do nothing or reverse direction?

Some thoughts from looking at your code. Transform.Rotate() is relative. It rotates by the specified amount from a starting angle. Assigning Quaternion's to transform.rotation is absolute. You are assigning a specific rotation to the object. Any of the Lerp methods (including LerpAngle() need a parameter that increment/decrements over time in the range of 0 to 1. Time.deltaTime is the time since the last frame and remains fairly constant.

What I want is when you click on the target it rotates slow and smooth to 90 degrees on the x- as of the target and if I click again it goes form 90 degrees to 180 also slow and smooth and so on till 360. And then from 360 the same rythm back. from 0/90/180/270/360 to 360/270/180/90/0

1 Reply

I recommend making the code that rotates the object a component of that object. You are using global code attached to some other objects you want to rotate. With that configuration, if you click on one object and start it rotating and then click on a second object object and start it rotating, the first object will stop rotating before the rotation is complete because you are reassigning lastHit. Attach the code below to any object you want to rotate by 90 degrees.

-5.008956e-06 is .000005008956...essentially 0.0. So the rotation vector is (0,180,180) which orients the cube the same as (180,0,0). This is the kind of thing you will get when working with Quaternions. The code does rotate by 90 degrees. This is not the problem in your code above. A setting of 43.4 is a fair ways away from 45 degrees. If you really need the values to read (180,0,0) there are other ways to rotate the cube, but the code will be more complex including dealing with the 360/0 boundary.

Your answer

Hint: You can notify a user about this post by typing @username

Attachments:
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.