- Home /
Restrict min/max rotation STILL wont work
How would I restrict the rotation so that when i get to a certain amount it stays there.
here is what i have
minRotation variable?
if (Input.GetKey(KeyCode.LeftArrow)) {
transform.Rotate(0,Time.deltaTime * 100,0);
}
if(SOMETHING == minRotation) {
stay at min rotation
}
i want it to only check the Y rotation
this is what ive got atm, now and still cant figure it out:
public Vector3 myEuler = Vector3.zero;
public float rotVel;
void Start () {
myEuler.x = transform.eulerAngles.x;
}
void Update () {
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow)) {
transform.Rotate(0,Time.deltaTime * 50,0, Space.World);
}
else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow)) {
transform.Rotate(0,Time.deltaTime * -50,0, Space.World);
}
else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow)) {
myEuler.x += rotVel * Time.deltaTime;
}
else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.DownArrow)) {
myEuler.x -= rotVel * Time.deltaTime;
}
}
Answer by Destran · Mar 15, 2014 at 10:07 AM
you could use Mathf.Clamp, so you could try something like:
function Update ()
{
transform.rotation = Vector3(Mathf.Clamp(transform.rotation, desired minimum rotation, desired maximum rotation), 0, 0);
}
// this would affect your x rotation. If you wanted it in your y or z you could just move the zeroes around so like this for Y rotation:
function Update ()
{
transform.rotation = Vector3(0,Mathf.Clamp(transform.rotation, desired minimum rotation, desired maximum rotation), 0);
}
how would i store it in a variable and check if its the same as something else though?
well I'm fairly new to scripting but in your instance you wouldn't need to store it in a variable. Because once it was over a certain rotation it would clamp to your desired $$anonymous$$/max rotation.
So from the example you gave what you would would need:
function Update ()
{
transform.rotation = Vector3(0,$$anonymous$$athf.Clamp(transform.rotation, desired $$anonymous$$imum rotation, desired maximum rotation), 0);
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow)) {
transform.Rotate(0,Time.deltaTime * 100,0);
}
im gettin this error:
Expression denotes a `type', where a `variable', `value' or `method group' was expected
it appears I led you astray :'( I was going almost entirely off of http://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.Clamp.html. Let me fix this and then get back to you
Your answer
![](https://koobas.hobune.stream/wayback/20220613140305im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Is it possible to add a point to the score, everytime the Player rotates 180°? 2 Answers
Rotating Character 1 Answer