Skip to content
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

Your first 3D game: Set the physics parameters of the mob before adding it to the scene tree #5689

Merged
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
20 changes: 8 additions & 12 deletions getting_started/first_3d_game/04.mob_scene.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ of motion and its velocity.
The function will take a ``start_position``, the mob's spawn position, and the
``player_position`` as its arguments.

We first position the mob at ``start_position``. Then, we turn it towards the
player using the ``look_at()`` method and randomize the angle by rotating a
We position the mob at ``start_position`` and turn it towards the player using
the ``look_at_from_position()`` method, and randomize the angle by rotating a
random amount around the Y axis. Below, ``rand_range()`` outputs a random value
between ``-PI / 4`` radians and ``PI / 4`` radians.

Expand All @@ -156,9 +156,8 @@ between ``-PI / 4`` radians and ``PI / 4`` radians.

# We will call this function from the Main scene.
func initialize(start_position, player_position):
translation = start_position
# We turn the mob so it looks at the player.
look_at(player_position, Vector3.UP)
# We position the mob and turn it so that it looks at the player.
look_at_from_position(start_position, player_position, Vector3.UP)
# And rotate it randomly so it doesn't move exactly toward the player.
rotate_y(rand_range(-PI / 4, PI / 4))

Expand All @@ -167,9 +166,8 @@ between ``-PI / 4`` radians and ``PI / 4`` radians.
// We will call this function from the Main scene
public void Initialize(Vector3 startPosition, Vector3 playerPosition)
{
Translation = startPosition;
// We turn the mob so it looks at the player.
LookAt(playerPosition, Vector3.Up);
// We position the mob and turn it so that it looks at the player.
LookAtFromPosition(startPosition, playerPosition, Vector3.Up);
// And rotate it randomly so it doesn't move exactly toward the player.
RotateY((float)GD.RandRange(-Mathf.Pi / 4.0, Mathf.Pi / 4.0));
}
Expand Down Expand Up @@ -270,8 +268,7 @@ Here is the complete ``Mob.gd`` script for reference.
move_and_slide(velocity)

func initialize(start_position, player_position):
translation = start_position
look_at(player_position, Vector3.UP)
look_at_from_position(start_position, player_position, Vector3.UP)
rotate_y(rand_range(-PI / 4, PI / 4))

var random_speed = rand_range(min_speed, max_speed)
Expand Down Expand Up @@ -303,8 +300,7 @@ Here is the complete ``Mob.gd`` script for reference.
// We will call this function from the Main scene
public void Initialize(Vector3 startPosition, Vector3 playerPosition)
{
Translation = startPosition;
LookAt(playerPosition, Vector3.Up);
LookAtFromPosition(startPosition, playerPosition, Vector3.Up);
RotateY((float)GD.RandRange(-Mathf.Pi / 4.0, Mathf.Pi / 4.0));

var randomSpeed = (float)GD.RandRange(MinSpeed, MaxSpeed);
Expand Down
23 changes: 12 additions & 11 deletions getting_started/first_3d_game/05.spawning_mobs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -227,46 +227,47 @@ Let's code the mob spawning logic. We're going to:
1. Instantiate the mob scene.
2. Sample a random position on the spawn path.
3. Get the player's position.
4. Add the mob as a child of the *Main* node.
5. Call the mob's ``initialize()`` method, passing it the random position and
4. Call the mob's ``initialize()`` method, passing it the random position and
the player's position.
5. Add the mob as a child of the *Main* node.

.. tabs::
.. code-tab:: gdscript GDScript

func _on_MobTimer_timeout():
# Create a Mob instance and add it to the scene.
# Create a new instance of the Mob scene.
var mob = mob_scene.instance()

# Choose a random location on Path2D.
# Choose a random location on the SpawnPath.
# We store the reference to the SpawnLocation node.
var mob_spawn_location = get_node("SpawnPath/SpawnLocation")
# And give it a random offset.
mob_spawn_location.unit_offset = randf()

var player_position = $Player.transform.origin
mob.initialize(mob_spawn_location.translation, player_position)

add_child(mob)
mob.initialize(mob_spawn_location.translation, player_position)

.. code-tab:: csharp

// We also specified this function name in PascalCase in the editor's connection window
public void OnMobTimerTimeout()
{
// Create a mob instance and add it to the scene.
// Create a new instance of the Mob scene.
Mob mob = (Mob)MobScene.Instance();

// Choose a random location on Path2D.
// We stire the reference to the SpawnLocation node.
// Choose a random location on the SpawnPath.
// We store the reference to the SpawnLocation node.
var mobSpawnLocation = GetNode<PathFollow>("SpawnPath/SpawnLocation");
// And give it a random offset.
mobSpawnLocation.UnitOffset = GD.Randf();

Vector3 playerPosition = GetNode<Player>("Player").Transform.origin;
mob.Initialize(mobSpawnLocation.Translation, playerPosition);

AddChild(mob);
mob.Initialize(mobSpawnLocation.Translation, playerPosition);

}

Above, ``randf()`` produces a random value between ``0`` and ``1``, which is
Expand All @@ -292,9 +293,9 @@ Here is the complete ``Main.gd`` script so far, for reference.
var mob_spawn_location = get_node("SpawnPath/SpawnLocation")
mob_spawn_location.unit_offset = randf()
var player_position = $Player.transform.origin
mob.initialize(mob_spawn_location.translation, player_position)

add_child(mob)
mob.initialize(mob_spawn_location.translation, player_position)

.. code-tab:: csharp

Expand All @@ -318,9 +319,9 @@ Here is the complete ``Main.gd`` script so far, for reference.
mobSpawnLocation.UnitOffset = GD.Randf();

Vector3 playerPosition = GetNode<Player>("Player").Transform.origin;
mob.Initialize(mobSpawnLocation.Translation, playerPosition);

AddChild(mob);
mob.Initialize(mobSpawnLocation.Translation, playerPosition);
}
}

Expand Down
24 changes: 13 additions & 11 deletions getting_started/first_3d_game/07.killing_player.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,20 @@ Starting with ``Main.gd``.


func _on_MobTimer_timeout():
# Create a Mob instance and add it to the scene.
# Create a new instance of the Mob scene.
var mob = mob_scene.instance()

# Choose a random location on Path2D.
# Choose a random location on the SpawnPath.
var mob_spawn_location = get_node("SpawnPath/SpawnLocation")
# And give it a random offset.
mob_spawn_location.unit_offset = randf()

# Communicate the spawn location and the player's location to the mob.
var player_position = $Player.transform.origin
mob.initialize(mob_spawn_location.translation, player_position)

# Spawn the mob by adding it to the Main scene.
add_child(mob)
mob.initialize(mob_spawn_location.translation, player_position)


func _on_Player_hit():
Expand All @@ -198,19 +200,21 @@ Starting with ``Main.gd``.

public void OnMobTimerTimeout()
{
// Create a mob instance and add it to the scene.
// Create a new instance of the Mob scene.
var mob = (Mob)MobScene.Instance();

// Choose a random location on Path2D.
// We stire the reference to the SpawnLocation node.
// Choose a random location on the SpawnPath.
// We store the reference to the SpawnLocation node.
var mobSpawnLocation = GetNode<PathFollow>("SpawnPath/SpawnLocation");
// And give it a random offset.
mobSpawnLocation.UnitOffset = GD.Randf();

// Communicate the spawn location and the player's location to the mob.
Vector3 playerPosition = GetNode<Player>("Player").Transform.origin;
mob.Initialize(mobSpawnLocation.Translation, playerPosition);

// Spawn the mob by adding it to the Main scene.
AddChild(mob);
mob.Initialize(mobSpawnLocation.Translation, playerPosition);
}

public void OnPlayerHit()
Expand Down Expand Up @@ -242,8 +246,7 @@ Next is ``Mob.gd``.


func initialize(start_position, player_position):
translation = start_position
look_at(player_position, Vector3.UP)
look_at_from_position(start_position, player_position, Vector3.UP)
rotate_y(rand_range(-PI / 4, PI / 4))

var random_speed = rand_range(min_speed, max_speed)
Expand Down Expand Up @@ -283,8 +286,7 @@ Next is ``Mob.gd``.

public void Initialize(Vector3 startPosition, Vector3 playerPosition)
{
Translation = startPosition;
LookAt(playerPosition, Vector3.Up);
LookAtFromPosition(startPosition, playerPosition, Vector3.Up);
RotateY((float)GD.RandRange(-Mathf.Pi / 4.0, Mathf.Pi / 4.0));

float randomSpeed = (float)GD.RandRange(MinSpeed, MaxSpeed);
Expand Down
4 changes: 2 additions & 2 deletions getting_started/first_3d_game/08.score_and_replay.rst
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,10 @@ Here is the complete ``Main.gd`` script for reference.
mob_spawn_location.unit_offset = randf()

var player_position = $Player.transform.origin
mob.initialize(mob_spawn_location.translation, player_position)

add_child(mob)
mob.connect("squashed", $UserInterface/ScoreLabel, "_on_Mob_squashed")
mob.initialize(mob_spawn_location.translation, player_position)


func _on_Player_hit():
Expand Down Expand Up @@ -435,9 +435,9 @@ Here is the complete ``Main.gd`` script for reference.
mobSpawnLocation.UnitOffset = GD.Randf();

Vector3 playerPosition = GetNode<Player>("Player").Transform.origin;
mob.Initialize(mobSpawnLocation.Translation, playerPosition);

AddChild(mob);
mob.Initialize(mobSpawnLocation.Translation, playerPosition);
mob.Connect(nameof(Mob.Squashed), GetNode<ScoreLabel>("UserInterface/ScoreLabel"), nameof(ScoreLabel.OnMobSquashed));
}

Expand Down
6 changes: 2 additions & 4 deletions getting_started/first_3d_game/09.adding_animations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,7 @@ And the *Mob*'s script.


func initialize(start_position, player_position):
translation = start_position
look_at(player_position, Vector3.UP)
look_at_from_position(start_position, player_position, Vector3.UP)
rotate_y(rand_range(-PI / 4, PI / 4))

var random_speed = rand_range(min_speed, max_speed)
Expand Down Expand Up @@ -510,8 +509,7 @@ And the *Mob*'s script.

public void Initialize(Vector3 startPosition, Vector3 playerPosition)
{
Translation = startPosition;
LookAt(playerPosition, Vector3.Up);
LookAtFromPosition(startPosition, playerPosition, Vector3.Up);
RotateY((float)GD.RandRange(-Mathf.Pi / 4.0, Mathf.Pi / 4.0));

float randomSpeed = (float)GD.RandRange(MinSpeed, MaxSpeed);
Expand Down