Making camera follow an object

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?

Just put this script on the camera and put the player into the myPlay variable.

var myPos : Vector3;
var myPlay : Transform;

function Update()
{
   transform.position = myPlay.position + myPos;
}

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).

1 Like

There is a SmoothFollow script in the standart assets. You can use/edit that.

2 Likes

Thank you so much, it worked for me too.

worked, thanks!

1 Like

It says Code(csharp) but I assmume it’s javascript right?

You do not declare functions like this in C#.

C# version would be:

public Vector3 myPos;
public Transform myPlay;

public void Update()
{
   transform.position = myPlay.position + myPos;
}
5 Likes

awesome!!

It doesnt work…

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):

  • using System.Collections;
  • using System.Collections.Generic;
  • using UnityEngine;
    • public class NAMEOFSCRIPT : MonoBehaviour
  • {
  • public Transform cam;
  • public Transform player;
  • public float CamDistance;
    • // Start is called before the first frame update
  • void Start()
  • {
  • }
    • // Update is called once per frame
  • void Update()
  • {
  • cam.position = player.position + new Vector3(CamDistance, 5, 0);
  • }
  • }
2 Likes

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?

I would use Cinemachine. They made for this and more.

Thanks!