using UnityEngine; public class CameraFollow : MonoBehaviour { private Transform target; public float offsetY = 3f; public float offsetZ = 6f; public float speed = 10f; void Awake() { //默认跟随标签为Player的物体 target = GameObject.FindGameObjectWithTag("Player").transform; } public void SetTarget(Transform t) { target = t; } void LateUpdate() { if (target != null) { transform.LookAt(target); Vector3 tar = target.TransformPoint(0, offsetY, -offsetZ); transform.position = Vector3.Lerp(transform.position, tar,Time.deltaTime * speed); } } }
根据跟随目标,获取它向后面和上面的指定长度的世界位置,作为相机的滑动目标位置。