控制相机跟随角色
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class FollowTarget : MonoBehaviour { 6 7 private Transform player; 8 private Vector3 offset; 9 private float speed = 3f; 10 // Use this for initialization 11 void Start () { 12 player = GameObject.FindGameObjectWithTag("Player").transform; 13 offset = transform.position - player.position; //相机位置减去玩家位置 14 } 15 16 // Update is called once per frame 17 void LateUpdate () { 18 Vector3 targetPosition = player.position + player.TransformDirection(offset); 19 transform.position = Vector3.Lerp(transform.position, targetPosition, Time.deltaTime * speed); 20 21 transform.LookAt(player.position); 22 } 23 }
常规情况下的跟随
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System.Collections; 2 using System.Collections.Generic; 3 using UnityEngine; 4 5 public class FollowPlayer : MonoBehaviour { 6 7 private Vector3 offset; 8 private Transform player; 9 private float speed = 3f; 10 11 // Use this for initialization 12 void Start () { 13 player = GameObject.FindGameObjectWithTag(Tags.player).transform; 14 offset = transform.position - player.position; 15 } 16 17 private void Update() 18 { 19 transform.position = player.position + offset; 20 } 21 }