Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Limit angles RotateAround

Discussion in 'Scripting' started by TheDeathKnight, Aug 5, 2015.

  1. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35
    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 :

    Code (CSharp):
    1. if (Input.GetMouseButton (0))
    2.         {
    3.  
    4.  
    5.             transform.RotateAround(Target.position, new Vector3 (Input.GetAxis ("Mouse Y"), Input.GetAxis ("Mouse X")), 300 * Time.deltaTime);
    6.    
    7.         }
    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.
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    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).
     
  3. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35
    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.


    Code (CSharp):
    1. if (Input.GetMouseButton (0)) {
    2.          
    3.          
    4.             transform.Rotate (new Vector3 (Input.GetAxis ("Mouse Y") * 350, Input.GetAxis ("Mouse X") * 350, 0) * Time.deltaTime);
    5.         }
    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.
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    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.
     
    Last edited: Aug 6, 2015
  5. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35
    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.


    Code (CSharp):
    1.         OriginRot = Target.rotation;
    2.         OriginRot *= Quaternion.Euler (25f, 0f, 0f);
    3.      
    4.         Origin = Target.TransformPoint (new Vector3 (0f, 8f, -15f));
    5.      
    6.         if (Input.GetMouseButton (0)) {
    7.          
    8.  
    9.  
    10. transform.Rotate (new Vector3 (Input.GetAxis ("Mouse Y") * 350, Input.GetAxis ("Mouse X") * 350, 0) * Time.deltaTime);
    11.         }
    12.         else
    13.         {
    14.             transform.position =  Vector3.Lerp (transform.position, Origin, Time.deltaTime * 5);
    15.          
    16.             transform.rotation = Quaternion.Slerp (transform.rotation, OriginRot, Time.deltaTime * 5);
    17.          
    18.          
    19.          
    20.         }
    21.      
    22.     }
    Im there for the moment, I tried 1000 things in input.getmousebutton. Im trying something new.
     
    Last edited: Aug 6, 2015
  6. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35
    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.
     
    Last edited: Aug 6, 2015
  7. Mark Kane

    Mark Kane

    Joined:
    Jul 24, 2015
    Posts:
    5
    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:

    Code (cs):
    1.  
    2.    float totalangle = 0;
    3.  
    4.    void Update () {
    5.      float PosHori = Input.GetAxis ("Horizontal");
    6.      totalangle += PosHori;
    7.      if (totalangle >= -90 && totalangle <= 90) {
    8.        transform.RotateAround (Vector3.zero, Vector3.up, PosHori);
    9.      } else {
    10.        totalangle -= PosHori;
    11.      }
    12.    }
    13.  
    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.
     
  8. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35
    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 ?
     
  9. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35