- Home /
Are there more options to rotation 'Space' relativity?
I have an object that I would like to rotate based on the axis of a different object, but at its own transformation point. Ideally, I would use something along the lines of
transform.Rotate(Vector3.forward * rotateAmount, differentObject);
so as to behave as it would if I had used Space.World instead. I don't want the object to move around a different object in space, just use its axis.
The reason for this is that I need the first object to be able to move and rotate freely while the rotation is happening, and so cannot use local axis since the axis could move over time. Nor can I used Space.World since the axis's are simply wrong.
Answer by robertbu · Jul 26, 2014 at 06:05 PM
I've read your question three times, and I'm still not clear on exactly what you need. I'm basing my answer on:
rotate based on the axis of a different object, but at its own transformation point.
The one, long line of code in Update() does that rotation. It uses the 'transform.forward' of the 'differentObject', but you can change it to 'transform.up' or 'transform.right'. To test, create a new scene, create two cubes, add the script to one and drag and drop the other from the Hierarchy on the 'differentObject' variable. Modify the rotation of 'differentObject' either before hitting play or while playing.
var differentObject : Transform;
var rotateSpeed : float = 90.0;
function Update () {
transform.rotation = Quaternion.AngleAxis(rotateSpeed * Time.deltaTime, differentObject.forward) * transform.rotation;
}
I'm not sure this is exactly what I need, but it's a direction worth pursuing from the test and certainly fits the request I gave. Cheers!