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. Dismiss Notice

Rotating a Camera Around An Object

Discussion in 'Scripting' started by robgw84, Jun 20, 2022.

  1. robgw84

    robgw84

    Joined:
    Mar 22, 2015
    Posts:
    9
    I have a camera that is supposed to follow the player, I can make it sit at a specific spot behind the player, but I want to be able to make it face the player (like a rear view mirror) and have it turn when the player does. I can make it face the player, but when I try to make the player turn with the mouse, the player rotates but the camera tries to rotate in place.

    How do I make the camera rotate around the player to stay in front of them?

    This is the code I am currently using:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class Camera_Follow : MonoBehaviour
    4. {
    5.     public Transform camTarget;
    6.     public float pLerp = 0.02f;
    7.     public float rLerp = 0.01f;
    8.     public Vector3 offset;
    9.     void LateUpdate ()
    10.     {
    11.         transform.position = Vector3.Lerp (transform.position, camTarget.position + offset, pLerp);
    12.         transform.rotation = Quaternion.Lerp(transform.rotation, camTarget.rotation, rLerp);
    13.     }
    14. }
     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
  3. robgw84

    robgw84

    Joined:
    Mar 22, 2015
    Posts:
    9
    That seems to spin it around constantly, I want it to turn with the player so its always in front. I can make it stay behind and turn when the player does that way, but cant figure out how to change that so its in front.
     
  4. TheDevloper

    TheDevloper

    Joined:
    Apr 30, 2019
    Posts:
    69
    you better use cinemachine from package manager, and use the free camera.
     
  5. itisMarcii_

    itisMarcii_

    Joined:
    Apr 5, 2022
    Posts:
    107
    Use RotateAround like suggested and LookAt for the camera rotation.

    If the camera shall look away from your character, negate the result.
     
  6. robgw84

    robgw84

    Joined:
    Mar 22, 2015
    Posts:
    9
    But RotateAround circles the player, even if I use LookAt that would just make it orbit the player while looking at them. I need to have the camera fixed in place where I want it to be.