- Home /
Limit Y Axis transform.RotateAround
Hello,
I have a camera that I want to rotate around a point (0,0,0) in all directions, but I want to put a clamp on it so that it can't go too far above or below the point. I have seen this question answered for the left and right directions before but never for the vertical one.
I have tried converting the code from these two questions (that basically say the same thing) to work in the vertical direction, but it bugs out at some points along the rotation, and I can't figure out why.
First Question, Second Question
And this is how I tried to convert it:
//how much we want to rotate by this frame
float rotX = Input.GetAxis("Mouse X") * rotSpeed;
float rotY = Input.GetAxis("Mouse Y") * rotSpeed; //(before clamping)
//find current direction
Vector3 currentDirection = transform.position - Vector3.zero;
//find current angle between basis for clamp & where we are now
float angle = Vector3.Angle(Vector3.forward, currentDirection);
//finds out if it's up or down
if (Vector3.Cross(Vector3.forward, currentDirection).x < 0) angle = -angle;
//find out how much you can move without violating limits
float newAngle = Mathf.Clamp(angle + rotY, yMinLimit, yMaxLimit);
//grabs how much you are allowed to move the angle from the current angle
rotY = newAngle - angle;
//spinning the garden
transform.RotateAround(Vector3.zero, Vector3.up, rotX);
transform.RotateAround(Vector3.zero, transform.TransformDirection(Vector3.right), -rotY); //vertical rotation
If anyone knows of the correct way to make this work for the Y axis, or a different way to clamp the vertical rotation, I would be super excited to hear it! Ty!
Edit: Seems to be other interest in this question here: http://answers.unity3d.com/questions/627515/clamp-camera-angle-not-working.html
Answer by Beks_Omega · Jul 06, 2017 at 08:33 PM
A very lovely person a StackOverflow gave me the answer to this question! For anyone finding this in the future here ya go: https://stackoverflow.com/questions/44837086/how-to-limit-clamp-y-axis-rotation-for-transform-rotatearound-unity
And here are the parts of it I used: (because he adds in some stuff about changing distances and stuff)
void Start()
{
//vector from where I am to what I'm rotating around
initialVector = transform.position - Vector3.zero;
//current angles of camera (make sure it's looking at what you want
//to rotate around)
rotYAxis = transform.eulerAngles.y;
rotXAxis = transform.eulerAngles.x;
//distance between me and what I'm rotating around
distance = Vector3.Magnitude(initialVector);
}
//You can probably call this in update I just call it from a dif script
public void Orbit()
{
//get your inputs
rotYAxis += Input.GetAxis("Mouse X") * thirdRotSpeed;
rotXAxis -= Input.GetAxis("Mouse Y") * thirdRotSpeed;
//clamp the angle
rotXAxis = ClampAngle(rotXAxis, thirdMin, thirdMax);
// convert it to quaternions
Quaternion toRotation = Quaternion.Euler(rotXAxis, rotYAxis, 0);
Quaternion rotation = toRotation;
//figure out what your distance should be (so that it's rotating around
//not just rotating)
Vector3 negDistance = new Vector3(0, 0, -distance);
Vector3 position = rotation * negDistance + Vector3.zero;
//and apply!
transform.rotation = rotation;
transform.position = position;
}
//clamp angle from before
public static float ClampAngle(float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp(angle, min, max);
}
Thanks so much again to Fenixrw on StackOverflow for the help!
Bro what a life saver! Was trying to rap my head around this but you came to the rescue. It was easier looking at ur code then the stuff you linked. To those wondering how to rotate around a point, I replaced his "Vector3.zero" with "Vector3.Up" in my case so it would effectively do the top of my character, this will likely be another value for you guys but I think that is where you put it.
Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220612124930im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Clamp Camera Angle not Working 2 Answers
Based on touch rotate camera object 0 Answers
How can I limit my rotation around an object ? 1 Answer
Set max allowed angle? 1 Answer
Clamping position of camera when using RotateAround? 0 Answers