这次要完成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 }