I am attempting to limit the player's view at the x euler angle of -80 and 80 (not final values), to prevent the player looking upside down. I do this by "wrapping" the values so I can work with angles in the range of -90 to 90 instead of 90 to 0 and 270 to 360 which aren't good for clamping the values, which I also do (360 degrees will now be -90 and so on). That clamp part works just fine.
Please consider the following code:
var wrappedX : float;
Inside update function:
transform.localEulerAngles = Vector3(Mathf.Clamp (wrappedX, -80, 80), 0, 0);
if (transform.localEulerAngles.x >= 270) {
wrappedX = transform.localEulerAngles.x - 360;
}
else {
wrappedX = transform.localEulerAngles.x;
}
print(Vector3(Mathf.Clamp (wrappedX, -80, 80), 0, 0));
It appears to be working fine except for the part where I actually assign the x euler angle as the new clamped value In this part of the code that you also read above:
transform.localEulerAngles = Vector3(Mathf.Clamp (wrappedX, -80, 80), 0, 0);
For some reason the player's x euler angle is forced to 0, therefor the player can only look side to side (handled in other code not shown), and not up or down at all. the player's view is stuck at 0 degrees.
So what specifically am I doing wrong here? I am very new to angles and stuff so exact code on what will fix it is perfered. Can anyone help me out here? Thanks.
EDIT: After more testing I have concluded that the problem is not that code, but the way it combines with this code:
transform.Rotate (lookSpeed * -Input.GetAxis ("Mouse Y"),0, 0)
This is causing the player's view to not be able to move.
Can you elaborate on how I would use transform.Rotate in this situation specifically?
– anon81475095I'd advice just checking out the mouseLook script that comes standard with Unity. It does exactly this for both x and y with a variable clamp. Just distill from there. :)
– anon73820239I edited the answer.
– anon73820239Unknown identifier: 'ClampAngle'.
– anon81475095Fixed it, check the edited answer :)
– anon73820239