Skip to content

Fix 3D movement example code not resetting vertical velocity #10953

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions getting_started/first_3d_game/03.player_movement_code.rst
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,9 @@ fall speed separately. Be sure to go back one tab so the lines are inside the
target_velocity.z = direction.z * speed

# Vertical Velocity
if not is_on_floor(): # If in the air, fall towards the floor. Literally gravity
if is_on_floor(): # Stop falling
target_velocity.y = 0
else: # If in the air, fall towards the floor. Literally gravity
target_velocity.y = target_velocity.y - (fall_acceleration * delta)

# Moving the Character
Expand All @@ -210,7 +212,11 @@ fall speed separately. Be sure to go back one tab so the lines are inside the
_targetVelocity.Z = direction.Z * Speed;

// Vertical velocity
if (!IsOnFloor()) // If in the air, fall towards the floor. Literally gravity
if (IsOnFloor()) // Stop falling
{
_targetVelocity.Y = 0.0f;
}
else // If in the air, fall towards the floor. Literally gravity
{
_targetVelocity.Y -= FallAcceleration * (float)delta;
}
Expand All @@ -221,11 +227,11 @@ fall speed separately. Be sure to go back one tab so the lines are inside the
}

The ``CharacterBody3D.is_on_floor()`` function returns ``true`` if the body collided with the floor in this frame. That's why
we apply gravity to the ``Player`` only while it is in the air.
we apply gravity to the ``Player`` only while it is in the air and reset the vertical velocity otherwise.

For the vertical velocity, we subtract the fall acceleration multiplied by the
delta time every frame.
We subtract the fall acceleration multiplied by the delta time every frame.
This line of code will cause our character to fall in every frame, as long as it is not on or colliding with the floor.
If we are colliding with the floor, we instead reset the vertical component of the velocity.

The physics engine can only detect interactions with walls, the floor, or other
bodies during a given frame if movement and collisions happen. We will use this
Expand Down Expand Up @@ -282,7 +288,9 @@ Here is the complete ``player.gd`` code for reference.
target_velocity.z = direction.z * speed

# Vertical Velocity
if not is_on_floor(): # If in the air, fall towards the floor. Literally gravity
if is_on_floor(): # Stop falling
target_velocity.y = 0
else: # If in the air, fall towards the floor. Literally gravity
target_velocity.y = target_velocity.y - (fall_acceleration * delta)

# Moving the Character
Expand Down Expand Up @@ -337,7 +345,11 @@ Here is the complete ``player.gd`` code for reference.
_targetVelocity.Z = direction.Z * Speed;

// Vertical velocity
if (!IsOnFloor()) // If in the air, fall towards the floor. Literally gravity
if (IsOnFloor()) // Stop falling
{
_targetVelocity.Y = 0.0f;
}
else // If in the air, fall towards the floor. Literally gravity
{
_targetVelocity.Y -= FallAcceleration * (float)delta;
}
Expand Down