Unity camera smooth Follow game characters

Source: Internet
Author: User

Assign this script to your camcorder, and then assign the game character to the character variable, and you can then implement the camera's smooth follow player anywhere on the earth.

Using unityengine;using System.collections;public class Smoothfollowerobj {private Vector3 targetposition;    private Vector3 position;    private Vector3 velocity;    private float smoothingtime;        private float prediction;        Public smoothfollowerobj (float smoothingtime) {targetposition = Vector3.zero;        Position = Vector3.zero;        velocity = Vector3.zero;        This.smoothingtime = Smoothingtime;    Prediction = 1;        } public Smoothfollowerobj (float smoothingtime, float prediction) {targetposition = Vector3.zero;        Position = Vector3.zero;        velocity = Vector3.zero;        This.smoothingtime = Smoothingtime;    This.prediction = prediction;        }//update should is called once per frame public Vector3 Update (Vector3 targetpositionnew, float deltatime) {        Vector3 targetvelocity = (targetpositionnew-targetposition)/deltatime;                Targetposition = targetpositionnew; Float d = mathf.min (1,Deltatime/smoothingtime);                Velocity = velocity* (1-d) + (targetposition+targetvelocity*prediction-position) *d;        Position + = Velocity*time.deltatime;    return position; Public Vector3 Update (Vector3 targetpositionnew, float deltatime, bool reset) {if (reset) {TA            Rgetposition = targetpositionnew;            Position = Targetpositionnew;            velocity = Vector3.zero;        return position;    } return Update (Targetpositionnew, deltatime);    } public Vector3 GetPosition () {return position;} Public Vector3 getvelocity () {return velocity;}}    public class Nogravitycamera:monobehaviour {//Here Nogravitycamera changed to your script name public gameobject character;    Public Vector3 Positionvector;    Public Vector3 Lookvector;    Private Smoothfollowerobj Posfollow;    Private Smoothfollowerobj Lookfollow;    Private Vector3 Lastvelocitydir;        Private Vector3 Lastpos; Use of this for initialization void STart () {positionvector=new Vector3 (0,2,4);        Lookvector=new Vector3 (0,0,1.5f);        Posfollow = new Smoothfollowerobj (0.5f,0.5f);        Lookfollow = new Smoothfollowerobj (0.1f,0.0f);        Posfollow.update (transform.position,0,true);        Lookfollow.update (character.transform.position,0,true);        Lastvelocitydir = Character.transform.forward;    Lastpos = character.transform.position; }//Update is called once per frame void lateupdate () {Lastvelocitydir + = (character.transform.positio        N-lastpos) *8;        Lastpos = character.transform.position;        Lastvelocitydir + = Character.transform.forward*time.deltatime;        Lastvelocitydir = lastvelocitydir.normalized;        Vector3 horizontal = transform.position-character.transform.position;        Vector3 Horizontal2 = horizontal;        Vector3 vertical = character.transform.up;        Vector3.orthonormalize (ref vertical,ref HORIZONTAL2); if (Horizontal.sqrmagnitude > HorizoNtal2.sqrmagnitude) horizontal = Horizontal2; Transform.position = posfollow.update (character.transform.position + horizontal*mathf.abs (positionvector.z) +                VERTICAL*POSITIONVECTOR.Y, Time.deltatime);        horizontal = Lastvelocitydir; Vector3 look = lookfollow.update (Character.transform.position + horizontal*lookvector.z-vertical*lookvector.y,        Time.deltatime);    Transform.rotation = Quaternion.fromtorotation (Transform.forward, look-transform.position) * transform.rotation; }}

This is the original article address: http://game.ceeger.com/forum/read.php?tid=1171&fid=2&page=1

Unity camera smooth Follow game characters

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.