unity 相机跟随物体(角色)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unity 相机跟随物体(角色)相关的知识,希望对你有一定的参考价值。

 

unity 相机平滑跟随游戏角色

把这个脚本赋给你的摄像机,再把游戏角色赋给character变量,之后就能实现摄像机平滑的跟随player在地球的任一角落了。

 

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 be 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) {

            targetPosition = 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 {    // 这里的NoGravityCamera修改成你的脚本名

 

    public GameObject character;

    public Vector3 positionVector;

    public Vector3 lookVector;

    private SmoothFollowerObj posFollow;

    private SmoothFollowerObj lookFollow;

    private Vector3 lastVelocityDir;

    private Vector3 lastPos;

     

    // Use 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.position-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;

    }

}

  这是原文章地址:http://game.ceeger.com/forum/read.php?tid=1171&fid=2&page=1

以上是关于unity 相机跟随物体(角色)的主要内容,如果未能解决你的问题,请参考以下文章

unity3D:游戏分解之角色移动和相机跟随

[Unity3D/2D]实现相机对人物角色的跟随效果/相机在一定范围内移动/内置插件实现

Unity3D中怎么实现摄像机环绕角色和跟随角色的切换?

Unity 如何实现游戏Avatar角色头部跟随视角转动

unity3d中怎样制作血条跟随角色运动

Unity笔记第三人称相机跟随