Locked rotation axis ?

Hi
my object rotate to face player. but when player moving up or down this object rotate to the wrong side.
X & Z should be locked.

    public Transform player;
    public Transform gun;

    // Update is called once per frame
    void Update () {

        var rotation = Quaternion.LookRotation(player.position - gun.transform.position);
        gun.transform.rotation = Quaternion.Slerp(gun.transform.rotation, rotation, Time.deltaTime * 2);
        rotation.x = 0.0f; // not wokring
        rotation.z = 0.0f; // not working

Acces to x, y or z values of a vector, its only for read, if you need set X to 0, you need create new vector

rotation = Quaternion.Euler(0, rotation.eulerAngles.y, 0);

        //This only changes the value of rotation variable. If you need set the gun rotation x and z to 0

        gun.transform.rotation = Quaternion.Euler(0, gun.transform.rotation.eulerAngles.y, 0);
1 Like

Thank you for your reply.
But the script not working. still x,z values updating and locked. Is there something else can i do ?

    public Transform player;
    public Transform gun;
    // Update is called once per frame
    void Update () {
        var rotation = Quaternion.LookRotation(player.position - gun.transform.position);
        gun.transform.rotation = Quaternion.Slerp(gun.transform.rotation, rotation, Time.deltaTime * 2);

        rotation = Quaternion.Euler(0, rotation.eulerAngles.y, 0);
        gun.transform.rotation = Quaternion.Euler(0, gun.transform.rotation.eulerAngles.y, 0);
       
    }