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!

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;
    }

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!