Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Rotate around my character

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

  1. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35
    Hi,
    I would like to rotate around my character with the mouse (horizontal and vertical axis).However, I'm not sure how can I do that.

    I can rotate my camera on itself, but what should I change to rotate around my hero.

    Code (CSharp):
    1. if (Input.GetMouseButton (0)) {
    2.          
    3.          
    4.             transform.position = Target.TransformPoint (new Vector3 (0f, 2f, -10f));
    5.             transform.Rotate (new Vector3 (Input.GetAxis ("Mouse Y") * 350, Input.GetAxis ("Mouse X") * 350, 0) * Time.deltaTime);
    6.          
    7.         }
     
    Last edited: Aug 4, 2015
  2. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    You could try putting a movement/rotation script onto the camera so that it uses the mouse for input & then have the camera always target the player.
     
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  4. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35
    Code (CSharp):
    1. transform.RotateAround(Target.position, Target.up, 90 * Time.deltaTime);
    However, my camera rotate on itselft and not around the target.
     
    Last edited: Aug 4, 2015
  5. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    You set the player as the target?
     
  6. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35
    If I remove :
    Code (CSharp):
    1.  transform.position = Target.TransformPoint (new Vector3 (0f, 2f, -10f));
    The RotateAround works, but If My character move, the camera doesn't follow the target.

    Basically, my first code works, but I would like the camera look at the character.
    If I use LookAt, the camera doesn't move at all.

    With RotateAround the camera doesn't keep the -10f distance.

    Is there a way with LookAt to look at the target, but be able to rotate the camera ?
     
  7. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Is the camera set as a child of the parent?
     
  8. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35
    Oh snap...

    I forgot to set the camera as child with the new code.
     
  9. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35
    Ted,
    do you know a way to modify the rotation the same way that the positon.

    Code (CSharp):
    1. Origin = Target.TransformPoint (new Vector3 (0f, 2f, -10f));
     
  10. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
  11. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    You need something like this:

    Code (CSharp):
    1.  
    2. transform.rotation = Quaternion.LookRotation(target.transform.position - transform.position);
    3.  
    I cant test this right now too see if the positions are the right way around, but this should make your camera look at the target.
     
  12. TheDeathKnight

    TheDeathKnight

    Joined:
    Jul 21, 2015
    Posts:
    35
    Hey, another question.

    Do you know why for quaternion/rotation the x is the first value and for a position x is the second value ?


    Code (CSharp):
    1. OriginRot *= Quaternion.Euler (25f, 0f, 0f);
    2.  
    3.         Origin = Target.TransformPoint (new Vector3 (0f, 8f, -15f));
    X seems to be horizontal for position and vertical for rotation.