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);

			
		}
	}

3 Answers

3

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);
		}

Thank you!

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 :stuck_out_tongue:

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.

  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 (Mathf.Clamp(10, 1, 3) * Time.deltaTime*speedUp);

Holy king of copy. This is not unity specific anymore, you clearly don't understand the code or programming principles. Compiler doesent know what to make of your Vecto3 and Clamp, you used no operator or anything. Usage: Mathf.Clamp(currentRotation, minRotation, maxRotation)

Method Rotate does not return anything. You therefore cannot save it in your currentRotation variable. I am sorry, but point of unity answers is not lecturing. While you can copy rotation implementation from MouseLook script that comes along in Standard Assets, you WILL have to learn to code to efficiently do anything. Without understanding even the slightest changes will stop your progress. You can try [this tutorial][1]. You cannot run without learning to walk first. It's same in programming. [1]: http://www.learncs.org/

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?