向图中一样,让摄像机偏移位置跟随物体,而不是生硬的去跟随,需要使用Lerp 如以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using UnityEngine; using System.Collections; public class FollowPlayer : MonoBehaviour { private Transform player; private float speed = 5f; // Use this for initialization void Start () { player = GameObject.FindGameObjectWithTag (Tags.player).transform; } // Update is called once per frame void Update () { // 定义摄像机的位置 Vector3 targetPos = player.position + new Vector3 (0, 3f, -3f); // 通过插值计算改变摄像机位置 transform.position = Vector3.Lerp (transform.position, targetPos, speed * Time.deltaTime); // 使摄像机一直看向Player Quaternion targetRot = Quaternion.LookRotation (player.position - transform.position); // Slerp 一般用于角度的插值计算 transform.rotation = Quaternion.Slerp (transform.rotation, targetRot, speed * Time.deltaTime); } } |
- 本文固定链接: http://www.u3d8.com/?p=299
- 转载请注明: 网虫虫 在 u3d8.com 发表过