For rigidbodies, we are able to lock an axis. I wanted to lock the y and x axis so the character does not look up or down. The following is what I use to rotate.
Is there perhaps some type of 0 quaternion i could multiply the quaternion by to set the rotation to (0, 0, z)?
You can use Quaternion.AngleAxis(whatever, Vector3.up)- that makes sure it only rotates around the y axis. (just to confirm- multiplying a quaternion with the identity quaternion does... absolutely nothing.)
There are a few errors in your understanding of character controllers: 1. rotating the y axis causes the character to rotate side to side, not up and down so you want to lock the x and z axises. 2. You can't rotate a character controller on any axis but the y axis. The mesh might rotate, but the character controller will always be a capsule collider in line with the y axis.
Well, to turn a Vector3 into a float you can always use trig (remember highschool maths? That was fun, wan’t it!)-
float angle = Atan2(point.x, point.z);
However, for what you want to do, you could do something clever like, before you do the LookRotation bit you could fix its Y value so that you never face up or down-
I figured a way to fix it. I changed the point to which i was pointing’s x value to that of the gameobject that way it never points in a certain direction in correspondence to the x or z axis.
targetPosition.x = Player.gameObject.transform.position.x
transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(targetPoint - transform.position, Vector3.up), Time.deltaTime * 2.0);
I figured it out. I changed my targetPosition variable’s (where I am going to be pointing at) y value to that of the gameObject. That way it never looks up or down. The code looks like this.
targetPosition.y = Player.gameObject.transform.position;
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);
You can use Quaternion.AngleAxis(whatever, Vector3.up)- that makes sure it only rotates around the y axis. (just to confirm- multiplying a quaternion with the identity quaternion does... absolutely nothing.)
– syclamothThere are a few errors in your understanding of character controllers: 1. rotating the y axis causes the character to rotate side to side, not up and down so you want to lock the x and z axises. 2. You can't rotate a character controller on any axis but the y axis. The mesh might rotate, but the character controller will always be a capsule collider in line with the y axis.
– Peter_G