平滑的摄像机旋转到面向方向的玩家



我尝试在 Unity 论坛上发布此内容,但在那里获得答案的情况非常罕见。

我正在尝试编写一个摄像机脚本,跟随我的角色走向他所面临的任何方向。它只能是北、南、西、东(每个动作输入左转或右转 90 度(。

所以我从Brackeys的视频中得到了这个脚本。我尝试自己实现旋转,但它根本没有旋转,但至少玩家已经被关注了。我希望你明白我在这里想要实现的目标。

{
public Transform target;
public float smoothSpeed = 0.125f;
public Vector3 offset;
private void LateUpdate()
{
Vector3 desiredPosition = target.position + offset;
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed);
transform.position = smoothedPosition;
transform.LookAt(target);
transform.rotation = Quaternion.RotateTowards(transform.rotation, target.transform.rotation, smoothSpeed);
}
}```

使用transform.LookAt(target);

此外,还有有用的:Quaternion.LookRotation()或者你可以处理向量,而不是四元数:

transform.forward = target.position - transform.position;

最新更新