使用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 ColliderorBox Collider) and that both the ground's Collider and your player's Collider haveIs Triggerunchecked. 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 Gravitybox on your player's Rigidbody component, or add_rb.useGravity = false;inside theStart()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 Colliderfor humanoid characters).
- Add ground collision: Ensure your ground object has a Collider (like
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
Horizontalinput axis) to rotate the player around the Y-axis, while W/S (Verticalaxis) controls forward/backward movement. But you're seeing horizontal movement when pressing A/D—this means either:- Your
Input Managersettings were modified (checkEdit > Project Settings > Input Managerto confirm theHorizontalaxis 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).
- Your
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 bePlayerBehaviour). 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
相关产品推荐
相关产品推荐

