- Home /
How to limit rotation of an object when pressing a key?
Currently, my gameObject is rotated up and down by pressing a key. This is my code:
switch (cannon){
case 0: break;
//press W
case 1: Cannon.transform.localEulerAngles -= new Vector3(cannonRotateSpeed * Time.deltaTime, 0, 0);
// print(Cannon.transform.rotation);
;break;
//press S
case 2: Cannon.transform.localEulerAngles += new Vector3(cannonRotateSpeed * Time.deltaTime, 0, 0);
;break;
}
I want when the user keeps pressing W, the Cannon rotate up but Rotation X in the inspectator capped at -25. And capped at 25 when pressing S(rotate down).
Answer by knobblez · Jan 22 at 10:59 AM
You just need to get the current rotation and do a IF STATEMENT to check if it is acceptable to rotate more.
switch (cannon)
{
case 0:
{
break;
}
//press W
case 1:
{
if (Cannon.transform.rotation.x > -25f)
{
Cannon.transform.localEulerAngles -= new Vector3(cannonRotateSpeed * Time.deltaTime, 0, 0);
// print(Cannon.transform.rotation);
}
break;
}
//press S
case 2:
{
if (Cannon.transform.rotation.x < 25f)
{
Cannon.transform.localEulerAngles += new Vector3(cannonRotateSpeed * Time.deltaTime, 0, 0);
}
break;
}
}
as a sidenote: This is not precise. Not at all. Furthermore it gets worse the smaller the framerate.
imagine you have an angle of -24.95f and thus decide that you can still rotate. Your rotationspeed is 2f and you get a laggy frame that is like 1 second long. Then you have rotated to -26.95f.
A better check would be if the current angle + the change is larger than the max value. If it is then rotate to the max angle. If it isn't you can normally apply the rotation.
I really meant <= 23f but I figured he'd get the gist. You're right though. Would be safer to check currentAngle + changeAmount. And probably have it allow the difference if below 25f. That way it's consistent and won't sometimes go 24.98 and sometimes 25.
Sorry man, I thought rotation.x refers to X rotation in the inspectator
What I really want is transform.localEulerAngles capped at -25 and 25. But when the object rotate up, it show in the image that localEulerAngles.x = -25. But when I print it in console it give me 334.2495
this really should be the first thing unity teaches about itself...
it comes up so often...
Unity will not display negative angles. So when you rotate something and the angle would be negative unity just adds 360. Which is annoying but makes sense if you think a bit further.
So what can you do?
Add another if statement.
pseude code:
angle = transform.rotation.x
if angle > 180
angle -= 360
then do the check you currently do.
not really that nice but should do the trick
Thanks for that. I got your idea and It's work!
Answer by bhavinbhai2707 · Jan 22 at 02:21 PM
There is a very simple way to clamp the rotation of the Gameobject. You need to use Mathf.Clamp01 in your situation. The above-provided solution by Knoblez is not precise and has a chance of failure due to FPS issues.
Mathf.Clamp01 is used to limit a value between two numbers.
//This will clamp the rotation of the object between minRotation and maxRotation.
float minRotation = 1f;
float maxRotation = 90f;
Vector3 currentRotation = transform.localRotation.eulerAngles;
currentRotation.z = Mathf.Clamp01(currentRotation.z, minRotation, maxRotation);
transform.localRotation = Quaternion.Euler(currentRotation);
Simply add this piece of code after you change the rotation of the object.
I just read the docs that u provide and it shows that Mathf.Clamp01(float value) only take 1 argument.
I provided you with a code snippet on how to use it. If you suppose just need to clamp one axis, like suppose z? then use the code above, or if you need to clamp all the values then simply clamp all three axis as
//This will clamp the rotation of the object between $$anonymous$$Rotation and maxRotation.
float $$anonymous$$Rotation = 1f;
float maxRotation = 90f;
Vector3 currentRotation = transform.localRotation.eulerAngles;
currentRotation.x = Mathf.Clamp01(currentRotation.x, $$anonymous$$Rotation, maxRotation);
currentRotation.y = Mathf.Clamp01(currentRotation.y, $$anonymous$$Rotation, maxRotation);
currentRotation.z = Mathf.Clamp01(currentRotation.z, $$anonymous$$Rotation, maxRotation);
transform.localRotation = Quaternion.Euler(currentRotation);
I think you are suggesting me to use Math.Clamp() instead of Math.Clamp01(). Because it only clamps value between 0 and 1 and returns value. But I agree that ur solution solve the Knoblez's answer problem. However, eulerAngles does not take negative value as show in the Inspectator image I provided. When I print it on console it show me 337, instead of -23. So we need to convert 337 to -23 first then we can use Math.Clamp() to limit the rotation
Your answer
![](https://koobas.hobune.stream/wayback/20220613052011im_/https://answers.unity.com/themes/thub/images/avi.jpg)