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

Rotate Camera around moving target (Player)

Discussion in 'Scripting' started by BlueBlane, Oct 24, 2015.

  1. BlueBlane

    BlueBlane

    Joined:
    Sep 28, 2015
    Posts:
    70
    I've got a camera and player movement script set up so that when you hold down Q or E, the camera rotates around the player left or right respectively. This works when the player is standing still (however, the position of the camera is reset to be the original offset of the player. Also, the camera does not move when the player moves AND Q or E are held down (If they are not held down, then the camera will move with the player)

    So, how can my camera script be modified so that when I hold Q or E, the camera will rotate around the player and still follow the player when moving? Also, how would I go about setting the camera's position after Q and E are let go, since as it is now it will just be reset to the original offset. Just as a note "HorizontalRotation" is an Input configuration for Q and E. Q being negative, E being positive.

    Camera Script:

    Code (csharp):
    1.  
    2. public class CameraMain : MonoBehaviour
    3. {
    4.     public float zOffset = 12;
    5.     public float yOffset = 9;
    6.     public Transform player;
    7.     Vector3 pos;
    8.  
    9.     void LateUpdate ()
    10.     {
    11.         if (Input.GetAxis ("HorizontalRotation") == 0)
    12.         {
    13.             pos = player.localPosition;
    14.             pos.z -= zOffset;  
    15.             pos.y += yOffset;
    16.             transform.position = pos;
    17.         }
    18.         else
    19.         {
    20.             transform.RotateAround (player.localPosition, player.up, Input.GetAxis ("HorizontalRotation"));
    21.             pos = transform.position;
    22.         }
    23.     }
    24. }
    25.  
    26.  

    Player Movement Script:

    Code (csharp):
    1.  
    2. public class Player : MonoBehaviour
    3. {
    4.  
    5. public float moveSpeed;
    6. public float rotationSpeed;
    7. Vector3 previousLocation;
    8. Vector3 moveDirection;
    9.  
    10.     void Update ()
    11.     {
    12.      
    13.         moveDirection = Vector3.zero;
    14.         previousLocation = transform.position;
    15.      
    16.         if(Input.GetKey(KeyCode.W))
    17.         {
    18.             moveDirection.z = 1;
    19.         }
    20.         if(Input.GetKey(KeyCode.S))
    21.         {
    22.             moveDirection.z = -1;
    23.         }
    24.         if(Input.GetKey(KeyCode.A))
    25.         {
    26.             moveDirection.x = -1;
    27.         }
    28.         if(Input.GetKey(KeyCode.D))
    29.         {
    30.             moveDirection.x = 1;
    31.         }
    32.      
    33.         transform.position = Vector3.Lerp(transform.position, transform.position + moveDirection.normalized, Time.fixedDeltaTime * moveSpeed);
    34.         if (moveDirection != Vector3.zero) transform.rotation = Quaternion.Lerp (transform.rotation, Quaternion.LookRotation(transform.position - previousLocation), Time.fixedDeltaTime * rotationSpeed);
    35.     }
    36. }
    37.  
     
  2. vintar

    vintar

    Joined:
    Sep 18, 2014
    Posts:
    90
    I would create an empty parent object and put the camera in it. Then in update, continuously position the parent at the player position + offset, and just rotate the camera in the parents local space.
     
  3. BlueBlane

    BlueBlane

    Joined:
    Sep 28, 2015
    Posts:
    70
    Thank you a whole bunch! If I could give you a cookie, I wouldn't. (I'd give you two!)

    Though one last thing that I'd like to change is to have the player move relative to the camera's position. Right now, if I rotate the camera left 90 degrees around the player, I'm looking at the side of the player, but pressing W moves me up in the world, but left from the camera's perspective.

    How would this be done given my player script in my OP?