Mathf.Clamp working strangely

So basically I have a spaceship that rotates left and right when the user presses the respective key.
I use Mathf.Clamp to clamp the rotation value between 240 and 300, it starts at 270. My problem is that pressing right it stays locked at 270 (the starting position). Pressing left it will properly go to 240 and back to 270. Sometimes it will go past 270 and lock at 300 but its very unpredictable and happens when pressing the opposite key! So it’s kind of half working. I really can’t see why it’s doing this and it seems pretty simple code. Hope someone can help, thanks! :slight_smile:

        myTrans = GetComponent<Transform>();
        float leftOrRightPressed = Input.GetAxis("Horizontal");
        Vector3 rot = myTrans.eulerAngles;
        if (leftOrRightPressed == -1)
        {
            myTrans.Translate(new Vector3(0, 0, moveSideSpeed) * Time.deltaTime, Space.World);
            rot.x += rotAmount * Time.deltaTime;
        }
        if (leftOrRightPressed == 1)
        {
            myTrans.Translate(new Vector3(0, 0, -moveSideSpeed) * Time.deltaTime, Space.World);
            rot.x -= rotAmount * Time.deltaTime;
        }
        rot.x = Mathf.Clamp(rot.x, 240, 300);
        myTrans.eulerAngles = rot;

generally it’s a bad idea to clamp euler angles directly since the angles wrap around. 0 360 720 1080, all the same angle…

1 Like

I did EXACTLY this same thing before. Well, almost. You’re mushing the input up with if unnecessary if statements.

Try updating the angle by making a max angle variable then multiplying it with the horizontal input.

1 Like

Something like this SHOULD work.

private Transform myTransform = transform;
private float horizontalInput;
public float turnSpeed = 90f;

void Update (){
horizontalInput = Input.GetAxis ("Horizontal");
myTransform.Rotate (Vector3.up, turnSpeed * horizontalInput * Time.deltaTime);

As a bonus, if you plug in a gamepad you can turn slowly or quickly.

1 Like

My angles are between 0 and 360 ,so it should be okay?

Okay, so referring to your two messages, I have removed my if statements(good riddance! Thanks for the tip). Left and right Translate and Rotate are working fine, but when I try and clamp the Euler rotation it stays locked at 270(starting position). Here is my new code.

myTrans = GetComponent<Transform>();
        float leftOrRightPressed = Input.GetAxis("Horizontal");
        Vector3 rot = myTrans.eulerAngles;

        myTrans.Translate(new Vector3(0, 0, leftOrRightPressed * moveSideSpeed * Time.deltaTime), Space.World);
        myTrans.Rotate(leftOrRightPressed * rotAmount * Time.deltaTime, 0, 0, Space.World);
        rot.x = Mathf.Clamp(rot.x, 240, 300);
        myTrans.eulerAngles = rot;

IT’S WORKING! Thank you Not_Sure! I simply moved “Vector3 rot = myTrans.eulerAngles;” To after the Rotate happens and it’s working perfectly, with improved smoothness thanks to removing the ifs. :slight_smile:

You’re setting rot before doing myTrans.Rotate, so when you assign it back to eulerAngles you’re wiping out whatever Rotate did.

Edit: Sounds like you got it.

1 Like

Thanks Errorsatz, looks like we were typing at the same time :stuck_out_tongue:

Glad to hear it.

And again, don’t get hung up on that. I seriously did the EXACT same thing before.