Search Unity

Where is the smooth follow script in Unity3?

Discussion in 'Scripting' started by Mangopork, Sep 30, 2010.

  1. Mangopork

    Mangopork

    Joined:
    Aug 16, 2009
    Posts:
    108
    Hey,

    I noticed in the Unity3 assets the smooth follow is gone along with the assets that went with the island demo.

    Is there a suitable replacement I could use?

    Thanks.
     
  2. jimmio92

    jimmio92

    Joined:
    Nov 3, 2009
    Posts:
    31
    I wrote this, you can use it all you want.

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraControl: MonoBehaviour
    5. {
    6.     public Transform followTransform;
    7.    
    8.     public Vector3 offset = new Vector3(0f, 2.5f, -5f);
    9.     public float moveSpeed = 1;
    10.     public float turnSpeed = 1;
    11.    
    12.     Vector3 goalPos;
    13.     //Quaternion goalRot;
    14.    
    15.     // Use this for initialization
    16.     void Start()
    17.     {
    18.         if(!followTransform)
    19.             followTransform = GameObject.FindGameObjectWithTag("Player").transform;
    20.         if(!followTransform)
    21.             this.enabled = false;
    22.     }
    23.    
    24.     void FixedUpdate()
    25.     {
    26.         goalPos = followTransform.position + followTransform.TransformDirection(offset);
    27.         transform.position = Vector3.Lerp(transform.position, goalPos, Time.deltaTime * moveSpeed);
    28.         transform.rotation = Quaternion.Lerp(transform.rotation, followTransform.rotation, Time.deltaTime * moveSpeed);
    29.     }
    30. }
    It will auto follow an object tagged Player if you forget to set the transform, otherwise will disable the script. The offset is how far from the object to stop.
     
  3. jimmio92

    jimmio92

    Joined:
    Nov 3, 2009
    Posts:
    31
    Also, the scripts are in Scripts.unityPackage in Unity\Editor\Standard Packages\
     
  4. charles0698

    charles0698

    Joined:
    Jan 5, 2014
    Posts:
    1
    it doesn't work it says:unexected char 'u'
    and they don't show the case 'target'