You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Unity Rigidbody组件时GameObject持续下落问题咨询

Troubleshooting Your Player Controller Issues

1. Why does your GameObject keep falling?

The short answer: Unity's Rigidbody component enables gravity by default. Here's the full breakdown:

  • When you add a Rigidbody to an object, it automatically uses the global gravity settings (found in Edit > Project Settings > Physics, default is 9.81 downward). Since your script doesn't override this, the object will keep falling until a physical collision stops it.
  • To fix this, pick an option that fits your game's needs:
    • Add ground collision: Ensure your ground object has a Collider (like Plane Collider or Box Collider) and that both the ground's Collider and your player's Collider have Is Trigger unchecked. The physics engine will then detect the collision and halt the fall.
    • Disable gravity entirely: If your player shouldn't be affected by gravity (e.g., a flying character), uncheck the Use Gravity box on your player's Rigidbody component, or add _rb.useGravity = false; inside the Start() method of your script.
    • Verify your player's Collider: Without a Collider attached to your player, the Rigidbody will pass right through the ground's Collider—so make sure your player has a Collider component (like Capsule Collider for humanoid characters).

2. Is the A/D horizontal movement behavior expected?

Based on your script's intended logic, no—this doesn't match what you wrote. Here's why:

  • Your code is set up to use A/D (bound to the Horizontal input axis) to rotate the player around the Y-axis, while W/S (Vertical axis) controls forward/backward movement. But you're seeing horizontal movement when pressing A/D—this means either:
    • Your Input Manager settings were modified (check Edit > Project Settings > Input Manager to confirm the Horizontal axis is still bound to A/D and left/right arrow keys), or
    • You had a different expectation for A/D (e.g., you want the player to strafe left/right instead of rotating).

If you want A/D to make the player strafe (side-to-side movement) instead of rotating, adjust your code like this:

void FixedUpdate() {
    SetVerticalDirection();
    SetHorizontalDirection();

    // Calculate forward/backward movement from W/S
    Vector3 forwardMovement = transform.forward * _vInput1 * Time.fixedDeltaTime;
    // Calculate left/right strafe movement from A/D
    Vector3 strafeMovement = transform.right * _hInput1 * Time.fixedDeltaTime;

    // Combine movements and apply to Rigidbody
    _rb.MovePosition(transform.position + forwardMovement + strafeMovement);
}

// Update this method to use MoveSpeed instead of RotateSpeed for strafing
void SetHorizontalDirection() {
    _hInput1 = Input.GetAxis("Horizontal") * MoveSpeed;
}

Quick Code Cleanup Tips

  • Your script class is named PlayerBehavoiur (note the missing 'u'—should be PlayerBehaviour). It won't break your game, but fixing typos helps with readability and consistency.
  • You can remove the commented-out code blocks to avoid cluttering your script as you refine it.

内容的提问来源于stack exchange,提问作者The_emporer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 19:47:50