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);
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;
...