Limit angles RotateAround

Hey,

I’m wondering how can I limit the x axis. I tried the mathf.clamp, but it doesn’t work.

I’m trying to make a MouseOrbit with RotateAround like that :

if (Input.GetMouseButton (0))
        {


            transform.RotateAround(Target.position, new Vector3 (Input.GetAxis ("Mouse Y"), Input.GetAxis ("Mouse X")), 300 * Time.deltaTime);
   
        }

However it doesn’t work well. If I move my character on the right, the y rotation seems to be the Z axis.

In addition, I would like to rotate only on Y and X not in diagonal.

You need to describe more clearly what it is you’re attempting to accomplish.

As it is, we have a vague description, and example code that doesn’t work (and therefore doesn’t explain anything).

I think transform.rotate should be a better option than transform.RotateAround. However, I dont know how to rotate the camera around the character and not around itself.

if (Input.GetMouseButton (0)) {
          
          
            transform.Rotate (new Vector3 (Input.GetAxis ("Mouse Y") * 350, Input.GetAxis ("Mouse X") * 350, 0) * Time.deltaTime);
        }

This code makes the camera rotate around itself, but I would like to rotate around the character.

Basically, the camera should always face the character, but I cant figure out how.

You seem to be misunderstanding how RotateAround works. The first parameter is the point in space that you want to rotate around, the second is the AXIS of that point which you want to rotate around (this should generally be Vector3.up, the Y axis), the third is the number of degrees to rotate around that point (generally multiplied by Time.deltaTime for consistency). In other words, the final parameter (degrees) is where you determine the direction, as anything positive will rotate clockwise (I think?) and negative will rotate counter-clockwise. If you want to RotateAround using two different inputs (MouseX and MouseY) the simple way is to use two different RotateAround() functions back to back, one which rotates around the Y axis (Vector3.up with MouseX), and one which rotates around the Z axis (?) (Vector3.forward with MouseY). Experiment, you’ll figure it out.

If you wish to limit yourself to rotating only a certain number of degrees in either direction, clamp by using the relative direction of the camera from the target’s local rotation, like with transform.InverseTransformPoint() on the character using the camera’s position as a parameter.

I know who tranformaround works , but I should be better with rotate.

However, as I said, I dont know who to rotate around the target with rotate.

        OriginRot = Target.rotation;
        OriginRot *= Quaternion.Euler (25f, 0f, 0f);
      
        Origin = Target.TransformPoint (new Vector3 (0f, 8f, -15f));
      
        if (Input.GetMouseButton (0)) {
          


transform.Rotate (new Vector3 (Input.GetAxis ("Mouse Y") * 350, Input.GetAxis ("Mouse X") * 350, 0) * Time.deltaTime);
        }
        else
        {
            transform.position =  Vector3.Lerp (transform.position, Origin, Time.deltaTime * 5);
          
            transform.rotation = Quaternion.Slerp (transform.rotation, OriginRot, Time.deltaTime * 5);
          
          
          
        }
      
    }

Im there for the moment, I tried 1000 things in input.getmousebutton. Im trying something new.

Basically, I just want to rotate around my character with limited degrees and only up/down, right/left not in diagonal.

I get some bugs with rotatearound, thats the reason Im trying to use rotate.

With RotateAround is like if the camera doesnt rotate on itself over the character.

Mouse X rotate by the north even if my character is facing east or whatever, so the camera is behind the character and rotate on the left, on the Z axis.

Another exemple : If I type vector3.left, the camera will rotate on the X axis and if I rotate my character on the right, the camera will rotate on the Z axis.

Its pretty hard to explain by typing. But you should test my code to know what im talking about.

By the way, its not the only issue with RotateAround. I dont think making a mouse orbit with RotateAround works well.

Since I am just working on a camera script myself, I quickly checked this out. The code you may want to use could look like this:

   float totalangle = 0;

   void Update () {
     float PosHori = Input.GetAxis ("Horizontal");
     totalangle += PosHori;
     if (totalangle >= -90 && totalangle <= 90) {
       transform.RotateAround (Vector3.zero, Vector3.up, PosHori);
     } else {
       totalangle -= PosHori;
     }
   }

totalangle will increase or decrease as long as you hold the arrow buttons in this case. Subtrackting the PosHori value in case you have reached the limit is important, because without it the totalangle would grow further out of range, delaying movement in the opposite direction.

Hey Mark,

Thanks for the code, but as I said, I have some issues with RotateAround.

Ill try to give you an exemple.

Basically, if I dont move my character (the target I want to rotate around) and I try to ratate on the X axis with Mouse X thats works well. However, If I rotate my character on the left (90 degrees) then I try to rotate my camera with Mouse X, the camera will now rotate on the Z axis and not over the targets head.

Do you know what I mean ?

Up