Search Unity

Problem with camera rotation and movement

Discussion in 'Getting Started' started by papagiorgio, Jan 21, 2020.

  1. papagiorgio

    papagiorgio

    Joined:
    Mar 24, 2016
    Posts:
    1
    Hello,
    The part where camera follows object (moving object) is working fine, but it's not rotating properly - seems like its rotating around itself.
    I have tried separating camera rotation script and movement script, and rotation script is working like I wanted but only with movement script OFF. When ON cam rotating around itself.

    I could probably download similar script, and learn nothing. Spend 2h on Google and didn't help.
    I have feeling I missing something, but can't figure it out. I'm just starting my adventure with Unity.

    Code (CSharp):
    1. public class CameraFollowObject : MonoBehaviour
    2. {
    3.     public Transform targetTransform;
    4.     private Vector3 cameraOffset;
    5.     float rotationSpeed = 1.0f;
    6.  
    7.     [Range(0.01f, 1f)]
    8.     public float smoothFactor = 0.5f;
    9.  
    10.     [Range(5f, 20f)]
    11.     public float cameraAltitude = 10.0f;
    12.  
    13.    
    14.     void Start()
    15.     {
    16.         cameraOffset = transform.position - targetTransform.position;   //vector camera -> target
    17.         transform.LookAt(targetTransform);                              //aim camera to target
    18.     }
    19.  
    20.    
    21.     void LateUpdate()
    22.     {
    23.         Vector3 newPosition = targetTransform.position + cameraOffset;  //update camera->target vector
    24.         newPosition.y = cameraAltitude;                                 //fix camera altitude
    25.  
    26.         transform.position = Vector3.Slerp(transform.position, newPosition, smoothFactor); //follow player - gently
    27.  
    28.         RotateCamera();
    29.     }
    30.  
    31.  
    32.  
    33.     void RotateCamera()
    34.     {
    35.         if (Input.GetKey(KeyCode.E))
    36.         {
    37.             transform.RotateAround(targetTransform.position, Vector3.up, rotationSpeed);
    38.         }
    39.  
    40.         if (Input.GetKey(KeyCode.Q))
    41.         {
    42.             transform.RotateAround(targetTransform.position, Vector3.up, -rotationSpeed);
    43.         }
    44.     }
    45.  
    46. }
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    You can't deal with these separately. Right now you have RotateCamera setting the transform position (via transform.RotateAround), and you also have LateUpdate setting the transform position (by abusing Slerp). You're telling your object to be in two places at once.

    Instead you should have properties that keep track of (1) the desired altitude, and (2) the desired angle in the XZ plane. Adjust these based on key inputs. Then in LateUpdate, you must calculate the correct position using both of these bits of information at once.
     
    papagiorgio likes this.