IN THIS ARTICLE
Script Canvas Tutorial: Shooting a Target by Spawning Entities and Detecting Collisions
Script Canvas Tutorial: Shooting a Target by Spawning Entities and Detecting Collisions
This tutorial builds on what you learned in the previous two tutorials. In the following tutorial, you create a controllable sphere that shoots projectiles at a target.
Example
This involves several tasks:
- Spawn entities
- Add scripts that run on the spawned entities
- Set up collision
- Add tag filtering
Prerequisites
- Complete the Script Canvas Tutorial: Creating a Controllable Entity and Script Canvas Tutorial: Opening and Closing a Door with Trigger Areas and Variables.
Topics
- Prerequisites
- Step 1: Set Up a Projectile Entity
- Step 2: Create a Script to Propel the Projectile Forward
- Step 3: Save the Projectile Entity as a Slice
- Step 4: Set Up the Player Entity to Spawn the Slice
- Step 5: Set Up the Input Binding for the Projectile
- Step 6: Add Logic to the Player Script to Spawn the Projectile
- Step 7: Create a Target Entity
- Step 8: Add Logic to the Projectile to Destroy Target Entities
Step 1: Set Up a Projectile Entity
To create a projectile entity, create an entity and then add the Mesh, Rigid Body component, Mesh Collider , and Script Canvas components.
To create a projectile entity
In Lumberyard Editor, right-click in the Perspective viewport near your sphere and choose Create entity.
In the Entity Inspector, do the following:
For Name, enter Projectile.
In the Transform component, for Scale, set X to 0.25, Y to 0.25, and Z to 0.25.
Click Add Component and then choose the Mesh component.
In the Mesh component, for Mesh asset, click the browse (...) button, select the
\SamplesProject\Objects\default\primitive_sphere.cgf
file, and then click OK.
In the Entity Inspector, click Add Component and then choose the following components:
- Rigid Body Physics
- Mesh Collider
- Script Canvas
In Lumberyard Editor, choose Tools, Script Canvas.
In the Script Canvas editor, choose File, New Script.
After the graph loads, choose File, Save As.
In the Save As dialog box, for File name, enter projectile and then click Save.
In the Script Canvas component, for Script Canvas Asset, click the browse (...) button, select the
projectile.scriptcanvas
file, and then click OK.Verify that the Projectile entity looks like the following.
Example
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
In the Script Canvas editor, open the
projectile.scriptcanvas
file, if it’s not still open from Step 1: Set Up a Projectile Entity.In the Node Palette, enter start in the search box.
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.
In the Node Palette, enter Get Forward.
From Entity, Transform, drag the Get Forward node to the graph.
From On Graph Start, drag the Out pin to connect it to the In pin of the Get Forward node.
In the Get Forward node, for Scale, enter 200.
In the Node Palette, enter Set Velocity and drag the node to the graph.
From Get Forward, drag the Out pin to connect it to the In pin of the Set Velocity node.
From** Get Forward**, drag the Forward pin to the Velocity pin of the Set Velocity node.
To set a lifetime for the projectile, do the following:
In the Node Palette, enter delay in the search box.
Under Timing, drag Delay from the Node Palette to the graph.
From On Graph Start, drag the Out pin to connect it to the In pin of the Delay node.
In the Delay node, for Time, enter 1.0.
To destroy the projectile after the set time, do the following:
In the Node Palette, enter destroy in the search box.
Under Entity, Game Entity, drag Destroy Game Entity and Descendants from the Node Palette to the graph.
From Delay, drag the Out pin to connect it to the In pin of the Destroy Game Entity and Descendants node.
Verify that your
projectile.scriptcanvas
graph looks like the following.
Example
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
In Lumberyard Editor, in the Entity Outliner, right-click Projectile and choose Create slice.
In the Save As dialog box, for File name, enter projectile and then click Save.
To enable the projectile to spawn, do the following:
In the Asset Browser, navigate to the
projectile.slice
file that you just created.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.
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
In Lumberyard Editor, in the Entity Outliner, right-click the Player entity and choose Create child entity.
Enter a name for the child entity such as spawner.
In the Transform component, for the Translate Y property, enter 1.0. This sets the spawn point 1 meter in front of the parent entity.
Select the child entity and in the Entity Inspector, click Add Component, and then choose the Spawner component.
In the Spawner component, for Dynamic slice, click the browse (...) icon, select the
projectile.slice
file, and then click OK.Verify that the spawner entity looks like the following.
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
In Lumberyard Editor, in the Entity Outliner, select the Player entity.
In the Entity Inspector, under Input, click the Input Bindings Editor icon
.
In the Edit Asset window, do the following:
For Input Event Groups, click + to add a new input event group.
Expand the input event group. For Event Name, enter shoot.
For Event Generators, click + to add an event generator.
In the Class to create dialog box, click OK to add an input class.
Expand shoot, Event Generators, gamepad_button_a. For Input Device Type, choose keyboard.
For Input Name, choose keyboard_key_edit_space.
Verify that your settings appear as shown in the following.
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
In Lumberyard Editor, choose Tools, Script Canvas.
In the Script Canvas editor, choose File, Open. Select
player.scriptcanvas
and then click Open.Do the following to enable the event node to listen for the input event:
In the Node Palette, enter input in the search box.
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.
For Event Name, enter shoot.
Do the following to control the firing rate for the shoot event:
In the Node Palette, enter once in the search box.
Under Logic, drag Once from the Node Palette to the graph.
From Input Handler, drag the Held pin to connect it to the In pin of the Once node.
In the Node Palette, enter spawn in the search box.
Under Gameplay, Spawner, drag Spawn from the Node Palette to the graph.
From Once, drag the Out pin to connect it to the In pin of the Spawn node.
In the Spawn node, for Source, enter the name of the child entity with the Spawner component attached from Step 4.
In the Node Palette, enter delay in the search box.
Under Timing, drag Delay from the Node Palette to the graph.
From Spawn, drag the Out pin to connect it to the In pin of the Delay node.
In the Delay node, for Time, enter 0.25 (seconds).
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 Inputs, Outputs, and Connection Types.
Verify that your
player.scriptcanvas
graph looks like the following.
ExampleChoose File, Save. You can also press Ctrl+S.
In Lumberyard Editor, press Ctrl+G to enter game mode and test your script.
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.
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
In Lumberyard Editor, create an entity: Right-click in the Perspective viewport near your controllable sphere and choose Create entity.
In the Entity Inspector, for Name, enter Target.
Click Add Component and then choose the Mesh component.
In the Mesh component, for Mesh asset, click the browse (...) button, select the
\SamplesProject\Objects\default
\primitive_cylinder.cgf
file, and then click OK.Click Add Component and choose the following components:
- Static Physics
- Mesh Collider
- Tag
In the Tag component, click + to add a tag and for [0], enter Target.
Verify that your Target entity appears like the following.
Example
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
In the Script Canvas editor, open the
projectile.scriptcanvas
file.Do the following to add collision nodes:
In the Node Palette, enter collision in the search box.
Under Physics, Physics Component, drag On Collision from the Node Palette to the graph. This node has a special data enter called collision.
Do the following to add a Set hit node:
In the bottom-right pane, in the Variable Manager, click Create Variable. You can use variables to store and modify persistent values in your graph.
For more information, see Managing Script Canvas Variables.
Select Collision, double-click Variable 1 and then rename the variable to hit.
Right-click the hit variable and choose Set hit.
Select the Out pin for On Collision and drag to connect it to the In pin for the Set hit node.
From On Collision, drag the Collision pin to connect it to the Collision pin of the Set hit node.
Do the following to add tags:
In the Node Palette, enter CRC32 in the search box.
Under Math, CRC32, drag Create CRC32 from the Node Palette to the graph. The tag system uses CRC32s to store tag names.
From Set hit, drag the Out pin to connect it to the In pin of the Create CRC32 node.
In the Create CRC32 node, for String, enter Target.
In the Node Palette, enter tag in the search box.
Under Gameplay, Tag, drag Has Tag from the Node Palette to the graph.
From Create CRC32, drag the CRC32 pin to connect it to the Tag pin of the Has Tag node.
Do the following to add a Get hit node:
In the Variable Manager, right-click the hit variable and choose Get hit.
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.
From Create CRC32, drag the Out pin to connect it to the In pin of the Get hit node.
From Get hit, drag the Out pin to the In pin of the Has Tag node.
Do the following to add the If node:
In the Node Palette, enter if in the search box.
Under Logic, drag If from the Node Palette to the graph.
From Has Tag, drag the Out pin to connect it to the In pin of the If node.
From Has Tag, drag the Result pin to connect it to the Condition pin for the If node.
Do the following to add a destroy node:
In the Node Palette, enter destroy in the search box.
Under Entity, Game Entity, drag Destroy Game Entity and Descendants from the Node Palette to the graph.
From If, drag the True pin to connect it to the In pin of the Destroy Game Entity and Descendants node.
From Get hit, drag the entity: EntityID pin to connect it to the EntityID pin of the Destroy Game Entity and Descendants node.
Verify that your
projectile.scriptcanvas
graph looks like the following.
Example
In the Script Canvas editor, choose File, Save. You can also press Ctrl+S.
In Lumberyard Editor, press Ctrl+G to enter game mode and test your script.
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.
When you are done testing your script, press Esc.