I have two scripts for this and how it is supposed to work is, if I enter a trigger, my isClimbingWall bool turns true which performs specific movements inside of my WallClimbing script. I also use that bool in my second script CameraLook in order to clamp my x-axis rotation.
// WallClimbing script
public void OnTriggerEnter(Collider other)
{
if (col.gameObject.tag == "Wall")
{
isClimbingWall = true;
}
}
To get the clamp to work, I just add a quick if statement in the middle of my code so that if I am climbing, clamp x-axis rotation but it does not seem to work. I replaced eulerAngles with AngleAxis but I still get gimbal lock.
// CameraLook script
Vector2 mouseDelta = new Vector2(Input.GetAxis(xAxis), Input.GetAxis(yAxis));
rotation.x += mouseDelta.x * sensitivity;
//Keep rotation in the range 0-360.
if (rotation.x < 0) rotation.x += 360f;
if (rotation.x > 360f) rotation.x -= 360f;
rotation.y += mouseDelta.y * sensitivity;
rotation.y = Mathf.Clamp(rotation.y, -yRotationLimit, yRotationLimit); // vertical clamp
if (wallClimbing.isClimbingWall)
{
//horizontal clamp
rotation.x = Mathf.Clamp(rotation.x, -xRotationLimit, xRotationLimit); // xRotLimit 35f
}
var xQuaternion = Quaternion.AngleAxis(rotation.x, Vector3.up);
var yQuaternion = Quaternion.AngleAxis(rotation.y, Vector3.left);
transform.localRotation = yQuaternion;
player.transform.rotation = xQuaternion;
However what happens is, no matter which angle the wall is positioned, when I enter the trigger my player/camera rotation will "snap" forward.
Limiting the rotation to +/-35 degrees will yield +35 for 270 degrees by applying only Clamp, but should yield -35, since 270 is the same as -90.
Therefore, create a function which normalizes the angle between -180 and +180 degrees:
public static float NormalizeAngle(float angle)
{
angle = (angle + 180) % 360;
if (angle < 0) {
angle += 360;
}
return angle - 180;
}
Then you can write:
rotation.x =
Mathf.Clamp(NormalizeAngle(rotation.x), -xRotationLimit, xRotationLimit);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With