How to use Quaternion.slerp and Quaternion.LookRotation with a child gameobject
I'm trying to add a lock-on feature to my player, but I'm having issues with my camera. I'm trying to use LookRotation than slerp to make my camera look at his target. The camera can ONLY rotate around it's X axis. I'm having issues because of the way my player his made.
I have a character that uses a character controller. It has a children camera. When the player looks right or left, the character rotate around it's Y axis (since the parent rotates, so will the children camera). When the player looks up or down, the camera rotate around it's X axis.
When I try to lock-on, the player rotate correctly, but it's children camera rotates on his X axis but it rotate it's Y and Z axis to make it 0. The end result is that if my player lock on to an object that is at his left, the player will correctly face the left side, but the camera will end up looking in the global foward axis, but with the right X rotation.
Here is my code:
public void rotateCamera(Vector3 target, bool debug = false)
{
Vector3 cameraToTarget = target - transform.position;
cameraToTarget.x = 0f;
Quaternion rotation = Quaternion.LookRotation(cameraToTarget);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * lockOnSpeed);
}
I know that it does that because I use = operator, but I don't know how I can make the camera rotate around it's X axis AND make sure that will correctly turn with his parent. I want specifically that the parent control the rotation around the Y axis for the entire player, but that the camera only controls it's X rotation and just goes along with the rotation of his parent.