Reader small image

You're reading from  Enhancing Virtual Reality Experiences with Unity 2022

Product typeBook
Published inNov 2023
Reading LevelBeginner
PublisherPackt
ISBN-139781804619537
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Steven Antonio Christian
Steven Antonio Christian
author image
Steven Antonio Christian

Steven Christian is an Augmented Reality Developer, Animator, and Medical Student Student. He's also the Owner of Iltopia Studios where he creates comics and cartoons that explore the Black experience in America. He is Unity Certified 3D Artist and Instructor and his goal is to improve equity in arts and technology through content creation and community-focused education.
Read more about Steven Antonio Christian

Right arrow

Working with C#, Unity Events, and Input Actions

This chapter will explore the basics of C# and many of the XR-specific namespaces that are associated with VR SDKs and toolkits. Scripting for VR has a few quirks that make it different from scripting for games and apps. We will take a deep dive into all the features we can unlock with C# that can improve our VR development workflow.

This is probably the most important chapter of the book because it focuses on one of the core components of development, unlocking the potential of the Unity Editor to create the most immersive experiences possible. To be clear, this chapter is not meant to teach you how to code. The sole purpose of this chapter is to teach you how to navigate one of the most technical aspects of VR development, which is software development. You will learn how code can be used to enhance the capabilities of your VR scenes and how input actions and Unity Events can be used to complement that workflow.

By the end of...

How does C# work in Unity?

C# (pronounced “C-sharp”) is a programming language that is commonly used in Unity, a popular game engine used to create 2D and 3D games and interactive experiences. Unity supports C# as a scripting language, allowing developers to write scripts to control the behavior and functionality of their games. In Unity, C# scripts can be used to create character controllers, enemy AI, physics simulations, and other features that make a game interactive.

C# is used in Unity for scripting game logic and behavior that can be used to create a wide range of interactive and immersive experiences in VR, and Unity provides many built-in classes and functions to help with VR development, including the following:

  • Interaction: C# can be used to create scripts that handle input from VR controllers, such as button presses and trigger pulls, and use this input to perform specific actions in the game or application, such as opening doors, picking up objects...

Creating custom input actions

As a reminder, input actions are specific user inputs or gestures that are mapped to a particular action or behavior in the VR experience. Custom input actions are an essential tool for VR development, providing better control, interactivity, and flexibility in creating immersive VR experiences.

We will create custom input actions that allow us to use all the buttons on our VR hand controllers. Once we have these, we can use code to trigger events using specific button presses.

To create a custom input action (Figure 8.3), go through the following steps:

  1. Go to the XR Interaction Manager GO in our Hierarchy.
  2. In Inspector, double-click on the XRI Default Input Actions asset.
  3. The Input Actions menu will open.
  4. Click the + button in the Action Maps section to create a new action map.
  5. Name it Custom Inputs.
  6. Select Save Asset.

With our Custom Inputs action map created, we can create custom actions we can pair to buttons...

Getting to know Unity Events

In Unity, events are a way to communicate between different components or scripts in a game. Events allow you to send a message to other scripts or components, without having to explicitly reference them.

Unity Events are defined using the UnityEvent class, which is part of the UnityEngine.Events namespace. Unity Events can be used to trigger actions, such as playing a sound, displaying a message, or changing a variable. Unity Events can be used to invoke methods, change properties, and send custom events.

Events can be added to a script in Unity by creating a public variable of the UnityEvent type and then using the UnityEvent editor window to add methods or functions to the event. Once the event is set up, other scripts or components can subscribe to the event using the + button in the UnityEvent editor window.

Unity Events are useful for creating a flexible and modular system in your project, where different parts of your game can communicate...

Creating portals with colliders and events

Using colliders and Unity Events to create portals that teleport the player to other locations on the map is important for VR development because it enables seamless and immersive navigation within the virtual environment. By using colliders to trigger Unity Events when the player enters a specific area, we can create a mechanism for teleporting the player to another part of the map instantly. This can help to eliminate motion sickness and other discomforts associated with traditional movement in VR, such as joystick-based locomotion or teleporting through menus.

Portals can also be used to create interesting and engaging puzzles, challenges, and experiences in VR. For example, a puzzle game could require the player to find and activate several portals to unlock the final challenge. Or an adventure game could use portals to transport the player to different worlds, environments, or dimensions. Additionally, portals can help to save time...

Switching between scenes with portals

Portals can be used to create immersive experiences by allowing players to move between different environments. Unity Events and scene management are important for VR development because they allow you to create interactive objects and environments that can be controlled by player input. With these tools, you can create portals, teleportation systems, and other immersive features that enhance the VR experience. By using Unity Events, you can make your VR application more interactive and engaging, which can improve user engagement and immersion.

In this section, we will learn how to create a portal in Unity that can take us to another scene. We will use Unity Events and scene management to achieve this. This section is essential for VR development as it allows for seamless transitions between different areas of the virtual environment.

To create a portal that teleports to another scene (Figure 8.14), go through the following steps:

    ...

Summary

We have covered various aspects of VR development in Unity in this chapter. We started by discussing the importance of structuring C# code and understanding its syntax to make it clear, concise, and easily understandable for others. We also learned about the Unity Event System and how it can be used to trigger events with input actions, which is essential for VR development.

Next, we learned how to create portals that can transport us to another scene in Unity. We achieved this by using triggers and colliders to detect when the player enters a certain area, allowing us to load a new scene and move the player to that location. We also created a prefab variant for our portal scenes to make it easier to add them to our game.

We then explored the C# code required to make the portals work. We created a new C# script called PortalSceneManager that handles the transition between scenes. This script includes a public scene asset, which allows us to select the scene we want to...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Enhancing Virtual Reality Experiences with Unity 2022
Published in: Nov 2023Publisher: PacktISBN-13: 9781804619537
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Steven Antonio Christian

Steven Christian is an Augmented Reality Developer, Animator, and Medical Student Student. He's also the Owner of Iltopia Studios where he creates comics and cartoons that explore the Black experience in America. He is Unity Certified 3D Artist and Instructor and his goal is to improve equity in arts and technology through content creation and community-focused education.
Read more about Steven Antonio Christian