How do I make a camera follow an object in unity3d C#?

63,992

Solution 1

If you just simply want to follow the target object align the position of the camera the way you want it and make the camera the child of the target object and the rest will do

Solution 2

Here're one script I found useful during my game development. I didn't create them so I give credit to wiki.unity3d.com for providing this amazing script.

Smooth Follow:

using UnityEngine;
using System.Collections;

        public class SmoothFollow2 : MonoBehaviour {
        public Transform target;
        public float distance = 3.0f;
        public float height = 3.0f;
        public float damping = 5.0f;
        public bool smoothRotation = true;
        public bool followBehind = true;
        public float rotationDamping = 10.0f;

        void Update () {
               Vector3 wantedPosition;
               if(followBehind)
                       wantedPosition = target.TransformPoint(0, height, -distance);
               else
                       wantedPosition = target.TransformPoint(0, height, distance);

               transform.position = Vector3.Lerp (transform.position, wantedPosition, Time.deltaTime * damping);

               if (smoothRotation) {
                       Quaternion wantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
                       transform.rotation = Quaternion.Slerp (transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
               }
               else transform.LookAt (target, target.up);
         }
}

More information about my work

Solution 3

Include Standard Mobile Asset to your project. It contains a SmoothFollow2D.js code in its script section. Attach this code with the gameobject and initialize public variables. This will simply do the job for you.

Solution 4

I found this simple and useful unity 2d camera follow script.

using UnityEngine;
using System.Collections;

public class FollowCamera : MonoBehaviour {

public float interpVelocity;
public float minDistance;
public float followDistance;
public GameObject target;
public Vector3 offset;
Vector3 targetPos;
// Use this for initialization
void Start () {
    targetPos = transform.position;
}

// Update is called once per frame
void FixedUpdate () {
    if (target)
    {
        Vector3 posNoZ = transform.position;
        posNoZ.z = target.transform.position.z;

        Vector3 targetDirection = (target.transform.position - posNoZ);

        interpVelocity = targetDirection.magnitude * 5f;

        targetPos = transform.position + (targetDirection.normalized * interpVelocity * Time.deltaTime); 

        transform.position = Vector3.Lerp( transform.position, targetPos + offset, 0.25f);

    }
   }
}

source unity2d camera follow script

Share:
63,992
chesnutcase
Author by

chesnutcase

A really sad person who spends too much time on the computer. Save me. I am on StackOverflow and JSE.

Updated on September 29, 2020

Comments

  • chesnutcase
    chesnutcase over 3 years

    I have an object called Ball, and I added keyboard interactivity to it(WASD to move the ball) I need the camera to stay behind and follow the ball, but I am getting errors.

    using UnityEngine;
    using System.Collections;
    public class ballmain : MonoBehaviour {
        public bool isMoving = false;
        public string direction;
        public float camX;
        public float camY;
        public float camZ;
        // Use this for initialization
        void Start () {
            Debug.Log("Can this run!!!");
        }
    
        // Update is called once per frame
        void Update () {
            camX = rigidbody.transform.position.x -=10;
            camY = rigidbody.transform.position.y -=10;
            camZ = rigidbody.transform.position.z;
            camera.transform.position = new Vector3(camX, camY, camZ);
                //followed by code that makes ball move
        }
    }
    

    I get error "Assets/ballmain.cs(18,44): error CS1612: Cannot modify a value type return value of 'UnityEngine.Transform.position'. Consider storing the value in a temporary variable" Does anyone know the answer? If I comment out the code about the camera the ball can move around.