Skip to content

Commit 14823a2

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 14823a2

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

Diff for: getting_started/first_3d_game/04.mob_scene.rst

+20-4
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 = new 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));
@@ -271,7 +279,11 @@ Here is the complete ``mob.gd`` script for reference.
271279
func initialize(start_position, player_position):
272280
# We position the mob by placing it at start_position
273281
# and rotate it towards player_position, so it looks at the player.
274-
look_at_from_position(start_position, player_position, Vector3.UP)
282+
#
283+
# Ignore the player's height, so that the mob's orientation is not slightly
284+
# shifted if the mob spawns while the player is jumping.
285+
var target = Vector3(player_position.x, start_position.y, player_position.z)
286+
look_at_from_position(start_position, target, Vector3.UP)
275287
# Rotate this mob randomly within range of -45 and +45 degrees,
276288
# so that it doesn't move directly towards the player.
277289
rotate_y(randf_range(-PI / 4, PI / 4))
@@ -310,7 +322,11 @@ Here is the complete ``mob.gd`` script for reference.
310322
{
311323
// We position the mob by placing it at startPosition
312324
// and rotate it towards playerPosition, so it looks at the player.
313-
LookAtFromPosition(startPosition, playerPosition, Vector3.Up);
325+
//
326+
// Ignore the player's height, so that the mob's orientation is not slightly
327+
// shifted if the mob spawns while the player is jumping.
328+
Vector3 target = new Vector3(player_position.x, start_position.y, player_position.z);
329+
LookAtFromPosition(startPosition, target, Vector3.Up);
314330
// Rotate this mob randomly within range of -45 and +45 degrees,
315331
// so that it doesn't move directly towards the player.
316332
RotateY((float)GD.RandRange(-Mathf.Pi / 4.0, Mathf.Pi / 4.0));

0 commit comments

Comments
 (0)