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

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

Introduction to Unity Visual Scripting Part 6: How to add collision detection with Collision

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
  • 1 Final Image
  • 2 Create Random Coordinates with the Random Function
  • 3 Use Instantiate to Create Objects and Add Them to the List
  • 4 Use a For Loop to Assign Numbers to the Objects in a List
  • 5 Limit the Number of Targets Generated

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

Name it [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]

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 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


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

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


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

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

Drag and drop Target into the Asset folder in the Project window to turn it into a 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

Add Instantiate and Get Identity to the Graph Editor.

Instantiate, Get Identity


Instantiate, Get Identity

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

Original PortConnect 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

Run the program. A [Target] is now generated 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 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


Get List Item


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

Run the program. The generated objects can now be 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

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

Run the scene. You can now 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

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

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

References

Top Articles
Travelers Inn ab 90 €. Motels in South San Francisco - KAYAK
Travelers Inn in South San Francisco, in den USA ab CHF 84: Angebote, Bewertungen, Fotos | momondo
Kmart near me - Perth, WA
Public Opinion Obituaries Chambersburg Pa
UPS Paketshop: Filialen & Standorte
Kathleen Hixson Leaked
Libiyi Sawsharpener
Caesars Rewards Loyalty Program Review [Previously Total Rewards]
Le Blanc Los Cabos - Los Cabos – Le Blanc Spa Resort Adults-Only All Inclusive
Obor Guide Osrs
Immobiliare di Felice| Appartamento | Appartamento in vendita Porto San
Limp Home Mode Maximum Derate
Calamity Hallowed Ore
Evita Role Wsj Crossword Clue
Obituary Times Herald Record
Large storage units
R/Altfeet
Best Fare Finder Avanti
Craigslist Apartments In Philly
Rams vs. Lions highlights: Detroit defeats Los Angeles 26-20 in overtime thriller
Sound Of Freedom Showtimes Near Cinelux Almaden Cafe & Lounge
Der Megatrend Urbanisierung
Divina Rapsing
Www Craigslist Milwaukee Wi
360 Tabc Answers
Is The Yankees Game Postponed Tonight
Baja Boats For Sale On Craigslist
6 Most Trusted Pheromone perfumes of 2024 for Winning Over Women
European Wax Center Toms River Reviews
Regina Perrow
Craigslist Middletown Ohio
Nail Salon Open On Monday Near Me
Strange World Showtimes Near Regal Edwards West Covina
Morlan Chevrolet Sikeston
Barrage Enhancement Lost Ark
Asian Grocery Williamsburg Va
Gold Nugget at the Golden Nugget
National Insider Threat Awareness Month - 2024 DCSA Conference For Insider Threat Virtual Registration Still Available
Gvod 6014
Author's Purpose And Viewpoint In The Dark Game Part 3
Live Delta Flight Status - FlightAware
manhattan cars & trucks - by owner - craigslist
ACTUALIZACIÓN #8.1.0 DE BATTLEFIELD 2042
Atu Bookstore Ozark
Gli italiani buttano sempre più cibo, quasi 7 etti a settimana (a testa)
Go Nutrients Intestinal Edge Reviews
Rescare Training Online
Wolf Of Wallstreet 123 Movies
Willkommen an der Uni Würzburg | WueStart
Canada Life Insurance Comparison Ivari Vs Sun Life
Minute Clinic Mooresville Nc
The Love Life Of Kelsey Asbille: A Comprehensive Guide To Her Relationships
Latest Posts
Article information

Author: Laurine Ryan

Last Updated:

Views: 5953

Rating: 4.7 / 5 (77 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Laurine Ryan

Birthday: 1994-12-23

Address: Suite 751 871 Lissette Throughway, West Kittie, NH 41603

Phone: +2366831109631

Job: Sales Producer

Hobby: Creative writing, Motor sports, Do it yourself, Skateboarding, Coffee roasting, Calligraphy, Stand-up comedy

Introduction: My name is Laurine Ryan, I am a adorable, fair, graceful, spotless, gorgeous, homely, cooperative person who loves writing and wants to share my knowledge and understanding with you.