Reputation: 13
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);
}
}
Upvotes: 1
Views: 1768
Reputation: 28128
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);
Upvotes: 1