简体   繁体   English

蹲伏时相机在旋转的播放器上晃动

[英]Camera shaking on rotating player while crouching in unity

I am having a problem with the camera following the player. 跟随播放器的相机出现问题。 When the player moves, the camera shakes and this effect is more noticeable when the player is in the crouched position. 当播放器移动时,相机会晃动,并且当播放器处于蹲伏位置时,此效果会更加明显。 I am using root motion for the player with animations by mixamo. 我正在为混合动画的播放器使用根运动。

Player Script: 播放器脚本:

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);

     }
}

Camera Script: 相机脚本:

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;
 }

For public transform I just added an empty game object to player chest level and parented it to the player. 对于公共转换,我只是将一个空白的游戏对象添加到玩家的胸口级别并将其作为玩家的父元素。 I got the rotating camera smoothly trick from an online tutorial but that exact thing won't work on player rotation though. 我从一个在线教程中获得了旋转摄像机的流畅技巧,但是,尽管如此,确切的事情却无法在玩家旋转时起作用。

A character controller is attached to player without any rigidbody or collider. 角色控制器连接到玩家,没有任何刚体或对撞机。

You are going to want to use Lerp on the camera in order to smooth its motion from one position to another. 您将要在相机上使用Lerp,以使其从一个位置移动到另一个位置变得平滑。 The Scripting API has a good example of Vector3.Lerp . 脚本API有一个很好的Vector3.Lerp示例。

So in your case, you could add a public variable for smoothing position as well. 因此,在您的情况下,您也可以添加一个公共变量以平滑位置。 Something like positionSmoothTime. 类似于positionSmoothTime。 Then make a "Desired Position" variable, we'll call it destPosition. 然后创建一个“所需位置”变量,我们将其称为destPosition。

Vector3 destPosition = player.position - transform.forward * distanceFromPlayer;

transform.position = Vector3.Lerp(transform.position, destPosition, positionSmoothTime);

This should effectively smooth it to where the stuttering should go away. 这样可以有效地平滑到卡住的位置。 Another thing that will help that someone else mentioned is using a part of the body that moves less (Like the head) as the target. 可以帮助其他人提到的另一件事是将身体移动较少的部分(例如头部)用作目标。 This combined with the Lerp function will give you a smooth camera movement. 结合使用Lerp功能,可以使相机平稳移动。

Another large help would be to move things into the Update() function. 另一个大的帮助是将事物移到Update()函数中。 The reason being is FixedUpdate() doesn't get called every frame. 原因是, FixedUpdate()不会在每一帧都被调用。 So you could potentially get stutter as a result. 因此,您可能会因此结结巴巴。 If you move everything into Update() it will update every frame and help smooth things out. 如果将所有内容移到Update()中 ,它将更新每一帧并有助于使内容平滑。 If you do this though you will need to multiply all movement by Time.deltaTime as shown in the example below. 如果这样做,则需要将所有运动乘以Time.deltaTime ,如下例所示。

Vector3 destPosition = (player.position - transform.forward * distanceFromPlayer) * Time.deltaTime;

For more examples, check the links to each function to see Unity's documentation on it. 有关更多示例,请查看每个函数的链接,以查看其上的Unity文档。 It has examples of everything I've shown here. 它具有我在此处显示的所有内容的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM