Unity 中简单的第三人称摄像机跟随

半腔热情 提交于 2020-11-13 07:48:08

先说较为简单的一种:

一、将摄像机作为人物角色的子对象,设置好相对距离和偏移量即可,但这种方法弊端较多,一般不采用。

二、 设置好摄像机跟物体的相对距离,之后利用插值让摄像机平滑跟随。

原理:摄像机与player以向量(有大小,有方向)相连,这样就可以确定摄像机与player的相对距离了,这样人物走动,摄像机也会跟随移动。

 

将下列代码与camera绑定就可以实现第三人称摄像机跟随。代码:

public class CameraFollow : MonoBehaviour {

// 摄像机跟随的对象

public Transform target;

// The speed with which the camera will be following.

public float smoothing = 5f;

//偏移量

Vector3 offset;

void Start() {

//计算偏移量

offset = transform.position - target.position;

}

 

void LateUpdate () {

Vector3 targetCamPos = target.position + offset;

transform.position = Vector3.Lerp (transform.position, targetCamPos, smoothing * Time.deltaTime);

}

}

FixedUpdate():固定更新事件,执行N次,0.02秒执行一次。所有物理组件相关的更新都在这个事件中处理。

LateUpdate(): 一般用来处理摄像机方面的。

将场景中的角色(摄像机跟随的物体)拖入target中场景中的就可以实现简单的第三人称摄像机跟随。

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!