- Home /
limit rotation to 90 degree on one axis
i am making a game in which it first scale the plank on touchPhase.begin and Rotate to z-axis to 90 degree on touchPhase.ended but when i again try to end touchPhase it rotate to 180 degree . i want to limitize to 90 degree only. what should i do?
can you the code you are currently using to do the rotation?
this.gameObject.transform.Rotate(new Vector3(90,0,0))
THanks @mlnczk & @ShadowUser19 it worked in this way. i made variable name as var = movement; and when movement true,i called the $$anonymous$$oment () ftn if ( movement == true) { $$anonymous$$ovement(); } . void $$anonymous$$ovement() { Debug.Log("X");
if (this.gameObject.transform.eulerAngles.x >= 89.5f)
movement = false;
else
{
float move = Time.deltaTime + speed;
transform.Rotate (Vector3.right * move);
}
}
Answer by ShadowUser19 · Dec 26, 2018 at 01:50 PM
I haven't tested it myself, but I think what you are looking for are eulerangles:
var xRotation = this.gameObject.transform.rotation.eulerAngles.x;
if (xRotation <= 90)
{
this.gameObject.transform.Rotate(new Vector3(90,0,0))
}
Answer by mlnczk · Dec 27, 2018 at 08:37 AM
You should use Mathf.Clamp described here : https://docs.unity3d.com/ScriptReference/Mathf.Clamp.html.
You want to do it this way :
float limitedRotation = Mathf.Clamp(limitedRotation, 0, 90); And then in code where you want to limit z axis you just put clamped float -> limitedRotation.
Answer by Charlie_H · Dec 27, 2018 at 09:37 AM
THanks @mlnczk & @ShadowUser19 it worked in this way. i made bool variable name as movement and when movement true,i called the Moment () ftn . " bool movement = false; void Update() { if ( movement == true) { Movement(); } . } void Movement() { if (this.gameObject.transform.eulerAngles.x >= 89.5f) { movement = false; } else {
float move = Time.deltaTime + speed; transform.Rotate (Vector3.right * move); }
} "
Using clamp would be much more efficient. Thats a lot of codes and calling methods for just limiting rotation degrees. Anyways good luck !