- Home /
rotation of an object relative to its original position
Hi - just wondered how I might rotate an object by a random degree within a certain range, but always relative to its initial starting position. I have this code, which is working, but seems to rotate relative to the objects current rotation:
var randomAngle:float = Random.Range(-30, 30);
launcherRef.transform.Rotate(Vector3.up * randomAngle);
Answer by aldonaletto · Jul 13, 2011 at 09:32 PM
You can save the initial rotation at Start(), create the random angle and add it to the initial rotation - but be aware that rotations are quaternions, misterious creatures where X-Y-Z-W have nothing to do with the familiar angles we know, so we must use the quaternion right operations:
var initialRot: Quaternion;
function Start( // save the initial rotation
initialRot = launcherRef.transform.rotation;
}
// when you want to rotate the launcherRef do the following:
// create a random rotation around Y axis
var randomRot = Quaternion.Euler(0,Random.Range(-30, 30),0);
// combine the initial rotation with the random one:
launcherRef.transform.rotation = initialRot * randomRot;
...
So strange. That does seem to work. Why is it initialRot * randomRot ins$$anonymous$$d of initialRot + randomRot. I would think if your object had an initialRot of 150 degrees and you generated a randomRot of 30 degrees, that would give you a rotation of of 4500 degrees.
I agree with you: the + operator would be much more intuitive, because we are actually adding rotations (at least that's what it seems to us). But the convention adopted in Unity (maybe everywhere, I'm not sure) is the operator, which for Quaternions means combine, not multiply. Internally, Unity may execute a lot of other operations to effectively combine both rotations, not just a simple multiplication. And there is an additional detail: a b and b * a may give different results in the quaternion world. I've never tested it, but the docs warn us about this.
Turning left then laying on you back gives different results to laying on your back then turning left, so it is with Quaternion multiply order.
Answer by Waz · Jul 13, 2011 at 09:28 PM
That code will rotate around the global Y (up) axis. If you want the local up, use transform.up, not Vector3.up.
Thanks. I think I do want the global Y, actually, but it just needs to be relative to a starting point. So my launcher is set at 150 degrees, and I want it to fire between 120 and 180 degrees on the global access. I tried this, but it still doesn't seem to be quite right:
var randomAngle:float = Random.Range(120, 180);
launcherRef.transform.Rotate(Vector3.up * randomAngle);
plus transform.Rotate is specifically for modifying the current rotation, as opposed to the absolute you deal with when using rotation. Rotate() is a relative change.
Your answer
Follow this Question
Related Questions
How can I make the keys randomly change after each keypress? 1 Answer
A node in a childnode? 1 Answer
Flip over an object (smooth transition) 3 Answers
Rotation script 2 Answers
C# GameObject Reverses Z Rotation 0 Answers