Reader small image

You're reading from  Unity 2022 Mobile Game Development - Third Edition

Product typeBook
Published inJun 2023
Reading LevelIntermediate
PublisherPackt
ISBN-139781804613726
Edition3rd Edition
Languages
Tools
Right arrow
Author (1)
John P. Doran
John P. Doran
author image
John P. Doran

John P. Doran is a passionate and seasoned Technical Game Designer, Software Engineer, and Author who is based in Incheon, South Korea. His passion for game development began at an early age. He later graduated from DigiPen Institute of Technology with a Bachelor of Science in Game Design. For over a decade, John has gained extensive hands-on expertise in game development working in various roles ranging from game designer to lead UI programmer working in teams consisting of just himself to over 70 people in student, mod, and professional game projects including working at LucasArts on Star Wars: 1313. Additionally, John has worked in game development education teaching in Singapore, South Korea, and the United States. To date, he has authored over 10 books pertaining to game development. John is currently a Technical Game Design Instructor at George Mason University Korea. Prior to his present ventures, he was an award-winning videographer.
Read more about John P. Doran

Right arrow

Augmented Reality

Made popular with Niantic’s Pokemon GO and on Snapchat filters, Augmented Reality (AR) is a way of blending digital elements with the real world. Specifically, it is a technology that superimposes a computer-generated image on a user’s view of the real world, hence providing a composite view, meaning that both the real world and then digital elements put on top of it are displayed to the player.

In this chapter, we will explore how to set up our project to utilize AR for both Android and iOS devices and how we can customize them. This project will be a simple AR project in which the player can look at various surfaces in the game environment and spawn objects on top of them. The goal of this chapter will be to explore the basic concepts of AR and see how they can be used in a project.

This chapter will be split into several topics. It will contain a simple, step-by-step process from beginning to end. Here is the outline of our tasks:

  • Setting...

Technical requirements

This book utilizes Unity 2022.1.0b16 and Unity Hub 3.3.1, but the steps should work with minimal changes in future versions of the editor. If you would like to download the exact version used in this book, and there is a new version out, you can visit Unity’s download archive at https://unity3d.com/get-unity/download/archive. You can also find the system requirements for Unity at https://docs.unity3d.com/2022.1/Documentation/Manual/system-requirements.html in the Unity Editor system requirements section. To deploy your project, you will need an Android or iOS device.

You can find the code files present in this chapter on GitHub at https://github.com/PacktPublishing/Unity-2022-Mobile-Game-Development-3rd-Edition/tree/main/Chapter15.

Setting up a project for AR

Before we can start adding notifications to our project, we will need to add three packages that Unity makes available to enable AR for both iOS and Android devices. In our case, we are going to be utilizing both ARCore and ARKit to create our project, and the AR Foundation package to act as an intermediary so we can use both ARCore and ARKit while using a similar connection. Since this is a brand new way to create projects, we will actually create a new Unity project to demonstrate how to use it. Please follow the steps given here:

  1. To get started, open Unity Hub on your computer.
  2. From startup, we’ll opt to create a new project by clicking on the New button.
  3. Next, under Project Name put in a name (I have chosen Mobile AR), and under Templates, make sure that 3D is selected:
Figure 15.1 – Creating a 3D project

Figure 15.1 – Creating a 3D project

  1. Afterward, click on Create Project and wait for Unity to load up.
  2. From the...

Basic setup

Since the player can be anywhere when the game starts, we can’t use a camera in the traditional sense, so we will start by removing the original one. Follow the steps given here:

  1. From the Hierarchy panel, select the Main Camera object and delete it by right-clicking and selecting Delete or pressing the Delete key.

There are two key objects that we will need to create before we can start implementing our own features: AR Session and AR Session Origin.

  1. Right-click in the Hierarchy panel and select XR | AR Session:
Figure 15.6 – Creating an AR session

Figure 15.6 – Creating an AR session

AR Session is what controls the life cycle of any AR experience, which allows us to enable or disable AR features depending on the platform we are working on.

Note

AR Session is also responsible for telling you whether your device supports AR. For information on handling this, check out https://docs.unity3d.com/Packages/com.unity.xr.arfoundation@4.2...

Interacting with the AR environment

One of the ways that we can have the player interact with the world is by allowing them to spawn objects within the scene to help players see where items will spawn. We can create an indicator to show where they will actually spawn to. Let’s look at the steps to do just that:

  1. Create a quad using GameObject | 3D Object | Quad.

Quads represent a plane, the simplest type of geometry. In our case, we will use the quad as an indicator to the player where they will be spawning an object if they tap on the screen.

  1. With the quad selected, go to the Inspector window and go to the Transform component and set Position to (0,0,0), X Rotation to 90, and Scale to (0.2,0.2,1).

We made the quad smaller to be 20 centimeters long and rotated it so it could represent a floor better. We do not want these values to change but we will eventually want to move and rotate this object to follow our player when they move the camera. To...

Spawning objects in AR

The simplest way to spawn an object in AR would be to make it so that when the player taps on the screen, it will spawn an object where our Placement Indicator object is. But before we do that, we first need to make an object that we’d want to create within the scene.

Follow the steps given here:

  1. Create a sphere by going to GameObject | 3D Object | Sphere.
  2. From the Inspector window, set Position to (0,0,0) and set Scale to (0.2,0.2,0.2).
  3. Add a Rigidbody component to the sphere by going to Component | Physics | Rigidbody.

By adding the Rigidbody component, we are letting Unity know that we want this object to be affected by things such as gravity and react to collision events and forces being applied to it. At this point, you could customize the object as much as you’d like, change the mesh and collider, and so on.

  1. Go to the Project window, and open the Prefabs folder. Create a Prefab of our sphere by dragging and...

Summary

Throughout this chapter, you have learned how to utilize Unity’s AR toolsets to augment reality by adding artificial computer-generated objects into the real world. This new and growing technology is still being developed, and the skills gained from working in it will likely grow in importance in the future as things such as Virtual Reality (VR), Mixed Reality (MR), and other forms of Extended Reality (XR) become more and more commonplace.

In this chapter, you learned how to install ARKit for iOS, ARCore for Android, and AR Foundation for a multiplatform AR solution. Once installed, you learned how to set the platform settings for both iOS and Android AR development. Afterward, we did the basic setup to have Unity use its AR tools to allow users to add a simple mesh to the environment. We then built upon that to detect surfaces within the real world using the AR Plane Manager and learned how to visualize it by using the AR Default Plane object. We then learned how...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Unity 2022 Mobile Game Development - Third Edition
Published in: Jun 2023Publisher: PacktISBN-13: 9781804613726
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
John P. Doran

John P. Doran is a passionate and seasoned Technical Game Designer, Software Engineer, and Author who is based in Incheon, South Korea. His passion for game development began at an early age. He later graduated from DigiPen Institute of Technology with a Bachelor of Science in Game Design. For over a decade, John has gained extensive hands-on expertise in game development working in various roles ranging from game designer to lead UI programmer working in teams consisting of just himself to over 70 people in student, mod, and professional game projects including working at LucasArts on Star Wars: 1313. Additionally, John has worked in game development education teaching in Singapore, South Korea, and the United States. To date, he has authored over 10 books pertaining to game development. John is currently a Technical Game Design Instructor at George Mason University Korea. Prior to his present ventures, he was an award-winning videographer.
Read more about John P. Doran