- Home /
Transform.rotate limits
Hey. Ther's a way to limit the angles of rotation with transform.rotate? Can someone suggest me an example? I tried this but not work. I also tried to find some old question in unity-answers but nothing. Thanks a lot.
public GameObject[] arm;
float speedUp;
float speedDown;
Vector3 StartPosition;
void Awake()
{
speedUp = 80f;
speedDown = 60f;
}
// Update is called once per frame
void Update () {
//go up
if (Input.GetKey (KeyCode.Z)) {
arm[0].transform.Rotate(Vector3.right * Time.deltaTime*speedUp);
if(transform.Rotate > 50) //rotation limits
{
transform.Rotate = 50;
}
}
//go down
if (Input.GetKey (KeyCode.X)) {
arm[0].transform.Rotate(-Vector3.right * Time.deltaTime*speedDown);
}
}
Answer by cl0n · Nov 03, 2016 at 12:50 PM
Well, try this, It's easy to understand as well.
(Declaration) public float rotationZ; public float sensitivityZ = 5;
void Start()
{
transform.rotation = Quaternion.identity;
}
void Update()
{
Debug.Log(transform.rotation.eulerAngles.z);
LockedRotation();
}
void LockedRotation()
{
rotationZ += Input.GetAxis("Mouse ScrollWheel") * sensitivityZ * 10;
rotationZ = Mathf.Clamp(rotationZ, -45.0f, 45.0f);
transform.localEulerAngles = new Vector3(transform.localEulerAngles.x, transform.localEulerAngles.y, -rotationZ);
}
Answer by Lovrenc · May 31, 2014 at 10:03 AM
1) Check out this function.
2) Search old questions.
3) Look in MouseLook script in Standard Assets, it has this implemented
I applied the mathf.Clamp to my script but unity give me error.
arm[0].transform.Rotate(Vector3.right ($$anonymous$$athf.Clamp(10, 1, 3) * Time.deltaTime*speedUp);
but for me is very difficult because is all in english! So I try to understand from examples. And i don't need to lear all, i use the scrips very little
Someone know an example of mathf.Clamp that work with transform.rotate?
Uff i tried this way but give me error.. :(
$$anonymous$$athf.Clamp(arm[0].transform.Rotate(Vector3.right * Time.deltaTime*speedUp), $$anonymous$$Rotation, maxRotation);
Answer by Khaeops · Aug 28, 2015 at 11:13 AM
I haven't tried it yet, but I believe:
transform.localRotation.x = Mathf.Clamp(transform.localRotation.x, Max, Min);
where the transform.localRotation.x in the brackets is your current value, then it restricts it between your max and min values. You can set them with floats or integers.
Correct me if I'm wrong though, I myself am trying to get a local rotation restriction going too :P
Well for starters you cannot individually set a single variable of any property(position, scale, rotation) of transform. In order to change only one variable you must set the whole property by keeping the other variables same.
At the time I posted my answer, I was aware that you could do it in Javascript (Unityscript?). By now, I use C#, and yes, you do need to modify the property via another temporary variable.
Your answer
![](https://koobas.hobune.stream/wayback/20220613150327im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Following another object's position/rotation like parent/child relationship? 4 Answers
Smooth Rotation Help 1 Answer
I want to restrict the rotation with the following Script,I want to restrict tranform.Rotation 0 Answers
How do I align my player transform to waypoints and without restricting Z Axis rotation? 2 Answers
Limiting rotation of object, specifically using scroll wheel 2 Answers