# Mathf.Clamp working strangely

**URL:** https://discussions.unity.com/t/mathf-clamp-working-strangely/624713
**Category:** Scripting
**Tags:** Scripting
**Created:** [April 21, 2016, 6:55am UTC](https://discussions.unity.com/t/mathf-clamp-working-strangely/624713 "2016-04-21T06:55:33Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![uns0ciabl3](https://avatars.discourse-cdn.com/v4/letter/u/59ef9b/32.png) [@uns0ciabl3](https://discussions.unity.com/u/uns0ciabl3)
#### Post date: [April 21, 2016, 6:55am UTC](https://discussions.unity.com/t/mathf-clamp-working-strangely/624713/1 "2016-04-21T06:55:33Z")

</div>

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! 🙂

```csharp
        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;

```

---

<div class="post-metadata">

### Author: ![LeftyRighty](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@LeftyRighty](https://discussions.unity.com/u/LeftyRighty)
#### Post date: [April 21, 2016, 8:54am UTC](https://discussions.unity.com/t/mathf-clamp-working-strangely/624713/2 "2016-04-21T08:54:13Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Not\_Sure](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/not_sure/32/1011552_2.png) [@Not\_Sure](https://discussions.unity.com/u/Not_Sure)
#### Post date: [April 21, 2016, 9:08am UTC](https://discussions.unity.com/t/mathf-clamp-working-strangely/624713/3 "2016-04-21T09:08:20Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Not\_Sure](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/not_sure/32/1011552_2.png) [@Not\_Sure](https://discussions.unity.com/u/Not_Sure)
#### Post date: [April 21, 2016, 9:44am UTC](https://discussions.unity.com/t/mathf-clamp-working-strangely/624713/4 "2016-04-21T09:44:11Z")

</div>

Something like this SHOULD work.

```csharp
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.

---

<div class="post-metadata">

### Author: ![uns0ciabl3](https://avatars.discourse-cdn.com/v4/letter/u/59ef9b/32.png) [@uns0ciabl3](https://discussions.unity.com/u/uns0ciabl3)
#### Post date: [April 21, 2016, 11:33pm UTC](https://discussions.unity.com/t/mathf-clamp-working-strangely/624713/5 "2016-04-21T23:33:47Z")

</div>

> [@LeftyRighty](#):
>
> generally it’s a bad idea to clamp euler angles directly since the angles wrap around. 0 360 720 1080, all the same angle…

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

---

<div class="post-metadata">

### Author: ![uns0ciabl3](https://avatars.discourse-cdn.com/v4/letter/u/59ef9b/32.png) [@uns0ciabl3](https://discussions.unity.com/u/uns0ciabl3)
#### Post date: [April 21, 2016, 11:37pm UTC](https://discussions.unity.com/t/mathf-clamp-working-strangely/624713/6 "2016-04-21T23:37:48Z")

</div>

> [@Not\_Sure](#):
>
> Something like this SHOULD work.
> 
> ```csharp
> 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.

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.

```csharp
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;

```

---

<div class="post-metadata">

### Author: ![uns0ciabl3](https://avatars.discourse-cdn.com/v4/letter/u/59ef9b/32.png) [@uns0ciabl3](https://discussions.unity.com/u/uns0ciabl3)
#### Post date: [April 21, 2016, 11:45pm UTC](https://discussions.unity.com/t/mathf-clamp-working-strangely/624713/7 "2016-04-21T23:45:21Z")

</div>

IT’S WORKING! Thank you Not\_Sure! I simply moved “[Vector3](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=Vector3) rot = myTrans.[eulerAngles](http://unity3d.com/support/documentation/ScriptReference/30_search.html?q=eulerAngles);” To after the Rotate happens and it’s working perfectly, with improved smoothness thanks to removing the ifs. 🙂

---

<div class="post-metadata">

### Author: ![Errorsatz](https://avatars.discourse-cdn.com/v4/letter/e/77aa72/32.png) [@Errorsatz](https://discussions.unity.com/u/Errorsatz)
#### Post date: [April 21, 2016, 11:45pm UTC](https://discussions.unity.com/t/mathf-clamp-working-strangely/624713/8 "2016-04-21T23:45:37Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![uns0ciabl3](https://avatars.discourse-cdn.com/v4/letter/u/59ef9b/32.png) [@uns0ciabl3](https://discussions.unity.com/u/uns0ciabl3)
#### Post date: [April 21, 2016, 11:48pm UTC](https://discussions.unity.com/t/mathf-clamp-working-strangely/624713/9 "2016-04-21T23:48:10Z")

</div>

> [@Errorsatz](#):
>
> 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.

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

---

<div class="post-metadata">

### Author: ![Not\_Sure](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/not_sure/32/1011552_2.png) [@Not\_Sure](https://discussions.unity.com/u/Not_Sure)
#### Post date: [April 22, 2016, 5:53am UTC](https://discussions.unity.com/t/mathf-clamp-working-strangely/624713/10 "2016-04-22T05:53:55Z")

</div>

Glad to hear it.

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