I’m kinda desperate on that job. It sometimes costs me hours to tell Unity what I would like to do and I dont get a proper solution. I have such a simple task but dunno how to solve it:
I would like to click on a cube which will then rotate clockwise by exactly 90 degrees. When I click it again it turns 90 degress again (so at pos 180 degrees).
First I tried by simple rotation but it was so fast the rotate animation was not visible. It cant be the solution to integrate a for loop (pause) around this.
By Unity’s documentation I came up to the solution I have to do what I want in “onUpdate()” and working with “Time.deltaTime”. Well that works fine but wont stop at 90 degrees. It runs like forever. I was checking all relevant entries here in the forum and also the official Unity documentation but I just dont get the point.
How do I tell the OnUpdate() that the 90 degrees rotation should be done only when I click on the cube?
Does anyone have an existing Javascript function doing this or could let me know what Unity functions I have to use to solve this problem?
I just can’t imagine that a powerfull 3D engine like Unit can’t do this task.
function RotateLeft()
{
var oldRotation = transform.rotation;
transform.Rotate(0,-90,0);
var newRotation = transform.rotation;
for (t = 0.0; t <= 1.0; t += Time.deltaTime)
{
transform.rotation = Quaternion.Slerp(oldRotation, newRotation, t);
yield;
}
transform.rotation = newRotation; // To make it come out at exactly 90 degrees
}
this script was from this forum I believe posted by EricH. somewhere. try and play with it. should work
var rotating : boolean = false;
var rotateSpeed : int = 45;
function OnMouseDown()
{
if (!rotating) {
rotating = true;
var curRot : float = 0;
var startRot : float = transform.eulerAngles.y;
while (curRot < 90) {
curRot += rotateSpeed * Time.deltaTime;
transform.eulerAngles.y = startRot + curRot;
yield;
}
transform.eulerAngles.y = Mathf.Round(startRot + 90);
rotating = false;
}
}
You’ll need a collider on the cube to make it work, but it should rotate 90 degrees at the rotate speed per second and only allow another rotation if you click on it after it finishes with the first one.
Actually, this solution doesn’t work very well because it suffers from floating point inaccuracies. On the first turn of the cube, it may end up rotating 180.1 degrees and then 180.2 on the second turn, etc.
Using a quaternion lerp, like yellowlabrador suggets, avoids this instability.
(note that a normalized quaternion lerp would be better than a slerp, here’s why: The Inner Product)
Guys, you all rock! I’m overwhelmed about all answers! I tried the one of YellowLabrador and it works great (changed it to clockwise but was easy)!
I also ran several times into inappropriate final rotation angle, like 90.0001
I couldn’t find the proper explanation of “yield” in Unitys documention. Do I see it right that it kinda does a refresh of the screen?
Repeated calls to Rotate(0, speed * Time.deltaTime, 0) will result in horribly inaccurate behavior. Say you’re implementing something like the Rubix Clock puzzle (http://elinkage.com/RubikPuzzles/clock.htm). If you do that using the code you posted, you will find that after a few turns the dials will be off… instead of pointing at 3-o-clock, the dial will point somewhere between 3 and 4. The error will grow progressively larger as you keep turning the dials. That’s because you keep building on previous results and the previous result is never fully accurate, thanks to the nature of floating point values. Try it, if you don’t believe me!
Using a Lerp() like yellowlabrador suggested, you interpolate from a known position at t=0 to another known position at t=1. You’ll still suffer from floating point inaccuracies, of course, but the error will not add up. 6-o-clock will consistently be a rotation of, say, 180.01 degrees.
Of course, you do need to make sure that the last Update() turns the dial to the t=1 position and not any further. The script that Yellowlabrador posted does this with the transform.rotation = newRotation line. A more elegant solution would be to change the t parameter in the Slerp:
Hi I am trying to understand how to do rotations and from the examples above it certainly looks like there are a few ways to approach it.
I have a question about yellowlabrador’s code (2nd post).
function RotateLeft()
{
var oldRotation = transform.rotation;
transform.Rotate(0,-90,0);
var newRotation = transform.rotation;
for (t = 0.0; t <= 1.0; t += Time.deltaTime)
{
transform.rotation = Quaternion.Slerp(oldRotation, newRotation, t);
yield;
}
transform.rotation = newRotation; // To make it come out at exactly 90 degrees
}
At the beginning of the routine transform.Rotate() is called to determine the end rotation (which I am pretty sure would normally rotate the object by itself). Then Quaternion.Slerp is called to rotate the object (again)?. This time via Slerp.
So my confusion is why the apparent double rotation? When I run the code is appears like there is only one rotation happening which is the Slerp.
Also discussed here, where the code is from…I’ve since realized that t in Slerp is always clamped between 0 and 1 anyway, so the last line (transform.rotation = newRotation;) is unnecessary. My coding mistakes will haunt us all forever.
I thought that last line was to help with floating point drift? :?
btw I need to rotate my objects (spin them) more then 360’s degree’s (actually variable between 360 and 720). My approach has been to just rotate them in increments of 5 degrees via transform.Rotate() until the total rotation is decremented down to zero.
Is there perhaps an easier way to do that and is it still recommented to use Slerp vs a raw rotate to keep from drifting? I need to keep my final rotations on 15 degree boundaries with no drift.
The final rotation is always exactly equal to newRotation anyway, even without that last line. t, as I mentioned, is always clamped between 0 and 1 in Slerp. So the last line is just redundant. Although to be totally 100% correct, I believe the “for” line should use < instead of <=.