- Home /
Face to camera but rotate around one axis
Hello, I have a little problem here. I need to rotate my object only around it's Y axis, to face it to camera. I can't use LookAt() cuz I need to rotate it only by 1 axis. If I setting up something like
transform.parent.parent.Rotate(0f, 1f, 0f, Space.Self);
it works well, rotating object around it's local Y-axis. But when I trying change it to rotation by angle it's starting randomly spin.
Vector3 targetlookvector = new Vector3(Camera.main.transform.position.x, transform.parent.parent.position.y, Camera.main.transform.position.z) - transform.parent.parent.position;
targetlookvector = targetlookvector.normalized;
Vector3 currentlookvector = transform.parent.parent.forward;
currentlookvector.y = 0;
currentlookvector = currentlookvector.normalized;
float angle = Vector3.Angle(currentlookvector, targetlookvector);
if (targetlookvector.x > 0)
transform.parent.parent.Rotate(0f, angle, 0f, Space.Self);
else if (targetlookvector.x < 0)
transform.parent.parent.Rotate(0f, -angle, 0f, Space.Self);
So what can I do to fix this? Or is there any easier ways?
Answer by Minizinger · Nov 28, 2013 at 04:29 PM
Ah sorry. I better learn how to goole. I've found answer here : http://answers.unity3d.com/questions/566152/lookat-locked-at-y-axis-but-in-local-space.html
Your answer
Follow this Question
Related Questions
Rotate Around and Look At Problem 1 Answer
Rotating a GameObject 180 to face another Gameobject on single axis 0 Answers
Indicator rotating around player 1 Answer
An alternative to RotateAround for collisions 0 Answers
LookAt() and RotateAround() 1 Answer