Mathf.Clamp sets rotation when clamping

I’m having trouble clamping the rotation of my camera. I want to clamp it between -90 and 90 when my character is grounded, otherwise remove the clamp completely. This works fine currently, except that when the clamp gets re-applied after landing on the ground (and you have done a backflip, so the angle is probably above 360) it sets the camera angle to -90. This happens even when I’m looking in the same direction as when I didn’t do the back flip. In short, after you do a backflip and land back on the ground, the camera snaps and looks straight up.

This is the code for the clamping:
image

Is there another way to clamp the angle? Or is there another way to stop this from happening?

1 Like

It’s hard to tell what _mouseY actually contains. The name suggests that it’s either the mouse position or a mouse delta. In both cases your clamping wouldn’t make sense ^^. So I guess this variable is just badly named and actually represents the pitch angle of your character?

When you do weird rotations when in air, you may want to make sure the angle is back in the right range.

For example

while (_mouseY > 180)
    _mouseY -=360f;
while (_mouseY < -180)
    _mouseY +=360f;

This will make sure the angle properly wraps around whenever it gets larger than 180° (in which case it jumps to -180) or when it gets smaller than -180 (then it jumps back to 180°).

Of course if you actually land upside down, it would still clamp you to your ± 90° range, but I’m not sure that’s even possible in your game :slight_smile:

Switching between different controller modes is generally difficult. In SpaceEngineers when you enable your jetpack you have full 6 degrees of freedom (move in any direction and rotate around all 3 axis freely, Q/E rotate around z). When you disable the jetpack while in a gravity well (artificial or not), your character aligns itself with the gravity vector. It also aligns your view being perpendicular to gravity, parallel to the “ground”. This can sometimes be annoying but actually helps to align yourself in space. As I said, we don’t know what kind of game you make and when you say backfilp it may just be an animation that always plays out fully so you don’t have to worry about as long as you wrap your angle properly.

When using my code snippet, any angle between 90 and 180 would snap to 90 and any angle between -180 and -90 would snap to -90. Though when your angle ends up roughly in your normal view range, it should be between -90 and 90 anyways. So if you looked at 30° up, doing a flip which may add or subtract 360 to / from your angle, the code I posted would undo that full rotation and you’re back at 30… That’s because a value like 390° is greater than 180 and a value like -330 is smaller than -180.

Hi yes this fixed the problem! All _mouseY is, is this
image

I thought it would be something about the angle begin greater than 360 but didn’t know how to solve it! Thanks for the help. I’ll look into some quality of life things like the thing you mentioned with SpaceEngineers!

1 Like