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


    先说较为简单的一种:

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

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

    原理:摄像机与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中场景中的就可以实现简单的第三人称摄像机跟随。

  • 相关阅读:
    标签,css,排版
    浏览器的内核
    焦点事件
    cookie
    浏览器的行为
    百叶窗分析
    水仙花数
    递归函数
    拖拽的问题解决
    正则的具体
  • 原文地址:https://www.cnblogs.com/allyh/p/10987100.html
Copyright © 2020-2023  润新知