- Home /
Add 90 Degrees to Transform.Rotation
Hello,
Haven't been using Unity for a while and I've kind'a forgotten a few simple things, basically I'm using a 'LookAt' technique by using a transform.rotation = Quaternion.Slerp.
var lookPos = target.position - transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
var adjustRotation = transform.rotation.y + rotationAdjust;
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
The only thing I want to do I to add an extra 90 degrees to the transform rotation because the transform doesn't actually look at 'rotation', its 90 degrees off.
Cheers
Answer by aldonaletto · Nov 19, 2011 at 09:02 PM
You can't modify the components of transform.rotation directly: this property is a quaternion, and its components have nothing to do with the familiar x, y, z angles that appear in the Inspector/Transform/Rotation (these are actually the eulerAngles).
In this case, you should multiply the lookAt quaternion by a 90 degrees rotation - in the quaternion world, multiply actually means combine. Supposing you need this extra rotation around the Y axis, you could do the following:
var lookPos = target.position - transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
rotation *= Quaternion.Euler(0, 90, 0); // this adds a 90 degrees Y rotation
var adjustRotation = transform.rotation.y + rotationAdjust; //<- this is wrong!
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
I don't know how I missed this little gem.. I had just about given up on trying to modify quaternions. Thank you so much aldonaletto - you've opened a huge and important; and as to just now locked door for me. Very grateful.
not working for me.
targetRotation2 *= Quaternion.Euler(0, -90,0);
print(targetRotation2.eulerAngles);
targetRotation2.eulerAngles is always (0,0,0) no change.
Answer by Vollmondum · Dec 15, 2015 at 02:48 AM
Easier solution:
transform.Rotate(Vector3.right, 90, 1);
Not sure if that's correct in that Quaternion world (I really tried to understand, but that's too complicated), but works for me.
"right" actually rotates 90 on X, thus pick which you need (up, back etc.)
Quaterion gives a more accurate value than just using your method and thus making it smoother