Reader small image

You're reading from  Build Your Own Metaverse with Unity

Product typeBook
Published inSep 2023
PublisherPackt
ISBN-139781837631735
Edition1st Edition
Tools
Concepts
Right arrow
Author (1)
David Cantón Nadales
David Cantón Nadales
author image
David Cantón Nadales

David Cantón Nadales, software engineer from Seville, Spain, with more than 15 years of experience. He is currently a Technical Leader at Grupo Viajes El Corte Inglés, a leading travel company in Europe. He has done a multitude of projects and video games with Unity, VR/AR with Oculus, Hololens, HTC Vive, DayDream and LeapMotion. He was ambassador of the Samsung community "Samsung Dev Spain", and organizer of "Google Developers Group Sevilla". He led more than 100 projects throughout his career. As a social entrepreneur stands out Grita, a social network that emerged during the confinement of COVID-19 that allowed to talk to other people and help each other psychologically. In 2022 he won the Top Developers Awards organized by Samsung.
Read more about David Cantón Nadales

Right arrow

Preparing Our Player

After a long introductory chapter full of theory, we finally land on the fun part of the book. During this new chapter, we will learn how to create the main, most basic, and most necessary thing: the Player. Our player will be a 3D character able to move around the world we build, following our orders.

Using the keyboard and mouse, our character will be able to move forward, backward, and sideways, turn with the movement of the mouse, jump, run, and so on.

We will cover the following topics:

  • Creating a third-person controller
  • Working with colliders
  • Changing the default avatar

Technical requirements

This chapter does not have any special technical requirements; just an internet connection to browse and download an asset from Unity Asset Store. We will continue with the project we created in Chapter 1, Getting Started with Unity and Firebase.

Remember that we have the GitHub repository, https://github.com/PacktPublishing/Build-Your-Own-Metaverse-with-Unity/tree/main/UnityProject, which will contain the complete project that we will work on here.

Creating a third-person controller

One of the many advantages of using Unity is its large asset marketplace. Unity Asset Store is a place where you can find content created by third parties and also assets created by Unity, free and paid.

Unity has a multitude of assets called Starter Assets that greatly facilitate and accelerate the development of your project. In our case, we will use Starter Assets - Third Person Character Controller for our player. This player controller is already adapted to the new input system with which we previously configured the project.

To download and install Starter Assets - Third Person Character Controller, follow these steps:

  1. Go to https://assetstore.unity.com in your browser.
  2. In the search engine, type Starter Assets - Third Person Character Controller.
  3. Click on the single item that will appear in the search results.
  4. Click on the Add to My Assets button.
Figure 2.1 – Downloading Starter Assets - Third Person Character Controller

Figure 2.1 – Downloading Starter...

Working with colliders

As you may have noticed, the player walks on the floor that we placed in Chapter 1, Getting Started with Unity and Firebase. If you are a little naughty, you may also have noticed that if your player leaves the floor, he will fall into the void. This is explained by the fact that the floor that we placed is a plane. By default, this type of geometry comes with a component called Mesh Collider, which serves as a physical barrier so that any object that is placed on top does not fall into the void.

In order for one object not to fall through another or not to be passed through, both objects must each have a collider component. If you noticed earlier, when we created our player controller, a green capsule appears around the character. This capsule is nothing more and nothing less than a type of collider (Capsule Collider). By default, Unity includes one for us in our player.

Let’s do another test to learn more about how colliders work:

  1. Place...

Changing the default avatar

By default, Unity creates our character to look like a robot – possibly not what we’d like the most. In future chapters, we will see how to change the appearance of our player at runtime, as we play, as we would do in any video game. Now, as a preview, we will learn how to change the initial appearance of the player in a very easy way.

There is a web platform called Mixamo. It is a huge catalog that offers hundreds of 3D characters and animations ready to use in your Unity project.

As a mandatory step, visit https://www.mixamo.com and register for free or log in if you already have an account.

Figure 2.20 – Mixamo.com home page

Figure 2.20 – Mixamo.com home page

Once identified, let’s look for a character in the catalog to replace the robot avatar. To do so, follow these steps:

  1. Click on the Characters link at the top of the web page.
Figure 2.21 – Mixamo Characters link

Figure 2.21 – Mixamo Characters link

  1. Voila...

Adding Mobile Support

So far, our character is able to move around with PC controls, that is with the W, A, S, D keys, Spacebar, mouse clicks, and so on. But you may ask, what about on a mobile device? Here we solve it. Fortunately, Unity offers us an easy solution to solve the character movement with touch controls.

Next, we will add a step by step system that allows us to make movements with our fingers. To do so, we will follow these steps:

  1. We must make some modifications in the PlayerInstance Prefab, double click on the Prefab located in Assets | Photon | PhotonUnityNetworking | Resources to edit it.
  2. Unity offers a Prefab that practically brings this system, it is called UI_Canvas_StarterAssetsInputs_Joysticks and we look for it in the path Assets | StarterAssets | Mobile | Prefabs | CanvasInputs and we drag it to the PlayerInstance Prefab.
    Figure 2.35 – Adding Mobile Controls Prefab

Figure 2.35 – Adding Mobile Controls Prefab

  1. Now, we select the UI_Canvas_StarterAssetsInputs_Joysticks...

Summary

Throughout this chapter, we have learned how to create a complete controller for our player. Now we have a character capable of being controlled by the keyboard and mouse, with totally professional effects. We discovered how Unity facilitates development with Starter Assets that offer compact solutions to complicated tasks such as creating a controllable player.

We practiced collisions by placing a cube and a sphere in the scene with the physics of a soccer ball, which we are able to move upon collision. To enhance the player we created, Mixamo surprised us with its huge catalog of free characters ready to use in our Unity project.

In the next chapter we will learn how to design a scene from scratch, with buildings, decorative elements such as trees, pavements and even a fountain! We will create a wonderful little village that will serve as the main scene for our players to land in as soon as they connect to our metaverse. We will also make our first foray into performance...

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Build Your Own Metaverse with Unity
Published in: Sep 2023Publisher: PacktISBN-13: 9781837631735
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 ₹800/month. Cancel anytime

Author (1)

author image
David Cantón Nadales

David Cantón Nadales, software engineer from Seville, Spain, with more than 15 years of experience. He is currently a Technical Leader at Grupo Viajes El Corte Inglés, a leading travel company in Europe. He has done a multitude of projects and video games with Unity, VR/AR with Oculus, Hololens, HTC Vive, DayDream and LeapMotion. He was ambassador of the Samsung community "Samsung Dev Spain", and organizer of "Google Developers Group Sevilla". He led more than 100 projects throughout his career. As a social entrepreneur stands out Grita, a social network that emerged during the confinement of COVID-19 that allowed to talk to other people and help each other psychologically. In 2022 he won the Top Developers Awards organized by Samsung.
Read more about David Cantón Nadales