Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Camera Follow Target(Smooth Follow)

Discussion in 'Scripting' started by DaHackingNoob, Sep 17, 2014.

  1. DaHackingNoob

    DaHackingNoob

    Joined:
    Sep 17, 2014
    Posts:
    5
    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.
     
  2. cmcpasserby

    cmcpasserby

    Joined:
    Jul 18, 2014
    Posts:
    315
  3. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
  4. DaHackingNoob

    DaHackingNoob

    Joined:
    Sep 17, 2014
    Posts:
    5
  5. DaHackingNoob

    DaHackingNoob

    Joined:
    Sep 17, 2014
    Posts:
    5
  6. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    Last edited: Sep 17, 2014
  7. DaHackingNoob

    DaHackingNoob

    Joined:
    Sep 17, 2014
    Posts:
    5
  8. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    238
    Code (CSharp):
    1. //SmoothFollow.cs
    2. //C#
    3. using UnityEngine;
    4. using System.Collections;
    5.  
    6. public class SmoothFollow : MonoBehaviour
    7. {
    8.     public Transform target;        //GameObject that we should follow
    9.     public float distance;            //how far back?
    10.     public float maxDriftRange;        //how far are we allowed to drift from the target position
    11.     public float angleX;            //angle to pitch up on top of the target
    12.     public float angleY;            //angle to yaw around the target
    13.    
    14.     private Transform m_transform_cache;    //cache for our transform component
    15.     private Transform myTransform
    16.     {//use this instead of transform
    17.         get
    18.         {//myTransform is guarunteed to return our transform component, but faster than just transform alone
    19.             if (m_transform_cache == null)
    20.             {//if we don't have it cached, cache it
    21.                 m_transform_cache = transform;
    22.             }
    23.             return m_transform_cache;
    24.         }
    25.     }
    26.    
    27.     //this runs when values are changed in the inspector
    28.     void OnValidate()
    29.     {
    30.         if (target != null)
    31.         {//we have a target, move the camera to target position for preview purposes
    32.             Vector3 targetPos = GetTargetPos();
    33.             //update position
    34.             myTransform.position = targetPos;
    35.             //look at our target
    36.             myTransform.LookAt(target);
    37.         }
    38.     }
    39.    
    40.     //this runs every frame, directly after Update
    41.     void LateUpdate()
    42.     {//use this so that changes are immediate after the object has been affected
    43.    
    44.         Vector3 targetPos = GetTargetPos();
    45.         //calculate drift theta
    46.         float t = Vector3.Distance(myTransform.position, targetPos) / maxDriftRange;
    47.        
    48.         //smooth camera position using drift theta
    49.         myTransform.position = Vector3.Lerp(myTransform.position, targetPos, t * Time.deltaTime);
    50.         //look at our target
    51.         myTransform.LookAt(target);
    52.     }
    53.    
    54.     void OnDrawGizmos()
    55.     {//this is for editor purpose only, shows the relationship between this script and target as a line
    56.         Gizmos.color = Color.yellow;
    57.         Gizmos.DrawLine(myTransform.position, target.position);
    58.     }
    59.    
    60.     private Vector3 GetTargetPos()
    61.     {//returns where the camera should aim to be
    62.         //opposite of (-forward) * distance
    63.         Vector3 targetPos = new Vector3(0, 0, -distance);
    64.         //calculate pitch and yaw
    65.         targetPos = Quaternion.Euler(angleX, angleY, 0) * targetPos;
    66.         //return angled target position relative to target.position
    67.         return target.position + (target.rotation * targetPos);
    68.     }
    69. }
    70.  
    This script is smoother than some things I can think of! lol >.>
     
    Novodantis and ALaramee like this.
  9. DaHackingNoob

    DaHackingNoob

    Joined:
    Sep 17, 2014
    Posts:
    5

    Thanks will report back

    Noob.