开发摄像机的跟随
?使用Vertor3.Lerp 插值运算
摄像机的视角,让摄像机观察着角色
?Quaternion.LookRotation 得到一个四元数,表示跟参数一样方向的旋转
?Quaternion.Slerp 按照圆弧插值,这个插值计算更适用于,角度变换
首先调整摄像机,使它和player在一个合适角度和位置,然后选择GameObject-Align With View,锁定视角,把Camera拖动到Player下,以Player为本地坐标,记录下此时Camera的Y轴和Z轴。记录完之后把Camera拖出来恢复。,接下来到代码时间。
摄像机如何找到Player,可以通过标签找到Player,把Player的Tag设置为Player,定义一个名为Tags的C#脚本,代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class Tags {public const string player = "Player";}再定义一个名为fllowplayer的c#的脚本,代码如下:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class fllowplayer : MonoBehaviour {private Transform player;public float speed = 2;// Use this for initializationvoid Start () {player = GameObject.FindGameObjectWithTag(Tags.player).transform;}// Update is called once per framevoid Update () {Vector3 targetPos = player.position + new Vector3(0, 1.75f, -3.42f);//前面记录下的Y轴和Z轴的值.transform.position = Vector3.Lerp(transform.position, targetPos, speed * Time.deltaTime);Quaternion targetRotation = Quaternion.LookRotation(player.position-transform.position);transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, speed * Time.deltaTime);}
}