Introduction to Unity Visual Scripting Part 7: Creating a target game using Raycast and List (Part 1)

This article is the seventh installment of the Introduction to Visual Scripting series. You can check the previous article from the link below.

This article is divided into two parts and explains how to create a target shooting game using Raycast and List.

What you will learn in this part (Part 1):

 

  • Adding elements to a List
  • Random
  • For loops
  • Instantiate

Final Image

Final Image

Final Image

Create Random Coordinates with the Random Function

The Random function outputs a random number within a specified range.

Click the + button in the Hierarchy and create an empty GameObject.

Create a GameObject

Create a GameObject

Name it [AimLabManager].

[AimLabManager]

[AimLabManager]

Attach a Script Machine to [AimLabManager], create a new graph in a folder called Macros, and save it there.

Name the graph [AimLabGraph].

[AimLabGraph]

[AimLabGraph]

Since we want to generate targets every few seconds, we need to set a timer.

Add a variable to measure time. Add a Float variable [Time] to the Variables in [AimLabManager], and add [Time] to the Graph Editor.

Add a Float variable [Time] to [AimLabManager]

Add a Float variable [Time] to [AimLabManager]

Add the following nodes to the Graph Editor.

 

  • Get Delta Time
  • Add
  • If
  • Greater
  • Set Object Variable ×2
  • Float Literal

※ Only unclear node images are provided.

Get Delta Time

Get Delta Time


Add

Add

Connect the nodes as shown below.

This time, we will generate a target every 0.5 seconds, so input 0.5 into Greater.

If the time exceeds 0.5 seconds, the [Time] will be reset to zero. Therefore, two Set Object Variable nodes are prepared.

Prepare two Set Object Variable nodes

Prepare two Set Object Variable nodes

Next, we will create random coordinates. Add Random Range and Vector3 Create(X, Y, Z) to the Graph Editor.

Select the Random Range with Result (Float Output).

If you choose the Int type, the random numbers will be integers.

This time, we will add randomness with a Float variable.

Also, since we want to add randomness to the X and Y axes, create two Random Range nodes.

Random Range

Random Range


Vector3 Create(X, Y, Z)

Vector3 Create(X, Y, Z)

Connect the nodes as shown below.

The upper variable in Random Range is the minimum value, and the lower variable is the maximum value, which will generate a random number within that range.

This time, set the X-axis to -4 to 4 and the Y-axis to 0 to 3.

Connect nodes

Connect nodes

Use Instantiate to Create Objects and Add Them to the List

We can use Instantiate to create objects, so we’ll use it to create the targets.

First, create a Prefab for the target. Click the + button in the Hierarchy, then click 3D Object → Sphere to create a Sphere.

Name it [Target].

Create a Sphere

Create a Sphere

Drag and drop Target into the Asset folder in the Project window to turn it into a Prefab.

Drag and drop to create Prefab

Drag and drop to create Prefab

After converting it to a Prefab, delete the Target from the Hierarchy.

Drag and drop the Prefabbed Target into the Graph Editor, and when the following window appears, press Enter to add it.

Drag and drop the Prefabbed Target into the Graph Editor

Drag and drop the Prefabbed Target into the Graph Editor

Add Instantiate and Get Identity to the Graph Editor.

Instantiate, Get Identity

Instantiate, Get Identity


Instantiate, Get Identity

Instantiate, Get Identity

Connect the nodes as shown below. Here is an explanation of each port on the Instantiate node:

Original Port Connect the Prefab of the object to be instantiated.
Position Port (Vector3 variable) Connect the position of the object to be instantiated.
Rotation Port (Quaternion variable) Connect the rotation of the object to be instantiated.

Get Identity contains a Quaternion value of (0,0,0,1).

Connect nodes

Connect nodes

Run the program. A [Target] is now generated at random coordinates.

Generate [Target] at random coordinates

Generate [Target] at random coordinates

Use a For Loop to Assign Numbers to the Objects in a List

Add an Aot List type List variable. Name it [TargetList] and add it to the Graph Editor.

Add an Aot List type List variable

Add an Aot List type List variable

Add the following nodes to the Graph Editor:

 

  • Add Item
  • Count Item
  • For Loop
  • Get List Item
  • Integer To String
  • Set Name

※ Only unclear node images are provided.

Integer To String

Integer To String


Get List Item

Get List Item


Set Name

Set Name

Connect the nodes as shown below.

The instantiated object is output from the lower right port of Instantiate, so use Add Item to add it to the list.

The middle left port of Add Item connects to the list to which you want to add, and the lower port connects to the object to be added to the list.

After that, rename the Targets in the list.

The For Loop repeats the process connected to the Body for a specified number of times.

The First port has the initial value input.

The value in the Step port is added to the First value with each iteration, and when it equals the Last value, the For Loop ends, and the process exits through the Exit port.

Here’s what happens after the Body:

The Index outputs the current loop count as an Int.

To String is used to convert the Index to a String.

The list is numbered starting from 0 based on the order in which items are stored.

Get Item retrieves the information stored in the list at the number connected to the Index.

Finally, Set Name is used to rename the objects.

The object to be renamed is connected to the second port from the top, and the new name is connected to the port below it.

Connect nodes

Connect nodes

Run the program. The generated objects can now be numbered sequentially from 0.

Objects numbered sequentially from 0

Objects numbered sequentially from 0

Limit the Number of Targets Generated

Add the [Target List] variable to the Graph Editor.

Add the following nodes to the Graph Editor:

 

  • Count Items
  • Greater
  • If

※ Only unclear node images are provided.

If

If

Add these nodes after On Update.

This time, we will generate up to four objects.

Enter 3 into Greater (the count starts at 0, so 0 through 3 counts as four items).

When the elements in [Target List] exceed 4, Greater will return True, preventing the process after If from executing.

Connect nodes

Connect nodes

Run the scene. You can now limit the number of targets generated to four.

Limit the number of targets generated to four

Limit the number of targets generated to four

Here is an overview of all the Visual Scripting nodes used this time.

Overview of the nodes

Overview of the nodes

The next article will introduce a mechanism where targets disappear when clicked.