How to control the local axis with LookAt() C#

hi guys… i have this rotation problem that i would love to get some help about it…
here is the thing… i have a 2 piece canon attached to a vehicle… my aiming system pick a point in the world that should be my target… and i use the LookAt function to rotate the canon base, only in the Y axis, to face that point…

Vector3 relativePos = target.position - transform.position;
        Quaternion rotation = Quaternion.LookRotation(relativePos);	
        transform.rotation = Quaternion.Slerp(transform.rotation,rotation,Time.deltaTime*4);

everything works great when the vehicle is on a flat ground… when i go over a hill or something, changing the rotation of the vehicle itself, the canon’s X and Z rotation does not follow up…

i understand what is wrong with the code… but i can think in a way to solve this… thanks in advance…

How about setting the x and z rotation of your cannon to the rotation of the vehicle:

rotation.x = myVehicle.transform.rotation.x;
rotation.y = myVehicle.transform.rotation.z;

Alternatively, you could keep the rotation at whatever it was originally instead of setting it to 0 (might be a better solution)

rotation.x = transform.rotation.x;
rotation.y = transform.rotation.z;

You mention local axis in your question title, but in your code it looks like you’re only referring to world rotation, since you’re using rotation instead of localRotation.