小白的Unity5之路(二)镜头平滑跟随角色


这次要完成Camera跟随Player移动,

首先考虑Camera的跟随目标target和平滑移动速度smothing再考虑CameraPlayer的偏移量(就是CameraPlayer有一个永恒的长度)

 

目标位置和平滑移动速度 

public Transform target;

public float smothing = 5f;

 

偏移量

Vector3 offset;

 

设置偏移量

void Start () {

     offset = transform.position - target.position;

}

 

随着Player的移动,摄像机需要移动到新的位置

Vector3 targetCampos = target.position + offset;

得到Camera要移动的位置后

 transform.position=Vector3.Lerp(transform.position,targetCampos,smothing* Time.deltaTime);

 

则完整代码

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class CameraFloor : MonoBehaviour {
 5     public Transform target;
 6     public float smothing = 5f;
 7     Vector3 offset;
 8     // Use this for initialization
 9     void Start () {
10         offset = transform.position - target.position;
11     }
12     // Update is called once per frame
13     void FixedUpdate () {
14         Vector3 targetCampos = target.position + offset;
15         transform.position = Vector3.Lerp(transform.position, targetCampos, smothing * Time.deltaTime);//摄像机自身位置到目标位置
16     }
17 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM