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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Camera following player

Discussion in 'Scripting' started by Tornado77, Nov 14, 2018.

  1. Tornado77

    Tornado77

    Joined:
    Nov 14, 2018
    Posts:
    83
    Hello, i'm a total begineer with programation and i want to make that the camera is following player so i use a script that i found in a tutorial, this one. " https://unity3d.com/fr/learn/tutorials/projects/2d-ufo-tutorial/following-player-camera " The problem is that when i rotate the camera is not rotating. I added this line
            transform.rotation = player.transform.rotation;
    but the move are realy weird. upload_2018-11-14_13-15-10.png
    I tried
    transform.rotation = player.transform.rotation + offset;
    but its saying "cannot apply operator + to operands of type quaternion and vector3", someone can help me please? sorry if my english is bad im french and here is my script

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CompleteCameraController : MonoBehaviour
    5. {
    6.  
    7.     public GameObject player;       //Public variable to store a reference to the player game object
    8.  
    9.  
    10.     private Vector3 offset;         //Private variable to store the offset distance between the player and camera
    11.  
    12.     // Use this for initialization
    13.     void Start()
    14.     {
    15.         //Calculate and store the offset value by getting the distance between the player's position and camera's position.
    16.         offset = transform.position - player.transform.position;
    17.     }
    18.  
    19.     // LateUpdate is called after Update each frame
    20.     void LateUpdate()
    21.     {
    22.         // Set the position of the camera's transform to be the same as the player's, but offset by the calculated offset distance.
    23.         transform.position = player.transform.position + offset;
    24.         transform.rotation = player.transform.rotation;
    25.     }
    26. }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If you just want the camera to stay a fixed distance from the player and always point in a direction where the player would be in view, you can just make the camera a child of the player.

    As for your script instead, if you want it to be a 3rd person camera behind your player, you're probably going to want to use the player's local position rather than global, then convert the player's local position + offset to world space position, so you can keep the camera positioned behind the player as the player turns. Your rotation code looks fine to me if the player stays on a flat plane, but I'm a fan of just placing an object above the player and having the camera look at that object.
     
  3. Reeii

    Reeii

    Joined:
    Feb 5, 2016
    Posts:
    91
    The rotation is probably choppy? Using Vector3.SmoothDamp() for example would solve this, but I explain a much much easier way at the end.
    As it's said, transform.rotation has the type Quaternion and offset the type Vector3. What you want to use instead is transform.eulerAngles, which is the X, Y, Z representation of the rotation like in the editor.
    transform.eulerAngles = player.transform.eulerAngles + offset;


    But as the guy who was a little bit faster than me ( :eek: ) said, the easiest way is simply making the camera a child of the player and then adjusting the offset in the editor by changing the camera's position (which is then the camera's local position relative to the player).
     
  4. Tornado77

    Tornado77

    Joined:
    Nov 14, 2018
    Posts:
    83
    Ok thank you guys its working :)