【问题标题】:Camera Follow script not smooth?相机跟随脚本不流畅?
【发布时间】:2016-02-03 03:40:35
【问题描述】:

我的相机跟随脚本不是很流畅。如何平滑相机的运动?

这里是:

using UnityEngine;
using System.Collections;

public class FollowCamera : MonoBehaviour {

    public float interpVelocity;
    public float minDistance;
    public float followDistance;
    public GameObject target;
    public Vector3 offset;
    Vector3 targetPos;

    void Start () {
        targetPos = transform.position;
    }

    void FixedUpdate () {
        if (target)
        {
            Vector3 posNoZ = transform.position;
            posNoZ.z = target.transform.position.z;

            Vector3 targetDirection = (target.transform.position - posNoZ);

            interpVelocity = targetDirection.magnitude * 5f;

            targetPos = transform.position + (targetDirection.normalized * interpVelocity * Time.deltaTime); 

            transform.position = Vector3.Lerp( transform.position, targetPos + offset, 0.25f);

        }
    }
}

该脚本使相机跟随一个旋转目标。

【问题讨论】:

  • 将其从 FixedUpdate 移至 LateUpdate。 FixedUpdate 的调用频率通常比 Update 少,Camera 移动最好在 LateUpdate 上。
  • 还阅读了这个问题的数千个完全相同的重复项stackoverflow.com/questions/26640891/…

标签: c# unity3d unity5 gameobject


【解决方案1】:

您正在 FixedUpdate 上更新相机位置。将其更改为延迟更新。 FixedUpdate 是为其他目的而设计的,并且比每帧调用的频率更低。 LateUpdate 会在每一帧和 Update 之后调用,因此如果您的目标在 Update 相机上更新,那么稍后会更新其位置,这是所需要的。

【讨论】:

    猜你喜欢
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 2022-10-04
    • 2015-12-12
    • 1970-01-01
    • 2017-08-28
    • 2016-02-24
    • 1970-01-01
    相关资源
    最近更新 更多