Search Unity

  1. Unity 2018.3 is now released.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We've updated our Terms of Service. Please read our blog post from Unity CTO and Co-Founder Joachim Ante here
    Dismiss Notice
  4. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  5. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

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).
     
  3. Sarper-Soher

    Sarper-Soher

    Joined:
    Mar 10, 2009
    Posts:
    799
    There is a SmoothFollow script in the standart assets. You can use/edit that.
     
    jatinmb94 likes 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!
     
  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:
    159
    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. }
     
  9. mjs101389

    mjs101389

    Joined:
    Mar 13, 2018
    Posts:
    1
    awesome!!