小白的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