如何修复玩家下落运动速度极慢的问题?

发布于 2025-01-11 03:20:21 字数 651 浏览 0 评论 0原文

我发现了一个非常简单的运动脚本,可以在 Unity 3D 中运行。唯一的问题是玩家下落的速度非常慢(几乎没有)。我该如何解决这个问题?就好像它被某种东西减慢了速度,因为其他物体以正常速度下落。更改项目设置也不起作用。

C# 代码(分配给玩家):

 public class PlayerMovement : MonoBehaviour
 {
     Rigidbody rb;
     public float speed;
  
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         Cursor.lockState = CursorLockMode.Locked;
         Cursor.visible = false;
     }
 
     void Update()
     {
         rb.velocity = transform.forward * Input.GetAxis("Vertical") * speed + transform.right * Input.GetAxis("Horizontal") * speed;
         transform.Rotate(0, Input.GetAxis("Mouse X"), 0);
     }
 }

I found an extremely simple movement script, that works in Unity 3D. The only problem is that the player falls extremely slow (almost not). How can I fix this? It's like it's being slowed down by something, because other objects are falling at normal speed. changing project settings doesn't work either.

C# Code (assigned to player):

 public class PlayerMovement : MonoBehaviour
 {
     Rigidbody rb;
     public float speed;
  
     void Start()
     {
         rb = GetComponent<Rigidbody>();
         Cursor.lockState = CursorLockMode.Locked;
         Cursor.visible = false;
     }
 
     void Update()
     {
         rb.velocity = transform.forward * Input.GetAxis("Vertical") * speed + transform.right * Input.GetAxis("Horizontal") * speed;
         transform.Rotate(0, Input.GetAxis("Mouse X"), 0);
     }
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恏ㄋ傷疤忘ㄋ疼 2025-01-18 03:20:21

我认为这是因为您手动设置速度,并且这会覆盖物理引擎(重力)也设置速度。您可以通过仅通过玩家动作设置 X 速度并保留由重力生成的 Y 速度来解决此问题。

rb = GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(direction * speed, rb.velocity.y);

对于跳跃,您可以施加垂直力,因此重力也仍然起作用

rb.AddForce(new Vector2(0, 5), ForceMode2D.Impulse);

I think it's because you are setting the velocity manually, and that overrides the physics engine (gravity) from also setting the velocity. You could fix it by only setting the X velocity by player action, and keep the Y velocity that is generated by gravity.

rb = GetComponent<Rigidbody2D>();
rb.velocity = new Vector2(direction * speed, rb.velocity.y);

For jumping, you can apply a vertical force, so the gravity force will also still work

rb.AddForce(new Vector2(0, 5), ForceMode2D.Impulse);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文