Camera Follow Target(Smooth Follow)

Can someone make me a very simple camera follow target script? Or at least link me to where i can make my own. I tried using smooth follow but i dont like the rotation of it and dont know how to change it.

Noob.

dont have time to make a exemple right now, but the docs do inlcuded a basic version of what you want.

I found some
http://lmgtfy.com/?q=unity+camera+follow+tutorial

I appreciate your humor but none of those youtube videos helped. They all used smoothfollow. And some were for 2d games.

Noob.

I will try what you gave and report back.

Noob.

Yea sure Google entry NR4 Unity3D: Third-Person Cameras | Envato Tuts+
Not for 2D and not using the unity smoothfollow script. 5 Seconds of search…
Good luk with your try

Alright thanks. I will report back.

Noob.

//SmoothFollow.cs
//C#
using UnityEngine;
using System.Collections;

public class SmoothFollow : MonoBehaviour
{
    public Transform target;        //GameObject that we should follow
    public float distance;            //how far back?
    public float maxDriftRange;        //how far are we allowed to drift from the target position
    public float angleX;            //angle to pitch up on top of the target
    public float angleY;            //angle to yaw around the target
   
    private Transform m_transform_cache;    //cache for our transform component
    private Transform myTransform
    {//use this instead of transform
        get
        {//myTransform is guarunteed to return our transform component, but faster than just transform alone
            if (m_transform_cache == null)
            {//if we don't have it cached, cache it
                m_transform_cache = transform;
            }
            return m_transform_cache;
        }
    }
   
    //this runs when values are changed in the inspector
    void OnValidate()
    {
        if (target != null)
        {//we have a target, move the camera to target position for preview purposes
            Vector3 targetPos = GetTargetPos();
            //update position
            myTransform.position = targetPos;
            //look at our target
            myTransform.LookAt(target);
        }
    }
   
    //this runs every frame, directly after Update
    void LateUpdate()
    {//use this so that changes are immediate after the object has been affected
   
        Vector3 targetPos = GetTargetPos();
        //calculate drift theta
        float t = Vector3.Distance(myTransform.position, targetPos) / maxDriftRange;
       
        //smooth camera position using drift theta
        myTransform.position = Vector3.Lerp(myTransform.position, targetPos, t * Time.deltaTime);
        //look at our target
        myTransform.LookAt(target);
    }
   
    void OnDrawGizmos()
    {//this is for editor purpose only, shows the relationship between this script and target as a line
        Gizmos.color = Color.yellow;
        Gizmos.DrawLine(myTransform.position, target.position);
    }
   
    private Vector3 GetTargetPos()
    {//returns where the camera should aim to be
        //opposite of (-forward) * distance
        Vector3 targetPos = new Vector3(0, 0, -distance);
        //calculate pitch and yaw
        targetPos = Quaternion.Euler(angleX, angleY, 0) * targetPos;
        //return angled target position relative to target.position
        return target.position + (target.rotation * targetPos);
    }
}

This script is smoother than some things I can think of! lol >.>

2 Likes

Thanks will report back

Noob.