How to limit object rotation on y axis pressing key

I am trying to move a person head with the left and right arrow keys pretending he is watching a concert. The head transform rotations initialize at 0 (x), -180 (y), 0 (z) because the person is looking the orchestra.

My current code is working but now I want to limit the rotations. I would like the following 2 conditions:

  • When pressing the right key, move the head from -180 to -90 (on the Y axis).
  • When pressing the left key, move the head from -180 to-270 (on the Y axis).

I have the following code:

public class HeadController : MonoBehaviour {

public Camera cam;
public float speed;
private Rigidbody2D rb;
        
void Start()
{
    rb = GetComponent<Rigidbody2D>();

    if (cam == null)
    {
        cam = Camera.main;
    }

}

void FixedUpdate()
{
    if (Input.GetKey(KeyCode.RightArrow)) 
    {
        if ()   // Y rotation: from -180 to -90 
        {
            transform.Rotate(Vector3.up * speed * Time.deltaTime);
        }
    }
    else if (Input.GetKey(KeyCode.LeftArrow))   
    {
        if ()   // Y rotation: from -180 to -270 
        {
            transform.Rotate(-Vector3.up * speed * Time.deltaTime);
        }
    }
}

}

I need to fulfill the if() conditions.

Thank you!!

Hi Clararende,

this is a (kinda?) cheap way to do what you want.
This class can be used to rotate around something, and I use this for clamping camera positions as well!
All you have to do is create an new object inside the camera and set the Z position of the new op on 0.1

then create new script , name it like RotateAround.cs

    [SerializeField] private Transform _rotTarget; // set in editor
    [SerializeField] private float _minRotY = -20.0f; // change in editor
    [SerializeField] private float _maxRotY = 20.0f; // change in editor
    
    [SerializeField] private float _rotSpeed = 20.0f;  // change in editor
    
    private Vector3 _initialVector = Vector3.forward;
    	             
    void Start() {
		if(_rotTarget==null) Debug.LogError("Please assign the rotation target!");
		_initialVector = transform.position - _rotTarget.position;
		_initialVector.y = 0;
	}

	void FixedUpdate()  {
		float rotateDegrees = 0f;
		float speed = Time.deltaTime * _rotSpeed;

		if (Input.GetKey(KeyCode.RightArrow)) {
			rotateDegrees += speed;
		} else if (Input.GetKey(KeyCode.LeftArrow)) {
			rotateDegrees -= speed;
		}
              
		Vector3 currentVector = transform.position - _rotTarget.position;
		currentVector.y = 0;

		float angleBetween = Vector3.Angle(_initialVector, currentVector) * (Vector3.Cross(_initialVector, currentVector).y > 0 ? 1 : -1); // calculating angle    
		float newAngle = Mathf.Clamp(angleBetween + rotateDegrees, _minRotY, _maxRotY); // clamping angle
		rotateDegrees = newAngle - angleBetween;
          
		transform.RotateAround(_rotTarget.position, Vector3.up, rotateDegrees); // rotation
	}

Set in the editor the _rotTarget to the object you have set on the camera with an Z pos of 0.1

Now you can clamp it to what you want, this example clamp’s it from -20 degrees to 20 degrees, but it is all yours to do whatever you like I hope this helped a bit!

NOTE: this script is also very usefull for 3rd person camera behaviour to clamp around a player! :wink: