Search Unity

Camera rotate with player

Discussion in 'Scripting' started by TheJohnRudy, Oct 14, 2019.

  1. TheJohnRudy

    TheJohnRudy

    Joined:
    Oct 14, 2019
    Posts:
    9
    Hello!

    I'm new to coding and unity in general and I'm trying to create a tank controlled character that has a free look mouse.

    I've followed Filmstorms tutorial on creating a 3rd person setup with camera collision that is 75% what I want. I'd like to take the script a bit further and get the camera to turn with the character. Using the character as a pivot point and get the camera to look at the players forward most of the time.

    Original Code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraFollow : MonoBehaviour {
    6.  
    7.     public float CameraMoveSpeed = 120.0f;
    8.     public GameObject CameraFollowObj;
    9.     Vector3 FollowPOS;
    10.     public float clampAngle = 80.0f;
    11.     public float inputSensitivity = 150.0f;
    12.     public GameObject CameraObj;
    13.     public GameObject PlayerObj;
    14.     public float camDistanceXToPlayer;
    15.     public float camDistanceYToPlayer;
    16.     public float camDistanceZToPlayer;
    17.     public float mouseX;
    18.     public float mouseY;
    19.     public float finalInputX;
    20.     public float finalInputZ;
    21.     public float smoothX;
    22.     public float smoothY;
    23.     private float rotY = 0.0f;
    24.     private float rotX = 0.0f;
    25.  
    26.  
    27.  
    28.  
    29.     void Start () {
    30.         Vector3 rot = transform.localRotation.eulerAngles;
    31.         rotY = rot.y;
    32.         rotX = rot.x;
    33.  
    34.         Cursor.lockState = CursorLockMode.Locked;
    35.         Cursor.visible = false;
    36.  
    37.     }
    38.    
    39.     void Update () {
    40.         float inputX = Input.GetAxis("RightStickHorizontal");
    41.         float inputY = Input.GetAxis("RightStickVertical");
    42.  
    43.         mouseX = Input.GetAxis("Mouse X");
    44.         mouseY = Input.GetAxis("Mouse Y");
    45.  
    46.         finalInputX = inputX + mouseX;
    47.         finalInputZ = inputY + mouseY;
    48.  
    49.         rotY += finalInputX * inputSensitivity * Time.deltaTime;
    50.         rotX += finalInputZ * inputSensitivity * Time.deltaTime;
    51.  
    52.         rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);
    53.         //rotY = Mathf.Clamp(rotY, -clampAngle, clampAngle); Commented out until rotation is achieved.
    54.  
    55.         Quaternion localRotation = Quaternion.Euler(rotX, rotY, 0.0f);
    56.         transform.rotation = localRotation;
    57.  
    58.     }
    59.  
    60.     private void LateUpdate()
    61.     {
    62.         CameraUpdater();
    63.  
    64.     }
    65.  
    66.     void CameraUpdater()
    67.     {
    68.         //set target object to follow
    69.         Transform target = CameraFollowObj.transform;
    70.  
    71.         //move towards the game object that is the target
    72.         float step = CameraMoveSpeed * Time.deltaTime;
    73.         transform.position = Vector3.MoveTowards(transform.position, target.position, step);
    74.     }
    75. }
    76.  
    All that I have tried have cause the camera to rotate around itself with the character and/or orbit around the character, but keeping the original view direction and not the rotated characters view.

    If I'm not mistaken I need to add something or change how CameraUpdater() handles the movement. It all ready follows the lateral movement but not the horizontal rotation (vertical pivot around the character).

    Thank you in advance.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    On line 55, you should be able to just do this:
    Code (csharp):
    1.  
    2.         Quaternion localRotation = CameraFollowObj.transform.rotation * Quaternion.Euler(rotX, rotY, 0.0f);
    3.  
     
    TheJohnRudy likes this.
  3. TheJohnRudy

    TheJohnRudy

    Joined:
    Oct 14, 2019
    Posts:
    9
    Well hot diggidy that did the trick. Thank you very much!
     
  4. TheJohnRudy

    TheJohnRudy

    Joined:
    Oct 14, 2019
    Posts:
    9
    Ok the suggestion worked to change line 55. But the Camera keeps spawning in front of the player character now. I want to un-comment the line 53 clamp on the Y axis to limit the movement. Any suggestions to fix that?

    What I've tried so far is to try and mirror the camera follow object positions and rotations but it makes it so that the camera freaks out and spins out of control.

    Sorry for this late reply.

    EDIT:

    Ok Seems that I was just impatient and/or an idiot for not trying to just move the cameras base behind the player to start with. All is good now. Thank you for again for the fix!
     
    Last edited: Oct 15, 2019