- Home /
The question is answered, right answer was accepted
How limit the rotation of an object Or if there is other way to rotate
Hello Every One.
How can i limit the rotation of an object by the Axis of Horizontal (ont Y axis), i know there is method called Mathf.clamp but i don't know how to use it for below code.
If there is another why to Rotate an object instead of the code that i am using below that will be helpful too.
transform.Rotate(0, Input.GetAxis("Horizontal")*Time.deltaTime*turnSpeed, 0);
thanks to those who help!!!
Note: i Included Time.deltaTime*turnSpeed on the code to so that nobody get confuse
can you elaborate on the question. I dont understand what you are trying to achieve. Are you trying to limit how much the object can rotate on the Y axis? You could use an if statement, check the rotation of the object and if the rotation is below a certain value then rotate it otherwise dont rotate it.
I'm not sure exactly what you are asking, but your code will detect your "Horizontal Input" (maybe a joystick or mouse), and rotate the gameobject about the Y axis based on the magnitude of your joystick/mouse movement.
Odds are you will want to multiply that by Time.deltatime to normalize the rotation speed.
And like TRG96 said, you can use an if statement to limit the total rotation of the object.
@TRG96 Thanks for the quick replay.
Yes i want to limit the rotation of object on the Y axis. I don't know how to use the if statement on that code should i use if (input.GetAxis("Horizontal")) or if you can give an example of the if statement how can i use. i was trying to make reference to transform.Rotate but it was giving error so that's why i am lost about if statement i don't know how to limit that in if statement.
and thanks Jonesy19 and TRG69 again for your quick replies
Answer by Side.MZG · Mar 31, 2015 at 10:18 PM
The simplest option would be:
public float rotationSpeed = 5.0f;
public float minimumValue = -30f;
public float maximumValue = 30f;
void Update(){
transform.Rotate(0, Input.GetAxis("Horizontal")*rotationSpeed*Time.deltaTime, 0);
//The function is Mathf.Clamp(value, min, max)
transform.eulerAngles = new Vector3(transform.eulerAngles.x, Mathf.Clamp (transform.eulerAngles.y, minimumValue, maximumValue), transform.eulerAngles.z);
}
However, it has a problem. Unity automatically converts negative angles to their positive counterpart. For example, -10 becomes 350 (360-10). The problem is, that as soon as that happens our code realizes 350 is bigger than 30, so it clamps it. The solution to this is the following:
public float rotationSpeed = 5.0f;
public float minimumValue = -30f;
public float maximumValue = 30f;
void Update(){
transform.Rotate(0, Input.GetAxis("Horizontal")*rotationSpeed*Time.deltaTime, 0);
//The function is Mathf.Clamp(value, min, max)
if(transform.eulerAngles.y < 180){
transform.eulerAngles = new Vector3(transform.eulerAngles.x, Mathf.Clamp (transform.eulerAngles.y, 0, maximumValue), transform.eulerAngles.z);
}
else{
transform.eulerAngles = new Vector3(transform.eulerAngles.x, Mathf.Clamp (transform.eulerAngles.y-360, minimumValue, 0), transform.eulerAngles.z);
}
}
Here we are checking the angle to see from which 'side' of our imaginary circle to tackle the limitation. For all angles over 180 degrees, we assume the original angle was negative and try to recover it. (-10 is converted to 350, and we recover it by doing 350-360)
Hi Side.$$anonymous$$ZG
thanks for reply
i did use the time.delta time but still i cannot limit my rotation on the Y axis
for example i don't want to turn more then 30 and -30 even if i keep press the button but i don't go over those numbers
even i did try with FixedUpdate the code is not applying the limit to object rotation
I just found out why. We're not clamping the rotation, we're limiting the rotation speed. I'm testing a new version of the code and will place it in a moment
There, I've tested this and it works for me. I'm sorry I confused you for so long.
Follow this Question
Related Questions
Limit Rotations with Min and Max on Mobile 1 Answer
Limiting RotateTowards on two axes 1 Answer
Limiting rotation of object, specifically using scroll wheel 2 Answers
Limit GUI Rotation? 2 Answers
Raycasting, rotating a ray 360 degrees 2 Answers