Search Unity

How to make camera rotate around player as he moves?

Discussion in 'Scripting' started by smashingdashing, Apr 20, 2019.

  1. smashingdashing

    smashingdashing

    Joined:
    Apr 18, 2016
    Posts:
    33
    I need some help people, any would be appreciated! Currently I've got a Diablo style RPG project, how ever I want the camera to rotate around the player whilst he's moving instead of staying static the whole time. I've looked through guides but nothing I've found has solved my issue.

    Here's the script, if anyone knows how to help then please comment or PM me :)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ClickToMove : MonoBehaviour
    5. {
    6.  
    7.     public float speed;
    8.     public CharacterController controller;
    9.     private Vector3 position;
    10.  
    11.     public AnimationClip run;
    12.     public AnimationClip idle;
    13.  
    14.     public static bool attack;
    15.     public static bool die;
    16.  
    17.  
    18.  
    19.  
    20.  
    21.     // Use this for initialization
    22.     void Start ()
    23.     {
    24.         position = transform.position;
    25.     }
    26.  
    27.     // Update is called once per frame
    28.     void Update ()
    29.     {
    30.  
    31.  
    32.         if (!attack&&!die)
    33.         {
    34.                         if (Input.GetMouseButton (0))
    35.                         {
    36.                                 //Locate where the player clicked on the terrain
    37.                                 locatePosition ();
    38.  
    39.                         }
    40.  
    41.                         //Move player to position
    42.                         moveToPosition ();
    43.                 }
    44.                 else
    45.                 {
    46.                  
    47.                 }
    48.     }
    49.  
    50.     void locatePosition()
    51.     {
    52.         RaycastHit hit;
    53.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    54.  
    55.         if(Physics.Raycast(ray, out hit, 1000))
    56.         {
    57.         if(hit.collider.tag!="Player"&&hit.collider.tag!="Enemy")
    58.         {
    59.             position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
    60.         }
    61.      
    62.         }
    63.     }
    64.  
    65.     void moveToPosition()
    66.     {
    67.         if (Vector3.Distance (transform.position, position) > 1) {
    68.                         Quaternion newRotation = Quaternion.LookRotation (position - transform.position, Vector3.forward);
    69.  
    70.                         newRotation.x = 0f;
    71.                         newRotation.z = 0f;
    72.  
    73.                         transform.rotation = Quaternion.Slerp (transform.rotation, newRotation, Time.deltaTime * 10);
    74.                         controller.SimpleMove (transform.forward * speed);
    75.  
    76.             GetComponent<Animation>().CrossFade (run.name);
    77.                 }
    78.         //Game Object not moving
    79.         else
    80.         {
    81.             GetComponent<Animation>().CrossFade (idle.name);
    82.         }
    83.     }
    84.  
    85.  
    86.  
    87.  
    88.  
    89. }


    As you can see the script allows me to attack, die and click on the screen to move - surely there can be some line of code or something I can alter to have the camera rotate the player?

    PS happy easter :D
     
  2. smashingdashing

    smashingdashing

    Joined:
    Apr 18, 2016
    Posts:
    33
    EDIT - Figured it out, if anyone wants to know what I done PM me :)
     
  3. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,433
    It's better for everyone if you can just say in the thread what your solution was, even if just a summary. People may find this topic years in the future, and maybe you won't be able to respond to their PMs.

    Short answer would be to make the camera a child of the character, positioned where you want it to be relative to the character. Long answer would be to look into camera controllers which can get pretty fancy, such as Cinemachine. These typically move the camera toward invisible target location objects, one of which can be a child of the character.
     
    eses and smashingdashing like this.
  4. smashingdashing

    smashingdashing

    Joined:
    Apr 18, 2016
    Posts:
    33
    good point, I didn't do it how you suggested - but I completely reworked the script that makes the camera follow the player. I didn't need to mess with the Click To Move that I posted, but here's how I managed to solve my issue:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraFollow : MonoBehaviour
    6.  
    7. {
    8.  
    9.    public Transform target;
    10.  
    11.    public float height = 7;
    12.  
    13.  
    14.    public float radius = 6;
    15.  
    16.    public float angle = 0;
    17.    public float rotationalSpeed = 3;
    18.  
    19.     // Use this for initialization
    20.     void Start ()
    21.     {
    22.        
    23.     }
    24.    
    25.     // Update is called once per frame
    26.     void Update ()
    27.     {
    28.  
    29.  
    30.  
    31.         float cameraX = target.position.x + (radius * Mathf.Cos(angle));
    32.         float cameraY = target.position.y + height;
    33.         float cameraZ = target.position.z + (radius * Mathf.Sin(angle));
    34.  
    35.         transform.position = new Vector3(cameraX, cameraY, cameraZ);
    36.  
    37.         if(Input.GetKey(KeyCode.A))
    38.         {
    39.           angle = angle - rotationalSpeed * Time.deltaTime;
    40.         }
    41.         else if (Input.GetKey(KeyCode.D))
    42.         {
    43.           angle = angle + rotationalSpeed * Time.deltaTime;
    44.         }
    45.  
    46.         transform.LookAt(target.position);
    47.  
    48.     }
    49. }
    50.  

    It allows me to rotate the camera by pressing A & D, change the rotational speed & the position of the camera :)
     
  5. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,433
    Great! Thanks for sharing your solution.
     
    smashingdashing likes this.