How to limit the angle of the camera? (521495)

here is a third-person camera, her MouseOrbit script. I need to limit the angle of rotation of the camera along the axis X, with respect to the Target. Target - an object rigidbody.

You can do this:

add to script variables

public var xMinLimit : float = -120;
public var xMaxLimit : float = 120;

Add in Lateupdate

x = ClampAngle(x, xMinLimit, xMaxLimit);

and slightly lower values ​​multiply rotation

var baseRotation : Quaternion = target.rotation;
rotation = baseRotation * Quaternion.Euler (y, x, 0);

Everything works, the rotation angle is limited as it is necessary. The problem is that the camera must not be attached to the object rigidbody. Luggage must not depend on the rotation Target. Need to angle limited, but the camera was free. I’ve tried a lot of things, but nothing happens. Please help. Thanks in advance for your help.

Get the angle and then constrain it with interpolation.

To get the angle for the camera, you get the forward vector and vector to target and then get
The inverse cosine of their dot product. The. It’s just a matter of setting the proper angle via interpolation,

And then applying the angle via a lookAtRotation or something to the cameras transform.

EDIT: what exactly are you asking???

I understand very little English, so I find it hard to understand what you’re trying to say and can not accurately explained to the fact that I need.
I am attaching a small scene, which I was able to do. There you can see clearly what I want to do. Need to rotate the mouse and press keys Q and E to rotate the Target object.
This works as expected, but incorrectly, the camera jerks.

1435152–76640–$limitangle.unitypackage (519 KB)

wrong

more please

sry, what I’ve posted there was wrong not your question :slight_smile:

use this as mouseorbit script, i marked the modifications. it works for me, hope it will also work for you:

var xMinText : TextMesh;
var xMaxText : TextMesh;
var CameraText : TextMesh;

var target : Transform;
var distance = 10.0;

var xSpeed = 5.0;
var ySpeed = 2.4;

var yMinLimit = -20;
var yMaxLimit = 80;

var xMinLimit = -100;
var xMaxLimit = 100;

private var x = 0.0;
private var y = 0.0;
 			
private var clampminx;
private var clampmaxx;

@script AddComponentMenu("Camera-Control/Mouse Orbit")

function Start () {
    var angles = transform.eulerAngles;
    x = angles.y;
    y = angles.x;

	// Make the rigid body not change rotation
   	if (rigidbody)
		rigidbody.freezeRotation = true;
}

function LateUpdate () {
    if (target) {
        x += Input.GetAxis("Mouse X") * xSpeed;
        y -= Input.GetAxis("Mouse Y") * ySpeed;
 		
 			clampminx = target.eulerAngles.y + xMinLimit; //clamp angle translated to world
 			clampmaxx = target.eulerAngles.y + xMaxLimit;
 			
 			var tempx=ClampAngle(x, clampminx, clampmaxx); // test clamped angle
 			
 			if(x>tempx+100)x-=360; //if the difference is too big
 			else if(x<tempx-100)x+=360; //or too small, it means you have jumped a 360 degrees limit so inc or dec x with 360
 			
 			x = ClampAngle(x, clampminx, clampmaxx); //clamp the new x
 			 			
 		y = ClampAngle(y, yMinLimit, yMaxLimit);

	    //Debug
		CameraText.text = x.ToString("f2");
		Debug.DrawRay(target.position + Vector3(0,10,0), Quaternion.AngleAxis(xMinLimit + target.eulerAngles.y, target.up) * Vector3(0,0,-10), Color.red);
		Debug.DrawRay(target.position + Vector3(0,10,0), Quaternion.AngleAxis(xMaxLimit + target.eulerAngles.y, target.up) * Vector3(0,0,-10), Color.red);
		//Debug
		
        var rotation = Quaternion.Euler(y, x, 0);
        var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
        
        transform.rotation = rotation;
        transform.position = position;
        
        //Rotate target debug
        if(Input.GetKey(KeyCode.Q))
        	target.Rotate(0, -1, 0);
        if(Input.GetKey(KeyCode.E))
        	target.Rotate(0, 1, 0);
        //Rotate target debug
    }
}

static function ClampAngle (angle : float, min : float, max : float) {
	/*if (angle < -360) I've removed this as it would do double as I've done above and would make the camera jumping
		angle += 360;
	if (angle > 360)
		angle -= 360;*/
	return Mathf.Clamp (angle, min, max);
}

Thank you so much, killed on that week to come up with an algorithm, you just saved me! :slight_smile: