- Home /
Quaternion LookRotation/Slerp Axis Lock
I've been using this code to rotate an object towards another object:
var rotate = Quaternion.LookRotation(target.position - AI.position);
AI.rotation = Quaternion.Slerp(AI.rotation, rotate, Time.deltaTime * rotateDamp);
Unfortunately this rotates the whole object. I need it to only rotate along the y-axis. How should I lock all the other axis' besides the y-axis to rotate the object? (I've tried using the rigidbody axis constraints).
Thanks in advance.
Answer by DayyanSisson · Jul 17, 2012 at 02:15 AM
I ended up just using euler angles to lock the x/z axis to 0 and then use the normal rotation code from there.
I would be interested in seeing the snippit of code you used as I am trying to solve a similar problem but locking to the z axis. Thanks
I have found a solution to my problem from this page http://forum.unity3d.com/threads/36377-transform.LookAt-or-Quaternion.LookRotation-on-1-axis-only Just mentioning it here for other people to looking for a solution to a similar problem.
Here you go:
Vector3 eulerAngles = transform.rotation.eulerAngles;
eulerAngles = new Vector3(0, eulerAngles.y, 0);
transform.rotation = Quaternion.Euler(eulerAngles);
Answer by phil · Jul 10, 2012 at 08:45 PM
Use mathf.lerp http://docs.unity3d.com/Documentation/ScriptReference/Mathf.Lerp.html Var rotate = Quaternion.LookRotation(target.position - AI.position); Then put AI.rotation = Mathf.Lerp(AI.rotation.y ,rotate.y, Time.deltaTime * rotateDamp);
No that wouldn't because $$anonymous$$athf.Lerp returns a float and AI.rotation is a Quaternion. I tried AI.rotation.y = $$anonymous$$athf.Lerp(....); but that gave me a storing a temporary variable error and when I fixed that it started rotating strangely. Its supposed to move between two waypoints (a patrolling movement) but once it reaches one waypoint its supposed to turn to another one. Now it starts turning a little bit, then moves forward, then rotates back to its old rotation, then moves forward a bit more, then rotates back a little bit, etc.... not sure why its doing that.
Answer by phil · Jul 11, 2012 at 12:25 AM
Try this , Quaternion will need to use W too
pragma strict
var AI : Transform;
var target : Transform;
var rotateDamp : float= 10;
function Update () {
var rotate = Quaternion.LookRotation(target.position - AI.position);
var temp = Quaternion.Slerp(AI.rotation, rotate, Time.deltaTime * rotateDamp);
//AI.rotation.x = temp.x;
AI.rotation.y = temp.y;
//AI.rotation.z = temp.z;
AI.rotation.w = temp.w;
//AI.rotation = temp;
}