Browse Source

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

This follows the recommendation from https://github.com/godotengine/godot/issues/45638

(cherry picked from commit 3f0e91a4d81f4cc17542f86119a3d8f833a62ff7)
Ricardo Buring 3 years ago
parent
commit
cb3199c0ef

+ 8 - 12
getting_started/first_3d_game/04.mob_scene.rst

@@ -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.
 
@@ -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))
 
@@ -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));
     }
@@ -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)
@@ -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);

+ 12 - 11
getting_started/first_3d_game/05.spawning_mobs.rst

@@ -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
@@ -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
 
@@ -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);
         }
     }
 

+ 13 - 11
getting_started/first_3d_game/07.killing_player.rst

@@ -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():
@@ -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()
@@ -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)
@@ -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);

+ 2 - 2
getting_started/first_3d_game/08.score_and_replay.rst

@@ -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():
@@ -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));
         }
 

+ 2 - 4
getting_started/first_3d_game/09.adding_animations.rst

@@ -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)
@@ -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);