When I enter triggers for different types of climbing like mantling, vaulting and ladder climbing, I am trying to restrict the actual players rotation so that the 3D model (capsule in this case) does not rotate when I move my mouse. I’ve been trying to change it so that only the camera rotates while the player maintains its rotation that it had upon entering the trigger. I just don’t know how to separate them without temporarily unparenting my camera because then that causes rotation “snapping” issues.
My player hierarchy is:
Player (parent) - empty object, has player movement script and character controller component
Capsule (child of Player) - player mesh/3D model
CameraContainer (child of Player) - empty object, has camera script
Main Camera (child of CameraContainer) - the actual camera
This is my climbing related camera code, and I use currentRotation
from another script which gets my players current rotation upon entering the trigger for the x-axis clamp while climbing:
private Vector2 rotation = Vector2.zero;
private const string xAxis = "Mouse X";
private const string yAxis = "Mouse Y";
private float xRotationLimit = 40f;
void ClimbingCameraMovement(Quaternion currentRotation)
{
Vector2 mouseInput = new Vector2(Input.GetAxis(xAxis), Input.GetAxis(yAxis));
rotation.x += mouseInput.x * sensitivity;
rotation.y += mouseInput.y * sensitivity;
rotation.y = Mathf.Clamp(rotation.y, -88f, 88f);
Quaternion yQuat = Quaternion.AngleAxis(rotation.y, Vector3.left);
Quaternion xQuat = Quaternion.RotateTowards(currentRotation, Quaternion.AngleAxis(rotation.x, Vector3.up), xRotationLimit);
rotation.x = xQuat.eulerAngles.y;
transform.localRotation = yQuat;
player.transform.rotation = xQuat; // the parent 'Player' object is used
}