我正在尝试创建一个第三人称运动脚本使用影院作为相机,我遵循了Brackey“统一中的第三人称运动”YouTube教程。然后我改变了它的基础,从字符控制器到刚体,运动工作得很好。然而,当我移动播放器时,我的代码将刚体y轴的速度设置为0,这会对抗重力,使播放器在我移动时缓慢地抖动到地面。然而,当玩家停止移动时,角色确实会倒在地上。我所需要的就是让脚本忽略y轴,简单地倾听unity的引力。
void Update()
{
if (!photonView.isMine)
{
Destroy(GetComponentInChildren<Camera>().gameObject);
Destroy(GetComponentInChildren<Cinemachine.CinemachineFreeLook>().gameObject);
return;
}
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
isGrounded = Physics.CheckSphere(new Vector3(transform.position.x, transform.position.y - 1, transform.position.z), 0.01f, layerMask);
Vector3 inputVector = new Vector3(horizontal, 0f, vertical).normalized;
if (inputVector.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(inputVector.x, inputVector.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
rb.velocity = moveDir.normalized * speed;
}
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
rb.AddForce(Vector3.up * jumpForce);
}
}发布于 2021-03-24 06:51:33
播放器会抖动,因为在您的移动部分中,您将y速度设置为0,因为Vector3.forward返回new Vector3(0, 0, 1),而您仅围绕y轴旋转向量。可以考虑这样做,而不是这样:
Vector3 moveDir = new Vector3(transform.forward.x, rb.velocity.y, transform.forward.z);这将保持速度不变,消除抖动。
注意:transform.forward会自动获取播放器的前进向量。
https://stackoverflow.com/questions/66765845
复制相似问题