设置第一人称视角
1.把物体的坐标和摄像机的坐标设置成一样的,这样摄像机就在物体内部,就是第一人称的视角。
2.把摄像机拖拽进物体对象内,摄像机变成物体的子对象,这样摄像机就能跟着物体一起移动。
设置第三人称视角
方式一:(设置子对象)
1.将摄像机拖拽到合适的角度上
2.把摄像机拖拽进物体对象,摄像机成为物体的子对象,这样摄像机就可以跟着物体一起移动
方式二:(脚本设置)
- 创建摄像机对象变量:
private Animation animation;
- 获取主摄像机:
mainCamera = Camera.main;
- 设置主摄像机的位置:主摄像机的位置 = 物体对象的位置 + 两者相对坐标差
mainCamera.transform.position = transform.position + new Vector3(0, 11, -14);
实现摄像机延迟移动效果
Vector3.Lerp(主摄像机的位置, 主摄像机新位置, 时间差);
mainCamera.transform.position = Vector3.Lerp( mainCamera.transform.position, transform.position + new Vector3(0, 11, -14), Time.deltaTime);
代码实例
public class Player01 : MonoBehaviour
{
//设置变量
private Animation animation;
private Camera mainCamera;
float runSpeed;
float angleSpeed;
// Start is called before the first frame update
void Start()
{
//获取动画对象
animation = GetComponent<Animation>();
//初始化移动速度和角速度
runSpeed = 20f;
angleSpeed = 120f;
//获取主摄像机
mainCamera = Camera.main;
}
// Update is called once per frame
void Update()
{
//设置移动速度
float forward = Input.GetAxisRaw("Vertical") * runSpeed;
//设置角速度
float rotation = Input.GetAxisRaw("Horizontal") * angleSpeed;
//判断移动还是站立
if (forward > 0)
{
//设置移动方向
transform.Translate(0, 0, forward * Time.deltaTime);
//播放跑步动画
animation.CrossFade("run");
}
else
{
//播放站立动画
animation.CrossFade("stand");
}
//设置移动方向
transform.Rotate(0, rotation * Time.deltaTime, 0);
}
private void LateUpdate()
{
//主摄像机的位置 = 物体对象的位置 + 两者相对坐标差
//mainCamera.transform.position = transform.position + new Vector3(0, 11, -14);
//摄像机延迟移动效果
mainCamera.transform.position = Vector3.Lerp(
mainCamera.transform.position,
transform.position + new Vector3(0, 11, -14),
Time.deltaTime);
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)