- Home /
Mathf Clamp doesn't work
Good morning, I cannot set the max e min rotation limits, I followed an example that did so, but to me don't work..
Thanks for your support.
public GameObject[] arm;
float speedUp;
float speedDown;
Vector3 StartPosition;
void Awake()
{
speedUp = 80f;
speedDown = 60f;
}
void Update () {
//go up
if (Input.GetKey (KeyCode.Z)) {
arm[0].transform.Rotate(Vector3.right * Time.deltaTime*speedUp);
speedUp = Mathf.Clamp(speedUp, 0f, 5f);
}
//go down
if (Input.GetKey (KeyCode.X)) {
arm[0].transform.Rotate(-Vector3.right * Time.deltaTime*speedDown);
speedDown = Mathf.Clamp(speedDown, 0f, 5f);
}
}
What does "don't work" mean? What doesn't happen?
if i'm not mistaken you need to add the rotate command after you do the clamp becasue right now the rotate is happening with the original speed, try to debug your speeds to watch their values if they actualy go higher than the max value
Also if i set the mathf clamp, the rotation is still at 360°.. there aren't limits.
What are you trying to clamp? The rotation? Right now you are clamping the speed up and down values to 5 each frame
Answer by flower · Jun 18, 2014 at 11:49 AM
If you want to clamp the rotation and i guessed your intentions right, you will need something like
if (Input.GetKey (KeyCode.Z)) {
Vector3 rot = Vector3.right * Time.deltaTime*speedUp;
float rotX = Mathf.Clamp(rot.x, <minX>, <maxX>); // insert desired min/max values
float rotY = Mathf.Clamp(rot.y, <minY>, <maxY>); // insert desired min/max values
float rotZ = Mathf.Clamp(rot.z, <minZ>, <maxZ>); // insert desired min/max values
arm[0].transform.Rotate(rot);
// rest of code
}
@flower, thank you so much for your support, I insert the desired values just for try if it work, but any values that i inserted doesn't work. xD It's strange, but the rotation up and down is still at 360° why?
-Paolo
//go up
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.Z)) {
Vector3 rot = -Vector3.right * Time.deltaTime*speedUp;
float rotX = $$anonymous$$athf.Clamp(rot.x, 0.1f, 0.5f);
arm[0].transform.Rotate(rot);
}
//go down
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.X)) {
Vector3 rot = Vector3.right * Time.deltaTime*speedUp;
float rotX = $$anonymous$$athf.Clamp(rot.x, 0.1f, 0.5f);
arm[0].transform.Rotate(rot);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613151647im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Implementing mathf ? 1 Answer
Mathf.Clamp is not Clamping. 1 Answer
Modify distance with var? 1 Answer