为什么在我移动并且相机旋转时物体会卡住?

问题描述

我已经开始开发fps脚本,并按照教程来帮助使用该脚本。现在一切都很好,但是唯一的问题是,只要玩家同时移动和旋转,视图中的任何对象似乎就会口吃。 播放器移动和相机旋转的脚本如下。 有人能指出我正确的方向吗? 谢谢。

{

public Transform cam;
public Rigidbody rb;

public float camRotationSpeed = 5f;
public float camMinimumY = -60f;
public float camMaximumY = 75f;
public float rotationSmoothSpeed = 10f;

public float walkSpeed = 9f;
public float runSpeed = 14f;
public float maxSpeed = 20f;
public float jumpPower = 30f;

public float extraGravity = 45;

float bodyRotationX;
float camRotationY;
Vector3 directionIntentX;
Vector3 directionIntentY;
float speed;

public bool grounded;

void Update()
{
    LookRotation();
    Movement();
    ExtraGravity();
    GroundCheck();
    if(grounded && Input.GetButtonDown("Jump"))
    {
        Jump();
    }
}

void LookRotation()
{
    Cursor.visible = false;
    Cursor.lockState = CursorLockMode.Locked;

    bodyRotationX += Input.GetAxis("Mouse X") * camRotationSpeed;
    camRotationY += Input.GetAxis("Mouse Y") * camRotationSpeed;

    camRotationY = Mathf.Clamp(camRotationY,camMinimumY,camMaximumY);

    Quaternion camTargetRotation = Quaternion.Euler(-camRotationY,0);
    Quaternion bodyTargetRotation = Quaternion.Euler(0,bodyRotationX,0);

    transform.rotation = Quaternion.Lerp(transform.rotation,bodyTargetRotation,Time.deltaTime * rotationSmoothSpeed);
    cam.localRotation = Quaternion.Lerp(cam.localRotation,camTargetRotation,Time.deltaTime * rotationSmoothSpeed);
}

void Movement()
{
    directionIntentX = cam.right;
    directionIntentX.y = 0;
    directionIntentX.normalize();

    directionIntentY = cam.forward;
    directionIntentY.y = 0;
    directionIntentY.normalize();

    rb.veLocity = directionIntentY * Input.GetAxis("Vertical") * speed + directionIntentX * Input.GetAxis("Horizontal") * speed + Vector3.up * rb.veLocity.y;
    rb.veLocity = Vector3.ClampMagnitude(rb.veLocity,maxSpeed);

    if (Input.GetKey(KeyCode.LeftShift))
    {
        speed = runSpeed;
    }
    if (!Input.GetKey(KeyCode.LeftShift))
    {
        speed = walkSpeed;
    }

}

void ExtraGravity()
{
    rb.AddForce(Vector3.down * extraGravity);
}

void GroundCheck()
{
    RaycastHit groundHit;
    grounded = Physics.Raycast(transform.position,-transform.up,out groundHit,1.25f);
}

void Jump()
{
    rb.AddForce(Vector3.up * jumpPower,ForceMode.Impulse);
}

}

解决方法

这可能是因为相框需要同时更新相机的旋转角度和播放器的位置。我建议做固定更新的相机旋转。因为它将在每一帧的末尾运行。

像这样:

    void FixedUpdate()
    {
         LookRotation();
    }

我建议的另一件事是在运行地面检查时移动播放器,因为如果单独进行操作,则当播放器移动得更快时,我会看到一个错误出现,然后代码可以运行。因此,提示将是或可以在移动方法中运行它,或者在调用移动方法时调用该方法。

此:

    void Movement()
{
directionIntentX = cam.right;
directionIntentX.y = 0;
directionIntentX.Normalize();

directionIntentY = cam.forward;
directionIntentY.y = 0;
directionIntentY.Normalize();

rb.velocity = directionIntentY * Input.GetAxis("Vertical") * speed + directionIntentX * Input.GetAxis("Horizontal") * speed + Vector3.up * rb.velocity.y;
rb.velocity = Vector3.ClampMagnitude(rb.velocity,maxSpeed);

if (Input.GetKey(KeyCode.LeftShift))
{
    speed = runSpeed;
}
if (!Input.GetKey(KeyCode.LeftShift))
{
    speed = walkSpeed;
}
   GroundCheck();

}

或随意复制代码并将其粘贴到您的移动方法中。