- Home /
How to use Look-at using only 2 axis to rotate.
Hi, I want to begin by saying that I did search and found for answers about this topic in the past and I also tried multiple times to apply the answers I found.
Sadly I still have issues to constrain the rotation on the z axis :(
We are lucky though as I can show you guys my code and a video of the issue !
(note that the z axis I talk about is pointing down for the character feets , I want to point with my feets but not rotate left and right while doing so) First of here is my code for the look-at:
float oldZ = transform.rotation.z;
playerAnglePreview.LookAt(gravityTarget.position);
oldZ -= playerAnglePreview.rotation.z;
playerAnglePreview.Rotate(0,0,oldZ);
Vector3 desiredRot = playerAnglePreview.rotation.eulerAngles;
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(desiredRot) ,Time.deltaTime * weightPower);
// Note I also tried to make a look at using the position.z of this object but it dint work either..
/* dir = currentGravityText.text;
Vector3 desiredRot = Vector3.zero;
if (dir == "up")
{
desiredRot = new Vector3(-90,0,oldZ);
}
else if (dir == "down")
{
desiredRot = new Vector3(90,0,oldZ);
}
else if (dir == "left")
{
desiredRot = new Vector3(0,-90,oldZ);
}
else if (dir == "right")
{
desiredRot = new Vector3(0,90,oldZ);
}
else if (dir == "front")
{
desiredRot = new Vector3(0,0,oldZ);
}
else if (dir == "back")
{
desiredRot = new Vector3(0,180,oldZ);
}
transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(desiredRot) ,Time.deltaTime * weightPower);*/
/*
down = (90,0,oldZ);
up = (-90,0,oldZ);
back = (0,180,oldZ);
front = (0,0,oldZ);
left = (0,-90,oldZ);
right = (0,90,oldZ);
*/
The part before the first comment is what gives the result you see in the Video , I am pretty much desperate at this point. I really need to be able to switch the directions the foot of the player look at WITHOUT changing the rotation from left to right,
I can change the gravity and walk on walls just fine as you can see but the camera makes the look at on all the 3 axis :(
Your answer
Follow this Question
Related Questions
lookAt on the x axis, without the 180°turn! 3 Answers
Look at object when entering a trigger 1 Answer
Rotate slowly to target position? 3 Answers
Flying player Rotates in the left and right direction not forward and back 1 Answer
Rotating Game Object in Movement Direction on planet with Faux Gravity Unity 3D 2 Answers