這次要完成Camera跟隨Player移動,
首先考慮Camera的跟隨目標target和平滑移動速度smothing再考慮Camera與Player的偏移量(就是Camera與Player有一個永恆的長度)
目標位置和平滑移動速度
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 }