how can i look at a target on 2 axis but clamp the the object's rotation ,to only rotate from -45 to 45 on x and from -60 to 60 on y axis. i used this script to look at target and clamp the rotation, but it's not working right ): there is no problem with maximum value but whenever the rotation is about to become zero or a negative value the wired problem will show up. i made a gif here (http://www.mediafire.com/view/55phgr5qrhy8fk1/record.gif)

public Transform target;
public float speed;

public float minimumX;
public float maximumX;

public float minimumY;
public float maximumY;
// Start is called before the first frame update
void Start()


// Update is called once per frame
void Update()

    Quaternion OriginalRot = transform.rotation;
    Quaternion NewRot = transform.rotation;
    transform.rotation = OriginalRot;
    transform.rotation = Quaternion.Lerp(transform.rotation, NewRot, speed * Time.deltaTime);

    transform.eulerAngles = new Vector3(Mathf.Clamp(transform.eulerAngles.x, minimumX, maximumX), Mathf.Clamp(transform.eulerAngles.y, minimumY, maximumY), 0);



  • \$\begingroup\$ You put minimumX as both arguments in the first clamp \$\endgroup\$ – Bálint Jun 11 '19 at 10:11
  • \$\begingroup\$ ok, i fixed that one,but still it doesn't work right.when my camera is about to reach the minimum amount of rotations it shows some weird behavior \$\endgroup\$ – h00man13 Jun 11 '19 at 10:47
  • \$\begingroup\$ i made a gif to show the problem \$\endgroup\$ – h00man13 Jun 11 '19 at 12:09

My guess that eulerAngles returns angle from 0 to 360, so "-1" is 359 and is clamped to maximumY.

Try this:

float ry = transform.eulerAngles.y;
if (ry >= 180) ry -= 360;
transform.eulerAngles = new Vector3 (
    Mathf.Clamp(transform.eulerAngles.x, minimumX, maximumX),
    Mathf.Clamp(ry, minimumY, maximumY),
  • \$\begingroup\$ BEAUTIFUL, thanks man it works perfectly :) \$\endgroup\$ – h00man13 Jun 11 '19 at 13:14
  • \$\begingroup\$ @h00man13 you can also upvote my answer if you like it, I'll get another +10 rep \$\endgroup\$ – trollingchar Jun 11 '19 at 13:22
  • \$\begingroup\$ I'd love to upvote you but unfortunately my reputation is less than 15 \$\endgroup\$ – h00man13 Jun 11 '19 at 14:57
  • \$\begingroup\$ @h00man13 I thought the threshold was 10 \$\endgroup\$ – trollingchar Jun 11 '19 at 15:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.