Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Making camera follow an object

Discussion in 'Scripting' started by StarBright, Oct 26, 2009.

  1. StarBright

    StarBright

    Joined:
    Oct 14, 2009
    Posts:
    34
    I want to make the camera follow the player avatar around the screen. I know I can do this by making it a child of the avatar, but I don't want to do it this way because if I do, the camera will also rotate when the avatar rotates, and since the avatar will be doing a lot of sudden 90-degree turns, this is very disorienting for the player. How do I tell the camera to keep the avatar in the center of the screen, but not rotate when the avatar rotates?
     
  2. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,572
    Just put this script on the camera and put the player into the myPlay variable.

    Code (csharp):
    1. var myPos : Vector3;
    2. var myPlay : Transform;
    3.  
    4. function Update()
    5. {
    6.    transform.position = myPlay.position + myPos;
    7. }
    It'll make sure the camera is always displaced the exact same distance and direction from the avatar, which will allow you to move it around and rotate without any problems. If you want the camera -3 Z away, then myPos would be (0,0,-3).
     
    Lashes likes this.
  3. SarperS

    SarperS

    Joined:
    Mar 10, 2009
    Posts:
    824
    There is a SmoothFollow script in the standart assets. You can use/edit that.
     
    meanssebastian and jatinmb94 like this.
  4. wegaz

    wegaz

    Joined:
    May 19, 2014
    Posts:
    5
    Thank you so much, it worked for me too.
     
  5. Stiefo.o

    Stiefo.o

    Joined:
    Jul 11, 2013
    Posts:
    14
    worked, thanks!
     
    mfaxyzofficial likes this.
  6. supertimeal

    supertimeal

    Joined:
    Jul 24, 2016
    Posts:
    1
    It says Code(csharp) but I assmume it's javascript right?
     
  7. xjjon

    xjjon

    Joined:
    Apr 15, 2016
    Posts:
    630
    You do not declare functions like this in C#.
     
  8. GargerathSunman

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,572
    C# version would be:

    Code (csharp):
    1. public Vector3 myPos;
    2. public Transform myPlay;
    3.  
    4. public void Update()
    5. {
    6.    transform.position = myPlay.position + myPos;
    7. }
     
    AntiBeta, Lashes, KrisDev3 and 2 others like this.
  9. mjs101389

    mjs101389

    Joined:
    Mar 13, 2018
    Posts:
    1
    awesome!!
     
  10. AssassinBear23

    AssassinBear23

    Joined:
    May 5, 2019
    Posts:
    2
    It doesnt work...
     
  11. LITCoderKingYT_unity

    LITCoderKingYT_unity

    Joined:
    Sep 24, 2019
    Posts:
    1
    This Works I Coded it By Myself. You want to put your camera game object in the transform box in the script along with the player (The object you want to track) object
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;

    4. public class NAMEOFSCRIPT : MonoBehaviour
    5. {
    6. public Transform cam;
    7. public Transform player;
    8. public float CamDistance;

    9. // Start is called before the first frame update
    10. void Start()
    11. {
    12. }

    13. // Update is called once per frame
    14. void Update()
    15. {
    16. cam.position = player.position + new Vector3(CamDistance, 5, 0);
    17. }
    18. }
     
    Nateeeeeeeeeee likes this.
  12. AvagadroToast

    AvagadroToast

    Joined:
    Jun 22, 2020
    Posts:
    1
    when i use the smoothfollow it stretches the screen. I think its because of a newer version of unity. do you know of a newer smooth follow?
     
  13. I would use Cinemachine. They made for this and more.
     
  14. McPeppergames

    McPeppergames

    Joined:
    Feb 15, 2019
    Posts:
    104