Car race with Virtual Reality
4.2 (3 ratings)
Instead of using a simple lifetime average, Udemy calculates a course's star rating by considering a number of different factors such as the number of ratings, the age of ratings, and the likelihood of fraudulent ratings.
37 students enrolled
Wishlisted Wishlist

Please confirm that you want to add Car race with Virtual Reality to your Wishlist.

Add to Wishlist

Car race with Virtual Reality

Build your virtual reality driving experience with unity 3D
4.2 (3 ratings)
Instead of using a simple lifetime average, Udemy calculates a course's star rating by considering a number of different factors such as the number of ratings, the age of ratings, and the likelihood of fraudulent ratings.
37 students enrolled
Created by Dessain saraiva
Last updated 5/2017
English
Curiosity Sale
Current price: $10 Original price: $20 Discount: 50% off
30-Day Money-Back Guarantee
Includes:
  • 2 hours on-demand video
  • 1 Article
  • 3 Supplemental Resources
  • Full lifetime access
  • Access on mobile and TV
  • Certificate of Completion
What Will I Learn?
  • Set a car in Unity 3D with sound and real physics
  • Set different cameras and allow the player to switch between them
  • Import an awesome race track with palms and lake
  • Set a countdown timer to track your fastest laps
  • Improve the project’s performance via Profiler Tab and FPS output
  • Make your Unity project work on a Virtual Reality Headset
  • Use a VR controller to control your race car
  • Create a GIT and upload your project
  • Configure and use Unity Cloud
  • Download and run the project on your smartphone
  • Practice a little for when you are on a real live race car
  • All the steps are fully described on the lesson description
View Curriculum
Requirements
  • You need Unity 3D installed, some basic knowledge is good but not required.
  • You need a virtual reality headset Google's Cardboard style.
  • A gamepad will be extremely useful (I am using VR PARK-V3)
Description

Hello, my name is João Dessain Saraiva and besides working as a web developer one of my favourite hobbies is virtual reality. To stay updated with this technology I like to create content for Udemy and I am also a virtual reality technical advisor for a british VR company.

So I always wanted to drive a race car on a professional race track, and since I didn't have the chance to do it  yet I decided to make a virtual reality project about it.We are going to build a race track in Unity 3D and import a car model, set 3 different player view perspectives and a countdown timer tracker to keep track of our driving skills.The project will be built on Unity Cloud and we will also apply some techniques to improve the overall performance, and yes for sure it will have virtual reality and gamepad support.

The lessons contents is fully described, and if you have any question don’t hesitate in posting it I’ll be here to help you out, let’s go!

Who is the target audience?
  • Anyone with basic Unity 3D knowledge
  • Students what want to learn about Virtual Reality or improve their knowledge
Students Who Viewed This Course Also Viewed
Curriculum For This Course
20 Lectures
01:55:38
+
Intro
1 Lecture 06:13

Hey, thank you so much for betting this course! It’s my third here on Udemy and it’s being great to make this projects and share it with you. Hope you have fun building your virtual reality race track, and don’t forget to share your projects with the rest of the group I’ll be here to help you out if you need to.

You will learn:
Set a car in Unity 3D with sound and real physics
Set different cameras and allow the player to switch between them
Import an awesome race track with palms and a lake
Set a countdown timer to track your fastest laps

Improve the project’s performance via Profiler Tab and FPS output
Make your Unity project work on a Virtual Reality Headset
Use a VR controller to control your race car

Create a GIT and upload your project
Compile your project on the Unity Cloud platform

Download and run the project on your smartphone

Practice a little for when you are on a real live race car

What we are going to do

We are going to create a Unity project and start by setting our car controller, to start we want to make it work on a desktop version. After that we will import the race track and set a countdown timer is also important so we keep track of our driving skills. When we are happy with our scene and car controller we will adapt our project to a virtual reality headset, I am using a VR PARK-3 and it’s controller it’s best if you have one like mine however if you get into troubles post about it so can figure out the correct configuration for you. Unity has a service called Unity Cloud and it can be really useful to compile our projects, we are going to set our project and use this awesome feature to compile this project, you can certainly use this in your future Unity projects.  

What you need to know and have

This is one of my courses that has less code,  however to complete this it’s important that you have a smartphone and a virtual reality headset to try the project unless you just want to use it on your screen or export it to a smartphone later on.   

I am using an Android cell phone, however the project should run fine in other devices such as Iphones because Unity allows to export our projects to many other formats.  

Preview 06:13
+
Basic scene and car
4 Lectures 32:55

Start Unity and create a new project, mine is already created and opened so we don’t have to wait for it to load, let’s start with the terrain GameObject -> 3d Object -> Terrain, we want it to be big so select it click on the gear and will set it to 5000 5000, notice that this unity is metrics this is duo the fact that the company that develops Unity originally started in Denmark however their headquarters are now at San Francisco, US.

Import the environment package so we can have some ground textures among other objects, we will go to the brush icon and Edit Texture and set the grass hill albedo texture,

Also add some random objects to your scenario, this way when the car moves you can have a better perception of where and how it’s going, painting the terrain is also a good idea.

Preview 08:22

We will download the Standard Assets wich includes a car model with the controller, open the Asset Store tab and search for Standard Assets and import just the car we won’t need the rest of the assets.  Open the Car scene, copy the car gameobject now back to our scene paste the object, press play and see how it rolls.

We will start by placing the camera slightly over the hood,  we can get a feeling of what the player will see by adding the ‘Simple Mouse Rotator’ script to the Main Camera object this way when we move the mouse the camera will also rotate.

This type of camera is called interactive, because it follows the car and the player can interact directly with the camera, for now it’s just with the mouse but later on it will be with the headset.

Car engine
06:08

Our imported car model isn’t the best to place the camera inside, as you can see when we do this … there’s nothing special to see inside so let’s import some free models with better interiors.

A quick search on Unity Asset Store give us some nice options, I am going to use the Cars Free - Muscle Car Pack from Super Icon you can find the link on the description.
Muscle Car - https://www.assetstore.unity3d.com/en/#!/content/79854

Now we are going to import the asset to our project, open the scene where it’s copy the gameobject of the car model and paste it back on our scene, got it ?

Since we want this car model to replace the previous one we are  going to disable the SkyCar game object and place the FireGTO model inside the Car game object.

You probably need to rotate it so inside the FireGTO game object change the Y from 0 to 180, press play and everything should go smooth.

If you press play you will something wrong, the car won’t move or will be on it’s side. What we need to do is to remove the colliders from our fireGTO models and they are inside GTO_Alloy01 game objects.

To finish, just place the camera inside the car and adjust as you see fit. We don’t have much light inside so click on Add Component and add a Light to the camera, also change the game object name to camera 1st person, click play and move the mouse around to get a feeling of what the player will see

Drivers camera
06:09

Let´s name the Main Camera, 1st camera, duplicate the game object and place it on the back of the car to have an aerial camera, rotate it a little on the Y axis.I removed the Mouse Look script from this camera and also from the camera inside the hood there’s no point in having them, now let’s write some code to switch between cameras.

We will write script that loads the cameras inside an array and when we press TAB it will change between cameras, if you are having troubles following the script writing you have the source code on downloadable materials.

Back to Unity IDE you can see the little circle on the bottom right spinning, that’s when Unity is loading / compiling the scripts wait for it to finish to see if there are any errors.

Drag and Drop the cameras to the Camera Controller script:
Element 0 -> 1st camera
Element 1 -> aerial camera

If no errors are found press play and TAB to see if it’s working.

You can change the camera order as you see fit, however for now this is best because it gives you a direct perspective of the game and objects inside the scene.

Camera switch
12:16
+
Racetrack
4 Lectures 22:55
Import Scene
04:47

Let’s write a simple script that will detect when the user passes by the start box, start the counter and stop the counter when the car hits with the finish object

Create an empty object called timer and write the following script, you'll learn more if you type instead of pasting it.

public class timer : MonoBehaviour {    

   private float time;

       void Update() {  
          time += Time.deltaTime;
         Debug.Log(time);
       }
}

If there are no compilation errors press play and check if it’s counting, now let’s control it.

Countdown Timer
02:32

Create a text by Game Object -> UI -> Text call it timerLabel.

Font size will be 24 and Pos X: 500 and Pos Y: 450 we can also change the Next Text to 0

Back to our game object add this:

public Text timerLabel;

timerLabel.text = Time.time.ToString("0.0");

Since the timer is working remove the Debug.Log and click on the timer game object and select the Text object we just created, click play and see if it’s good for you.

You can also check the UI object's position by going to the Game tab.

Countdown Timer - Show Timer
05:18

Create an empty game object called timer, inside it create a 3d Object -> cube and call it start, let’s place it in front of the car and make it long enough to cover the track. Inside the Box Collider panel check Is Trigger option, this way the car will not stop when it hits the object.

Duplicate this object, place it a bit in front of the previous and call it finish.

On the timer script we will set a new variable called timerActivated and it will be a boolean with the value of false, this way the counter will only start when timerActivated is true and will stop when it’s false, restart when it’s true again.

Now on the start and finish game object we will add a script that sends a message to the parent object ‘timer’ to start or stop the timer:

public class childCollision : MonoBehaviour {    

  void OnTriggerEnter (Collider other) {  
     gameObject.SendMessageUpwards("collision", gameObject.name);
   }  
}

And inside the timer.cs let’s wait for the message and control the timer according to it:

void collision(string objectName) {   
   if (objectName == "start")
         timerActivated = true;
   if (objectName == "finish")
         timerActivated = false;
}

If the timer works fine, select the start object and disable Mesh Rendered, let’s place the finish object a little before the car and also disable the mesh renderer, ready for the first race ?

Countdown Timer - Start and Stop Timer
10:18
+
Projects Performance improvement
4 Lectures 13:38

Let’s analyze our project regarding performance, since this will be a virtual reality project we want it to have a smooth user experience

This is a usefull script to count the FPS: http://wiki.unity3d.com/wiki/index.php?title=FramesPerSecond

Create an empty object, name it FPSDisplay and create a #c script attached to it with the same name, paste the code on the link it’s also on the description.

I got X FPS …, let’s make an experiment and disable the scenery game object, create a new terrain and copy the Sun object from the raceTrackLakeLevel to make things faster, as you can see there’s a major difference between a complex scenario and a almost empty one so let’s tackle our previous scenario to make the experience smoother.

FPS script and Scene Check
04:36

There are no secret formulas to convert a heavy Unity project that uses lot of CPU and GPU into a light and high Frames Per Second project. The best way is good written code, improved 3D models with few vertices and use only the visual effects that you strictly nee

This way, we will check the scene and see what can be removed and check the differences in the Profiler tab and FPS indicator.

First go to Edit -> Project Settings -> Quality if we change the Levels you can see there’s a significant difference in the FPS count and the in the Profiler tab we want that the green graphic stays low because the higher it goes the less frame rate the project has, simply this means that what the users sees doesn't have a constant update breaking the immersion of the project which is really important in virtual reality.

This scene is quite simple although there are improvements that can always be made:

If you click on the water game object you can see that the attached script is Water Basic, this is what we are looking for because there are scripts, click Add Component and type Water and also if you click on Shader->FX give those a try and if they run nicely on your device let it be. Also remove the electrical-towers, personally I don’t so much point in it… if you hide a gameobject the project will not waste its resources however it’s better to deleted because it’s still an object and it will occupy space in your final executable.

On to the next chapter to do some baking ...

Unity Tools and Settings
04:45

Unity as some tools that can help us improve the project performance overall.

Occlusion Culling, disables the rendering of objects that are not seen by the user camera because they are behind other objects and cannot be seen. Go to Windows -> Occlusion  and click Bake, this is one of those processes that takes time enough to make yourself a sandwich…

Then you can go to Window -> Lighting and click Build this will create a light map of your project  that saves some resources because the device won’t need to process to where some of the light is.

If you find more features that we can improve in this project feel free to share it with us, there certainly are, you don’t need to run this every time you export the project save yourself sometime and do it on a pre-release version.

At the moment Unity Cloud doesn’t support this features, but I do believe it’s a question of time until it does.

Occlusion and Lighting
03:01

Since we imported some assets we will have some clutter in our project that we don’t need, let’s start by cleaning them by removing this folders:

Folders to keep:
Standard Assets / CrossPlatformInput
Standard Assets / Environment
Standard Assets / Utility

Make a backup of your project before doing this!

Had 911 mb
Now have 711 mb

You can check the project folder size previously the cleanup and after to see how much you gained in space. Unity projects use a lot of space by default so we just need to keep it as simple as we can. 

Clean the project
01:16
+
Virtual Reality
3 Lectures 24:53

Go to Google VR page https://developers.google.com/vr/unity/download and download the Unity package file, if we were to develop to Oculus or Vive the integration is already there in Unity versions prior to 5.6

Import the package to your project and what we are going to is to open a scene: 
GoogleVR -> Demos -> Scenes and GVRDemo, 

copy all the game objects, let's go back to our lake scene create an empty game object and place them inside, this way we make sure nothing is missing.

Now let's delete what we don’t need:
cubeRoom
Cube
PointLight
Floor Canvas
DemoInputManager

Google VR has it’s own FPS counter, let’s keep it and disable the one we were using by disabling the FPSDisplay game object.

Once you have the first camera setted up press play and check how it’s doing, you should have 2 viewports running side by side, if no check if the older camera was deactivated, you can even delete it. 

Let’s paste the VR camera and disable the other two and also the cameraController game object.

Import Google VR SDK
08:19

We want to make sure our project doesn’t have any error that we can fix right away and the project version is prepared for Android platform.

Go to File -> Project Settings select and Android and switch platform, yes it will take sometime.

Before submitting a project go to Build Settings and add scene we will only have one, also change the platform to Android and you need the Java SDK to make a local build for Android, when you switch your platform to Android it will take some time, almost an hour depending on your computer hardware.

Local build and run
02:28

As mentioned I am using the VR park 3D headset and gamepad, you can get it at:
http://www.gearbest.com/virtual-reality/pp_364125.html?vip=1302584
(yes the link has a referral to me ;) )

To get our project working we need to change the car controller script.

GamePad configuration
14:06
+
Unity Cloud
3 Lectures 14:45
Create a GIT and commit
05:47

You will receive an e-mail when the compilation is done, or you can always leave this page open and wait for it to finish, the more consecutive compilations you make the bigger the waiting time will be, also Unity Cloud will detect when a new GIT commit is made and will start compiling the new files automatically.

Once the compilation is done (if successful) download the APK and email it to an account that you have on your smartphone, you can also login to Unity Cloud on your mobile and download it directly however I find it it be slower.

Once you got the APK on your cell phone ( you need to enable untrusted sources ) install it and see how your awesome work goes on your cellphone.

Unity cloud is good if you are building big projects or your computer doesn’t have much hardware power, however you can always go to Files > Build Settings > Build and Run to build the project and deploy directly to your cell phone.

Set up the project at Unity's Cloud
08:09

You will receive an e-mail when the compilation is done, or you can always leave this page open and wait for it to finish, the more consecutive compilations you make the bigger the waiting time will be, also Unity Cloud will detect when a new GIT commit is made and will start compiling the new files automatically. 

Once the compilation is done (if successful) download the APK and email it to an account that you have on your smartphone, you can also login to Unity Cloud on your mobile and download it directly however I find it it be slower. 

Once you got the APK on your cell phone ( you need to enable untrusted sources ) install it and see how your awesome work goes on your cellphone.

Unity cloud is good if you are building big projects or your computer doesn’t have much hardware power, however you can always go to Files > Build Settings > Build and Run to build the project and deploy directly to your cell phone.

Build an APK and deploy in your Android
00:49
+
Conclusion
1 Lecture 00:18

If you arrived here you must have your car racing inside your virtual reality headset!

Your review is really important to help me make better courses, because Udemy is not just about making money but also to improve our knowledge as instructors by practicing and learning from you, if you had any problem during the lessons drop me a line and I’ll be glad to help out. Thanks and xau!

Conclusion
00:18
About the Instructor
Dessain saraiva
4.5 Average rating
25 Reviews
199 Students
5 Courses
Web developer and Virtual Reality enthusiast.

Hey, my name is João Dessain Saraiva and I am developer from Portugal. I been coding since 2001 and playing video games since I was 8, started in a ZX Spectrum all the way to the last Xbox and even my master degree project in design and multimedia was an immersive ski video game that uses a Oculus Rift headset and a Leap device to control the movements of the character.
Udemy now gives me the chance to share some of my knowledge and expertise in Unity 3D, video games and Virtual Reality however I also intend to learn a lot more from the community and keep improving as I teach.