
在本文中3D天堂将介绍在Unity3D中看到平滑的相机跟随,当希望相机在游戏中跟随玩家游戏对象时,它的稳定跟随状态是必需的。
平滑相机移动需要使用以下几点内容:
- 在更新中移动玩家对象
- 在LateUpdate中移动相机对象
- 使用SmoothDamp方法进行相机移动,它与LERP方法相比,这提供了更好的平滑度,可以从这里参阅文档。
使用下面的脚本来平滑相机移动并跟随玩家对象。
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
// camera will follow this object
public Transform Target;
//camera transform
public Transform camTransform;
// offset between camera and target
public Vector3 Offset;
// change this value to get desired smoothness
public float SmoothTime = 0.3f;
// This value will change at the runtime depending on target movement. Initialize with zero vector.
private Vector3 velocity = Vector3.zero;
private void Start()
{
Offset = camTransform.position - Target.position;
}
private void LateUpdate()
{
// update position
Vector3 targetPosition = Target.position + Offset;
camTransform.position = Vector3.SmoothDamp(transform.position, targetPosition, ref velocity, SmoothTime);
// update rotation
transform.LookAt(Target);
}
}
最终结果如下所示:

在第三点内容中提到了LERP的方法,可以从站内文章参阅:
…
以上是3D天堂关于Unity平滑相机跟随的全部内容,如果你有任何反馈,请随时在本页面下方留言。