Skip to content

Commit 76af79f

Browse files
committed
Fix look_at_from_position() usage in Your first 2D game tutorial
The mob's orientation was previously shifted according to the player's height, which could lead to collision and movement issues that were difficult to diagnose.
1 parent 1d9c4a4 commit 76af79f

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

getting_started/first_3d_game/04.mob_scene.rst

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,11 @@ between ``-PI / 4`` radians and ``PI / 4`` radians.
160160
func initialize(start_position, player_position):
161161
# We position the mob by placing it at start_position
162162
# and rotate it towards player_position, so it looks at the player.
163-
look_at_from_position(start_position, player_position, Vector3.UP)
163+
#
164+
# Ignore the player's height, so that the mob's orientation is not slightly
165+
# shifted if the mob spawns while the player is jumping.
166+
var target = Vector3(player_position.x, start_position.y, player_position.z)
167+
look_at_from_position(start_position, target, Vector3.UP)
164168
# Rotate this mob randomly within range of -45 and +45 degrees,
165169
# so that it doesn't move directly towards the player.
166170
rotate_y(randf_range(-PI / 4, PI / 4))
@@ -172,7 +176,11 @@ between ``-PI / 4`` radians and ``PI / 4`` radians.
172176
{
173177
// We position the mob by placing it at startPosition
174178
// and rotate it towards playerPosition, so it looks at the player.
175-
LookAtFromPosition(startPosition, playerPosition, Vector3.Up);
179+
//
180+
// Ignore the player's height, so that the mob's orientation is not slightly
181+
// shifted if the mob spawns while the player is jumping.
182+
Vector3 target = Vector3(player_position.x, start_position.y, player_position.z);
183+
LookAtFromPosition(startPosition, target, Vector3.Up);
176184
// Rotate this mob randomly within range of -45 and +45 degrees,
177185
// so that it doesn't move directly towards the player.
178186
RotateY((float)GD.RandRange(-Mathf.Pi / 4.0, Mathf.Pi / 4.0));

0 commit comments

Comments
 (0)