Lumberyard
User Guide (Version 1.15)

Script Canvas Tutorial: Shooting a Target by Spawning Entities and Detecting Collisions

This feature is in preview release and is subject to change.

This tutorial builds on what you learned in Script Canvas Tutorial: Creating a Controllable Entity and Script Canvas Tutorial: Opening and Closing a Door with Trigger Areas and Variables. This time you create a controllable sphere that shoots projectiles at a target.

Example


                    Example Script Canvas graph with collision targets.

This involves several tasks:

  • Spawn entities

  • Add scripts that run on the spawned entities

  • Set up collision

  • Add tag filtering

This tutorial assumes that Samples Project is set as the default project. For more information about changing your default project, see Choosing a Game Project to Open.

Note

If your Node Palette is missing nodes, you can update your preferences to show hidden nodes. For instructions, see Script Canvas Node Reference. To access diagnostic nodes, including the Print node, you must enable the Script Canvas Diagnostic Library gem and then build your game project. For more information, see Enabling Gems.

Step 1: Set Up a Projectile Entity

To create a projectile entity, you create an entity and add the Mesh, Rigid Body Physics, Mesh Collider, and Script Canvas components.

To create a projectile entity

  1. If you have not already done so, you must complete the Script Canvas Tutorial: Creating a Controllable Entity.

  2. In Lumberyard Editor, create an entity: Right-click in the Perspective viewport near your sphere and choose Create entity.

  3. In the Entity Inspector, do the following:

    1. For Name, enter Projectile.

    2. Under Transform, for Scale, set X to 0.25, Y to 0.25, and Z to 0.25.

      
                                Transform properties for the Projectile
                                    entity.
    3. Click Add Component.

    4. Under Rendering, click Mesh.

    5. Under Mesh, for Mesh asset, click the browse (...) button and navigate to the \SamplesProject\Objects\default directory. Select primitive_sphere.cgf and then click OK.

      
                                Select the mesh asset for the projectile entity.
  4. In the Entity Inspector, click Add Component. Under Physics, click Rigid Body Physics.

  5. Click Add Component again. Under Physics, click Mesh Collider.

    
                        Select the Mesh Collider component.
  6. Click Add Component again. Under Scripting, click Script Canvas.

    
                        Select the Script Canvas component.
  7. In Lumberyard Editor, choose Tools, Script Canvas.

  8. In the Script Canvas editor, choose File, New Script.

  9. After the graph loads, choose File, Save As.

  10. In the Save As dialog box, for File name, enter projectile and then click Save.

  11. In the Entity Inspector, under Script Canvas, for Script Canvas Asset, click the browse (...) button.

    
                        Select the Script Canvas graph file.
  12. Navigate to the projectile.scriptcanvas file. Select projectile.scriptcanvas and then click OK.

    
                        Select the Script Canvas graph file for the entity.

Step 2: Create a Script to Propel the Projectile Forward

Now that you've set up a projectile, you can create a script to propel the projectile forward when it spawns.

To create a projectile script

  1. In the Script Canvas editor, open the projectile.scriptcanvas file, if it's not still open from Step 1: Set Up a Projectile Entity.

  2. In the Node Palette, enter start in the search box.

  3. Under Utilities, drag On Graph Start from the Node Palette to the graph. This event node executes only for the first tick after the entity has initialized.

  4. To add the Get World Transform node, do the following:

    1. In the Node Palette, enter world transform in the search box.

    2. Under Entity, Transform, drag Get World Transform from the Node Palette to the graph.

    3. From On Graph Start, drag the Out pin to connect it to the In pin of the Get World Transform node.

      
                                Connect the On Graph Start node to the
                                        Get World Transform node.
  5. To obtain the y-axis of the transform, do the following:

    1. In the Node Palette, enter column in the search box.

    2. Under Math, Transform, drag Get Column from the Node Palette to the graph.

    3. From Get World Transform, drag the Out pin to connect it to the In pin of the Get Column node.

      
                                Connect the Get World Transform node to
                                    the Get Column node.
    4. From Get World Transform, drag the Transform pin to connect it to the Transform pin of the Get Column node.

      
                                Connect the Get World Transform node to
                                    the Get Column node.
    5. In the Get Column node, for Index, enter 1. This node returns the y-axis of the transform, which you use as the forward direction for the entity.

  6. To set the size of the forward node, do the following:

    1. In the Node Palette, enter multiply in the search box.

    2. Under Math, Vector3, drag Multiply By Number from the Node Palette to the graph. This node increases the size of the forward node for the initial velocity of the projectile.

    3. From Get Column, drag the Out pin to connect it to the In pin of the Multiply By Number node.

    4. From Get Column, drag the Column pin to connect it to the Vector 3 pin of the Multiply By Number node.

    5. In the Multiply By Number node, for Number, enter 200.

      
                                Connect the Get Column node to the
                                        Multiply By Number node.
  7. To set the velocity, do the following:

    1. In the Node Palette, enter velocity in the search box.

    2. Under Physics, Physics Component, drag Set Velocity from the Node Palette to the graph.

    3. From Multiply By Number, drag the Out pin to connect it to the In pin of the Set Velocity node.

    4. From Multiply By Number, drag the Vector3 pin to connect it to the Velocity pin of the Set Velocity node.

      
                                Connect the Multiply By Number node to
                                    the Set Velocity node.
  8. To set a lifetime for the projectile, do the following:

    1. In the Node Palette, enter delay in the search box.

      
                                Delay node in Script Canvas.
    2. Under Timing, drag Delay from the Node Palette to the graph.

    3. From On Graph Start, drag the Out pin to connect it to the In pin of the Delay node.

    4. In the Delay node, for Time, enter 1.0.

      
                                Connect the On Graph Start node to the
                                        Delay node.
  9. To destroy the projectile after the set time, do the following:

    1. In the Node Palette, enter destroy in the search box.

    2. Under Entity, Game Entity, drag Destroy Game Entity and Descendants from the Node Palette to the graph.

      
                                Use the Destroy Game Entity and
                                        Descendents node to destroy entities that are
                                    generated.
    3. From Delay, drag the Out pin to connect it to the In pin of the Destroy Game Entity and Descendants node.

      
                                Connect the Delay node to the
                                        Destroy Game Entity and Descendents
                                    node.
  10. Verify that your script looks like the following image:

    Example

    
                            Example script for the projectile entity in
                                Script Canvas.
  11. In the Script Canvas editor, choose File, Save. You can also press Ctrl+S.

Step 3: Save the Projectile Entity as a Slice

Now that you've created a projectile script, you can save the projectile entity as a slice.

To save the projectile entity as a slice

  1. In Lumberyard Editor, in the Entity Outliner, right-click Projectile and choose Create slice.

  2. In the Save As dialog box, for File name, enter projectile and then click Save.

  3. To enable the projectile to spawn, do the following:

    1. In the Asset Browser, navigate to the projectile.slice file that you just created.

      
                                Select the projectile.slice file in the
                                        Asset Browser.
    2. Right-click projectile.slice and choose Set Dynamic Slice. Scripts that run on a dynamic slice will properly remap source entity assignments to the entity that the graph runs on.

      
                                Set the projectile.slice file as a
                                    dynamic slice in the
                                    Asset Browser.
  4. In the Entity Outliner, right-click Projectile and then click Delete. Because you will spawn the projectile entity dynamically from your player script, you no longer need the entity in your scene.

Step 4: Set Up the Player Entity to Spawn the Slice

Now that you've created a slice, you can set up your Player entity to spawn the slice.

To set up the player entity to spawn the slice

  1. In Lumberyard Editor, in the Entity Outliner, select the Player entity.

  2. In the Entity Inspector, click Add Component.

  3. Under Gameplay, click Spawner.

    
                        Add the Spawner component to the
                            entity.
  4. In the Entity Inspector, under Spawner, click the browse (...) icon for Dynamic slice and navigate to the projectile.slice file. Select projectile.slice and then click OK.

    
                        Select the projectile.slice for the
                                Spawner component.

Step 5: Set Up the Input Binding for the Projectile

Now that you've added the Spawner component, you can set up the input binding to shoot the projectile when you press the Spacebar.

To set up the projectile input binding

  1. In Lumberyard Editor, in the Entity Inspector, under Input, click the Input Bindings Editor icon .

  2. In the Edit Asset window, do the following:

    1. For Input Event Groups, click + to add a new input event group.

    2. Expand the input event group. For Event Name, enter shoot.

    3. For Event Generators, click + to add an event generator.

    4. In the Class to create dialog box, click OK to add an input class.

    5. Expand shoot, Event Generators, gamepad_button_a. For Input Device Type, choose keyboard.

    6. For Input Name, choose keyboard_key_edit_space.

    7. Verify that your settings appear as shown in the following image.

      
                                Example inputbindings for the entity.
    8. In the Edit Asset window, choose File, Save.

Step 6: Add Logic to the Player Script to Spawn the Projectile

Now that you've set up the input binding, you can add logic to the Player script to spawn the projectile when you press the Spacebar.

To add logic to the player script

  1. In Lumberyard Editor, choose Tools, Script Canvas.

  2. In the Script Canvas editor, choose File, Open. Select player.scriptcanvas and then click Open.

  3. Do the following to enable the event node to listen for the input event:

    1. In the Node Palette, enter input in the search box.

    2. Under Gameplay, Input, drag Input Handler from the Node Palette to the graph. Input Handler is an event node. When an event occurs, the event node sends a message to the script.

    3. For Event Name, enter shoot.

      
                                Input Handler node in
                                    Script Canvas.
  4. Do the following to control the firing rate for the shoot event:

    1. In the Node Palette, enter once in the search box.

    2. Under Logic, drag Once from the Node Palette to the graph.

      
                                Once node in Script Canvas.
    3. From Input Handler, drag the Held pin to connect it to the In pin of the Once node.

      
                                Once node in Script Canvas.
    4. In the Node Palette, enter spawn in the search box.

    5. Under Gameplay, Spawner, drag Spawn from the Node Palette to the graph.

    6. From Once, drag the Out pin to connect it to the In pin of the Spawn node.

      
                                Connect the Once node to the
                                        Spawn node in Script Canvas.
    7. In the Node Palette, enter delay in the search box.

    8. Under Timing, drag Delay from the Node Palette to the graph.

      
                                Delay node in Script Canvas.
    9. From Spawn, drag the Out pin to connect it to the In pin of the Delay node.

    10. In the Delay node, for Time, enter 0.25 (seconds).

    11. From Delay, drag the Out pin to connect it to the Reset pin of the Once node.

    Note

    Node execution always flows from the left side to the right side of a node. For more information, see Pins and Connections.

  5. Verify that your script looks like the following image.

    Example

    
                            Example script for the spawned entities.
  6. Choose File, Save. You can also press Ctrl+S.

  7. In Lumberyard Editor, press Ctrl+G to enter game mode and test your script.

  8. Do the following:

    • To move the sphere forward, press W.

    • To move the sphere backwards, press S.

    • To move the sphere from side to side, press A and D.

    • To spawn a projectile that propels forward, press the Spacebar.

      If you hold the Spacebar, the spawn fires once and the Delay node resets the Once node every 0.25 seconds.

  9. When you are done testing your script, press Esc.

Step 7: Create a Target Entity

Now that the Player entity shoots projectiles, you can add a Target entity for the Player entity to destroy.

To create a target entity

  1. In Lumberyard Editor, create an entity: Right-click in the Perspective viewport near your controllable sphere and choose Create entity.

  2. In the Entity Inspector, for Name, enter Target.

    
                        Create an entity named Target.
  3. Click Add Component.

  4. Under Rendering, click Mesh.

  5. Under Mesh, for Mesh asset, click the browse (...) button and navigate to the \SamplesProject\Objects\default directory. Select primitive_cylinder.cgf and then click OK.

    
                        Assign a mesh asset to the Target
                            entity.
  6. Click Add Component again. Under Physics, click Static Physics.

  7. Click Add Component again. Under Physics, click Mesh Collider.

  8. Click Add Component again. Under Gameplay, click Tag.

  9. Under Tag, do the following:

    1. Click + to add a tag.

    2. For [0], enter Target.

      
                                Tag component attached to the
                                        Target entity.
  10. Verify that your settings appear as shown in the following image:

    
                        Example components attached to the Target
                            entity.

Step 8: Add Logic to the Projectile to Destroy Target Entities

Now that you've set up the Target entity, you can add logic to the projectile to destroy entities that have the Target tag.

To add logic to destroy target entities

  1. In the Script Canvas editor, open the projectile.scriptcanvas file.

  2. Do the following to add collision nodes:

    1. In the Node Palette, enter collision in the search box.

    2. Under Physics, Physics Component, drag On Collision from the Node Palette to the graph. This node has a special data type called collision.

      
                                On Collision node in
                                    Script Canvas.
  3. Do the following to add a Set hit node:

    1. Choose View, Variable Manager and click Create Variable.

    2. Select Collision and enter hit to rename it.

    3. Right-click the hit variable and choose Set hit.

  4. Select the Out pin for On Collision and drag to connect it to the In pin for the Set hit node.

  5. From On Collision, drag the Collision pin to connect it to the Collision pin of the Set hit node.

    
                                Connect the On Collision node to the
                                        Set hit variable node in
                                    Script Canvas.
  6. Do the following to add tags:

    1. In the Node Palette, enter CRC32 in the search box.

    2. Under Math, CRC32, drag Create CRC32 from the Node Palette to the graph. The tag system uses CRC32s to store tag names.

    3. From Set hit, drag the Out pin to connect it to the In pin of the Create CRC32 node.

    4. In the Create CRC32 node, for String, enter Target.

      
                                Connect the Set hit node to the
                                        Create CRC32 node in
                                    Script Canvas.
    5. In the Node Palette, enter tag in the search box.

    6. Under Gameplay, Tag, drag Has Tag from the Node Palette to the graph.

      
                                Has Tag node in
                                    Script Canvas.
    7. From Create CRC32, drag the Out pin to connect it to the In pin of the Has Tag node.

    8. From Create CRC32, drag the CRC32 pin to connect it to the Tag pin of the Has Tag node.

      
                                Connect the Create CRC32 node to the
                                        On Collision node in
                                    Script Canvas.
  7. Do the following to add a Get hit node:

    1. In the Variable Manager, right-click the hit variable and choose Get hit.

    2. From Get hit, drag the entity: EntityID pin to connect it to the Source pin of the Has Tag node. This enables the Has Tag node to check the entity that the projectile collided with rather than itself.

      
                                Connect the Get hit variable node to
                                    the Has Tag node in
                                    Script Canvas.
  8. Do the following to add the If node:

    1. In the Node Palette, enter if in the search box.

    2. Under Logic, drag If from the Node Palette to the graph.

    3. From Has Tag, drag the Out pin to connect it to the In pin of the If node.

    4. From Has Tag, drag the Result pin to connect it to the Condition pin for the If node.

      
                                Connect the Has Tag node in to the
                                        If node in Script Canvas.
  9. Do the following to add a destroy node:

    1. In the Node Palette, enter destroy in the search box.

    2. Under Entity, Game Entity, drag Destroy Game Entity and Descendants from the Node Palette to the graph.

      
                                Destroy Game Entity and Descendants
                                    node in Script Canvas.
    3. In the Destroy Game Entity and Descendants node, for EntityID, enter Target.

    4. From If, drag the True pin to connect it to the In pin of the Destroy Game Entity and Descendants node.

    5. From Get hit, drag the entity: EntityID pin to connect it to the EntityID pin of the Destroy Game Entity and Descendants node.

    6. From Destroy Game Entity and Descendants, drag the Out pin to connect it to the In pin of the Destroy Game Entity and Descendants node that is connected to the Delay node. You created this connection earlier in the tutorial.

  10. Verify that your script looks like the following image:

    
                        Example Script Canvas graph for the projectile entity.
  11. In the Script Canvas editor, choose File, Save. You can also press Ctrl+S.

  12. In Lumberyard Editor, press Ctrl+G to enter game mode and test your script.

  13. To aim and shoot at the target, do the following:

    • To move the sphere forward, press W.

    • To move the sphere backward, press S.

    • To move the sphere from side to side, press A and D.

    • To spawn a projectile that propels forward, press the Spacebar.

  14. When you are done testing your script, press Esc.