[英]Camera shaking on rotating player while crouching in unity
跟随播放器的相机出现问题。 当播放器移动时,相机会晃动,并且当播放器处于蹲伏位置时,此效果会更加明显。 我正在为混合动画的播放器使用根运动。
播放器脚本:
Transform cameraT;
void Start () {
cameraT = Camera.main.transform;
}
void FixedUpdate ()
{
float sideMotion = Input.GetAxis("SideMotion");
float straightMotion= Input.GetAxis("StraightMotion");
if (Mathf.Abs(sideMotion) > 0 || Mathf.Abs(straightMotion) > 0)
{
transform.eulerAngles = new Vector3(transform.eulerAngles.x,
cameraT.eulerAngles.y);
}
}
相机脚本:
public float distanceFromPlayer=2f;
public float mouseSensitivity=6;
public Transform player;
public Vector2 pitchConstraint= new Vector2(-30,80);
Vector3 rotationSmoothVelocity;
Vector3 currentRotation;
public float rotationSmoothTime = 0.2f;
float yaw; //Rotation around the vertical axis is called yaw
float pitch; //Rotation around the side-to-side axis is called pitch
private void LateUpdate()
{
yaw += Input.GetAxis("Mouse X") * mouseSensitivity;
pitch -= Input.GetAxis("Mouse Y") * mouseSensitivity;
pitch = Mathf.Clamp(pitch, pitchConstraint.x, pitchConstraint.y);
currentRotation = Vector3.SmoothDamp
(currentRotation, new Vector3(pitch, yaw), ref rotationSmoothVelocity, rotationSmoothTime);
transform.eulerAngles = currentRotation;
transform.position = player.position - transform.forward * distanceFromPlayer;
}
对于公共转换,我只是将一个空白的游戏对象添加到玩家的胸口级别并将其作为玩家的父元素。 我从一个在线教程中获得了旋转摄像机的流畅技巧,但是,尽管如此,确切的事情却无法在玩家旋转时起作用。
角色控制器连接到玩家,没有任何刚体或对撞机。
您将要在相机上使用Lerp,以使其从一个位置移动到另一个位置变得平滑。 脚本API有一个很好的Vector3.Lerp示例。
因此,在您的情况下,您也可以添加一个公共变量以平滑位置。 类似于positionSmoothTime。 然后创建一个“所需位置”变量,我们将其称为destPosition。
Vector3 destPosition = player.position - transform.forward * distanceFromPlayer;
transform.position = Vector3.Lerp(transform.position, destPosition, positionSmoothTime);
这样可以有效地平滑到卡住的位置。 可以帮助其他人提到的另一件事是将身体移动较少的部分(例如头部)用作目标。 结合使用Lerp功能,可以使相机平稳移动。
另一个大的帮助是将事物移到Update()函数中。 原因是, FixedUpdate()不会在每一帧都被调用。 因此,您可能会因此结结巴巴。 如果将所有内容移到Update()中 ,它将更新每一帧并有助于使内容平滑。 如果这样做,则需要将所有运动乘以Time.deltaTime ,如下例所示。
Vector3 destPosition = (player.position - transform.forward * distanceFromPlayer) * Time.deltaTime;
有关更多示例,请查看每个函数的链接,以查看其上的Unity文档。 它具有我在此处显示的所有内容的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.