- Home /
How to lock a quaternion axis?
Hello, I have this block of code that tracks my player
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.position - transform.position), turnSpeed * Time.deltaTime);
if(Vector3.Distance(transform.position, target.position) < 50)
{
transform.position += transform.up * 0.2f * Time.deltaTime;
transform.position += transform.forward * moveSpeed * Time.deltaTime;
transform.position = new Vector3(transform.position.x, transform.position.y, 0);
}
but I have a 2d sprite that its attached to - I need to lock the (z?) axis on the rotation, but when I try to edit the quaternion, the character stops tracking the player
any ideas?
bump, still haven't found a solution to this... surprised at how fast these forums fill up! Anyway, thanks for any advice
Quaternions are represented by an axis vector and angle about that axis, so what do you mean by locking the Z axis, and how are you trying to go about that? Some context will help, why do you need to lock the Z axis, not really enough to go on here. If I had to guess, you may want to do something like this:
Vector3 euler = transform.rotation.eulerAngles;
euler.z = 0;
transform.rotation = Quaternion.Euler(euler);
Of course, that's the long way. You could also just set transform.eulerAngles directly ins$$anonymous$$d.
Thanks - I have a section of my game that is a side scroller (3d environment and characters), and there are enemies that are made from quads (sprite sheet animations). The problem is that when I Instantiate the enemies, since they are quads, they rotate along an axis that makes them perpendicular to the camera, like looking at a piece of paper from the side - I need them to not rotate along an axis so they are always facing flat to the camera. I'll try what you've posted!
Answer by robertbu · May 31, 2014 at 02:33 AM
The issue here is not how to lock an axis. The issue is that Quaternion.LookRotation() is the wrong way to rotate a sprite in a 2D setting. LookRotation() faces the positive 'z' side of an object in a specified direction. For Sprites and Quads, you typically want to face either the right side, or the top side of the object towards another object. Atan2() is on solution. For the right side being forward, you could do:
var dir = target.position - transform.position;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.AngleAxis(angle, Vector3.forward), turnSpeed * Time.deltaTime);
If currently the top is considered forward, you can either rotate the sprite in Photoshop, or you can add 90 degrees to the angle before passing it to AngleAxis().
Your answer
Follow this Question
Related Questions
Quaternion LookRotation/Slerp Axis Lock 3 Answers
Gimbal lock - object moving on surface of sphere 0 Answers
Rotating an object around Z axis using Lerp 2 Answers
Creating a multiple part turret what locks onto certain axis. 4 Answers
How to fix gimbal lock? 0 Answers