- Home /
The question is answered, right answer was accepted
Rotate object in only one axis to face tha camera
Hello smart people!
Imagine a RTS game or something like this (with prespective camera from above with a little inclination). As the title says, i need to rotate an image, which is over the "ground" plane so its always facing the camera, so can only change the Z rotation component. But i dont want that image to rotate in any axis to face it, i want that always to be parallel to ground plane.
Look at the image below: I know how to get the position of the camera over the ground (B pos), its "projection", but im having problems to get how to rotate the image so its always "seen correctly" from the camera. I know is something about cos or sin but i con't get the correct formula.
anybody can help? Thaanks!
Answer by tormentoarmagedoom · Oct 05, 2019 at 11:51 AM
Hello!
Thanks for your answer, but i get a smart solution, IStead of calculate the rotation from the position of the image and the camera.... i just applied the oposite of the camera rotation to the image, so they are always facing eachother!
Bye!
Answer by WarmedxMints · Oct 05, 2019 at 11:45 AM
You can use Quaternion.LookRotation. Simply zero the Y before calculating your rotation.
private Transform _camera;
private Transform _transform;
private void Start()
{
_transform = transform;
_camera = Camera.main.transform;
}
private void Update()
{
var pos = transform.position;
var camPos = _camera.position;
pos.y = 0;
camPos.y = 0;
var targetRot = Quaternion.LookRotation(camPos - pos);
_transform.rotation = targetRot;
}
Follow this Question
Related Questions
Pointing object at mouse using raycast only works when object is center screen. 2 Answers
How can I pitch and roll a circular platform without releasing vertical/horizontal input? 1 Answer
Quaternion Slerp - Rotate to Angle? 1 Answer
2D Shooter - shooting forward at the current rotation 2 Answers
How to make the main camera rotate with the player's directions? 1 Answer