Skip to content
Closed
Show file tree
Hide file tree
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
10 changes: 8 additions & 2 deletions getting_started/first_3d_game/06.jump_and_squash.rst
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ With this code, if no collisions occurred on a given frame, the loop won't run.
if collision.get_collider().is_in_group("mob"):
var mob = collision.get_collider()
# we check that we are hitting it from above.
if Vector3.UP.dot(collision.get_normal()) > 0.1:
if not mob.is_queued_for_deletion() and Vector3.UP.dot(collision.get_normal()) > 0.1:
# If so, we squash it and bounce.
mob.squash()
target_velocity.y = bounce_impulse
Expand All @@ -274,7 +274,7 @@ With this code, if no collisions occurred on a given frame, the loop won't run.
if (collision.GetCollider() is Mob mob)
{
// We check that we are hitting it from above.
if (Vector3.Up.Dot(collision.GetNormal()) > 0.1f)
if (mob.IsQueuedForDeletion() && Vector3.Up.Dot(collision.GetNormal()) > 0.1f)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (mob.IsQueuedForDeletion() && Vector3.Up.Dot(collision.GetNormal()) > 0.1f)
if (!mob.IsQueuedForDeletion() && Vector3.Up.Dot(collision.GetNormal()) > 0.1f)

You've got it backwards here

{
// If so, we squash it and bounce.
mob.Squash();
Expand All @@ -300,6 +300,12 @@ information about where and how the collision occurred. For example, we use its

The method ``is_in_group()`` is available on every :ref:`Node<class_Node>`.

Multiple Godot collisions can happen during the same frame including multiple collisions
with the same object. That is why we should prevent ourselves from emitting the squash event
multiple times by checking if the mob node is queued for deletion. This will work since
we will be deleting the mob once squashed. We do this with the ``is_queued_for_deletion``
method.

To check that we are landing on the monster, we use the vector dot product:
``Vector3.UP.dot(collision.get_normal()) > 0.1``. The collision normal is a 3D vector
that is perpendicular to the plane where the collision occurred. The dot product
Expand Down
4 changes: 2 additions & 2 deletions getting_started/first_3d_game/07.killing_player.rst
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ Finally, the longest script, ``Player.gd``:
if collision.get_collider().is_in_group("mob"):
var mob = collision.get_collider()
# we check that we are hitting it from above.
if Vector3.UP.dot(collision.get_normal()) > 0.1:
if not mob.is_queued_for_deletion() and Vector3.UP.dot(collision.get_normal()) > 0.1:
# If so, we squash it and bounce.
mob.squash()
target_velocity.y = bounce_impulse
Expand Down Expand Up @@ -491,7 +491,7 @@ Finally, the longest script, ``Player.gd``:
if (collision.GetCollider() is Mob mob)
{
// We check that we are hitting it from above.
if (Vector3.Up.Dot(collision.GetNormal()) > 0.1f)
if (mob.IsQueuedForDeletion() && Vector3.Up.Dot(collision.GetNormal()) > 0.1f)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (mob.IsQueuedForDeletion() && Vector3.Up.Dot(collision.GetNormal()) > 0.1f)
if (!mob.IsQueuedForDeletion() && Vector3.Up.Dot(collision.GetNormal()) > 0.1f)

{
// If so, we squash it and bounce.
mob.Squash();
Expand Down