Learn Video Game Development & Program In C#! | Jack Donaldson | Skillshare

Learn Video Game Development & Program In C#!

Jack Donaldson, Game Designer & Programmer

Play Speed
  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x
24 Lessons (5h 8m)
    • 1. Course Introduction

      2:09
    • 2. Installing Unity & Visual Studio

      12:01
    • 3. Navigating The Unity Editor

      12:07
    • 4. Intro To Variables

      20:48
    • 5. If Statements

      10:21
    • 6. Loop Learning

      17:32
    • 7. Generating Road Edges

      7:00
    • 8. Adding Grass

      7:06
    • 9. Moving Our Car

      12:01
    • 10. Infinite Level Generation

      13:54
    • 11. Adding Side Collisions

      9:59
    • 12. Adding Road Objects

      11:33
    • 13. Implementing Hitting Objects

      10:42
    • 14. Adding Oil Spills & Speed Boosts

      10:00
    • 15. Side Road Objects

      12:27
    • 16. Object Spawning Chances

      16:46
    • 17. Bug Fixing

      8:38
    • 18. In Game UI Design

      24:07
    • 19. Creating A Main Menu

      17:47
    • 20. Creating A Death Screen

      11:38
    • 21. Implementing Death Statistics

      17:14
    • 22. Audio & Sound Effects

      24:56
    • 23. Building Your Game

      3:53
    • 24. Project Wrap Up

      13:14
18 students are watching this class

About This Class

Easily Learn To Make Amazing Games!

This course aims to teach students with no experience how to create a PC Game! This class teaches students coding fundamentals and game design principles through hands-on tutorials. Using the free game engine Unity, students can learn how to make a fully functioning game and how to create levels from scratch in order to make a customised game that is playable and fun.

Concepts Covered

By the end of the course students should have an understanding of the following concepts:

  • Unity & Visual Studio Installation
  • Navigating In Unity
  • Steps Required For Game Creation
  • Programming Fundamentals Including:
    • Variables
    • If/Else Statements
    • For/Foreach Loops
    • Arrays/Lists
    • Functions
  • Designing Obstacles For Your Game
  • Importing Art Assets
  • Creating UI & Menus
  • How To Build/Export Your Game
  • Implementing Music
  • Basic Game Design Principles

Is this beginner and kid-friendly?

This class is for students who are new to Unity and has no required knowledge but can still be undertaken by students who are at a more advanced level. The course has no adult themes and is suitable for all ages.

What You Will Need

  • 8-12 hours of free time (This is dependant on skill level and speed)
  • A modern PC / Mac capable of installing and running Unity (See specs here)
  • An internet connection to download Unity and Visual Studio

Transcripts

1. Course Introduction: Hello, Ron. And thank you for viewing of my course on game development in unity. So we're going to be covering a lots of different things in this course on. Eventually we're gonna end up building ah two d racing game that is infinite and ah, attract. Suppose scored distance. Those kind of things has menus, has audios. Ah will start off by taking you to install union visual studio. Ah, setting up the editor using it, navigating around through it. And then we will go through the programming fundamentals. So this includes things like variables fl statements for and for each loops, arrays, lists and functions. So hopefully you should have a in depth knowledge of how they work. By the end of it, if you don't even know any of those words, don't way. This assumes no knowledge on. We go through it right from the very beginning. After you flirt, there's programming fundamentals are you do that by implanting basic things into the game. I will then add different things like generating objects, adding grass, importing our sprites and textures and that kind of thing with the art assets, your Freddie's Arone. But we do supply free ones for you to use on everything in this course, you can do as long as you have a Windows or Mac or lineas computer. Um, and an internet connection. So I give it a try. It would be great to see ah, no knowledge needed. And we'll go through creating a game from the start to finish. Um, and hopefully teach some coding concepts along the way. Eso please make sure to follow me my other courses check them out. I do have another course which doesn't have, ah, any programming in it if you are of that aspect, um and also you complete the class project, upload the end on and share your game with everybody. So hopefully you should have a built game that you contend your friends without needing unity installed, and they can play the XY far away. The application filed however you need it. So thank you. And hopefully you enjoy the course 2. Installing Unity & Visual Studio: Hello, everybody. And welcome back to these first episode for learning to code with C Sharp. And we're going to be creating games as we learn the different elements. According Ah, And through this, we're gonna use unity. So which I'm pride in, The first thing we're gonna do is open up the Internet and I'm just going to get you to download unity three D. This is a free program, and a lot of professional game development studios used this to create the games. So you can just head over to Indy dot com or you can just open up Google and search unity, and it should pay the first result there. So once you were on the main page, it's pretty simple. We're just gonna pressed this get started button on the front page, and then it should take us to this page. You do have optional pricing subscriptions you can pay. Ah, the main differences between ah, this is that you get support and you can also have, um, different revenue features over the free version. You can, um, have revenue below $100,000 a year, and then for each different plan, it will go up in the amount of money that you can make. So we're just gonna press get started free, right? Him And it will take us to the start creating with unity Page. Ah, England gave the start. Here will go here. Um, I'm just gonna could go here, and it will take us to this download page. I'm just going to accept the terms of conditions I've read through earlier. So just make sure you've done that. And then I'm just gonna press the big damage button. There also versions of this for Mac and links if you do require that. So once that is downloaded, you can just run the set up, so I'm just going to open that now. Um, and you would just click. I agree. Ah, you will select your in store location, um, and then click being still. But, um, I have already done that, so I'm just going to click Cancel, But usually you would just click install. Once unity hub is installed, you can just open it up. Uh, okay. Here we go. You need help. So there are lots of different versions of unity that the development team will release with new updates in features. So uni hub is just a central way to manage all those different versions of unity. So you can head over to the installs section Yours majors look of it in your or different. But as long as you can find the installation section Ah, this is where you install the different versions. The unity I am going to be using. Ah, the regular. You need 2019.2 point six F one so you can just select that version. Or generally speaking, any of the newer versions, if they're available, should work as well. I want to do that. You're just like next. Um, I am going to take clinics and Mac build support, and I am going to be using Microsoft Visual Studio to edit my code. And I highly suggest that you do, um, you can either take this box here if you want to install the idea, or you can open up the Internet and go to visual studio. Don't Microsoft dot com and just click the download vigils. You impressed the community version. This is free for individual developers and people who aren't doing it to make a profit. Ah, you can, of course, pay for later versions with more features. So once you just click community here Ah, you can just download it and run the installer. So this is just an idea. That's way a When I say I d a i d I just mean a program that we can use to edit our code. Um, and it will help us along the way. So I highly suggest that you do use visual studio. Um, it's pretty easy once you just open the X A Ah, you can just click. Continue. Um, I'm not going to do anything because I already haven't installed. Ah, but just install that, um or you can optionally just take this box here. I suggest getting it from the Internet, though, because this box will only work for a chimney version. You have to keep installing it every single time, which is a pain 20 click that just click the done. I have selected Linux and Mac, which means that we can export our game to those platforms when we need to, so just click done. And you should see that in a moment it will pop up here. The newer version and it will just download. This may take a little while, depending on your Internet speeds. So just be wary off that. Eso I'm just going to quickly skip ahead and wait until that is installed. Okay, So the unity version 2019.2 point six f one has just finished downloading. So what I'm going to do is just head over to the projects tab. Now, mine is obviously full of previous projects that yours won't have any here if you haven't used you need before. But I'm just going to click the new button. Ah, and this whole allows to create new projects that this is just a combination of all our different files. And essentially, you would create a new project for every new game you're going to do. So currently, to start off with Wedge is going to be doing a two d game, so I will select the two D template, and I'm just going to give my product name, and I like to keep mine civil son just gonna call it project code because we're going to be learning to code in this. Ah, tutorial. Siri's. And I'm just going to quickly adjust my location so that it is saving in the right spot just on that end. And then I'm just gonna go ahead and click the create button. Now, this may take a little bit depending on your computer, a speed, so hopefully it won't take too long. Ah, Whilst we're waiting for this to happen, I am going Teoh, be downloading some free Our assets that are available. You can use them in commercial projects as well. Ah, this is a wonderful website I've found that has lots of different assets for games in case you need them. And they're really, really good for getting started. So I'm just going to open up Google and we will search Kenny assets and he go. The first result is Kenny dot nl. This is the right one, Ulqini. However you want to say it and I'm just going to scroll down and select two D and I will just search for the racing pack. Ah, caps lock is on there and hopefully racing back you ago. It's just appeared now. Ah, this is just a set of different images or sprites. They're just different words that I will use to describe images for games. Um, that we can use in our game to test different things out. Click this download bun. So this will download a set of sprites or images that we can use in our game. So I will quickly then open up at the folder where I have saved my unity project. Um, and in here is the files that unit has created, So it will have an assets library, logs, packages, predicts headings and temporary. Now, I am worried about the assets photo considers where all our folders, all our files, will be accessible within unity. So it needs to be a in this photo specifically. And I'm just going to quickly create a new photo and just call it something a long line of sprites or images. And if we just open that up there will say we have our blank photo, and if I quick I'm just going to drag the is it far we downloaded into this sprites follow , and I'm just going to extract it here. So this will you need to make sure you unzip the suit file and inside of this should be the P and J um, photo. Now we can just delete everything else because we really only need the Panji images. Um, which should mean that we are good to go. So as you can see, it's put up into a few different photos characters, cars, motorcycles, um, and obviously the objects and tiles. So they will be accessible inside of unity. So I'm just going to quickly wait for the predictive finish. Okay? Now, you know he should have opened. If you haven't opened it already, you can just reopen Unity Hub and Select Chul Project. So now it's opened. You may have seen a dialog box appear, and it would say something along the lines of importing sprites. That was just happening when we added our racing pack images. So in the next video, I'm going to teach you how to navigate in unity. But just to confirm that how images were imported correctly. If you don't have the same windows as may, just click window layouts default, and it should fix everything right up again. And you should have the same as may and then down in the bottom just quickly. If you go into the sprites Ford we made, you should see a pay and J Fota, and you should see the different folders we have imported, including the cars, ones and so forth. So I will end up with this episode so hopefully should have unity and stored visual studio installed and downloaded the racing packs properly. So head on over to the next video to see how the unity editor is laid out. If ever you encounter issuer throughout these videos, just head over to the course page. Here you can follow me for updates. Ah, you know, Let's go down to the community section where you can start a conversation, ask a question or share your project in here. I'll try to reply as quickly as possible on and solve any of your problems that you may be having. I will also put up at download the full project. If you were really stuck, you can also save the course so you can come back to it later and please try and share it if you can 3. Navigating The Unity Editor: Okay, so now we're just going to learn about the editor and just I'll give you some tips for navigating around it. So up the down the bottom here we have lots of different windows, so you can easily change and move the windows to however you seem fit. So if I wanted my hierarchy down here, I could just drag it down. I'm not gonna do that, though. Um, is it can see they're just popped out because it was separate. So you can move. Ah, this around however you see fit. If you ever do get at looking weird like you don't want it, you can always just press window layouts and default. And it would just reset to the regular, um, layout. So the first window we have is the project window that this is like your file explorer on Windows or Mac, Um, and it just allows you to navigate through your projects files. So if I right, click here and click showing Explorer, this will actually bring up the same folders in my Windows File Explorer. Um, but as you can say, if I clicked on scenes here, there would be a sample seen in my father's poor. This is pretty much identical to it, Um, and there's no different. So it's just a fancy fall Explorer inside of unity. So to go back, you can just click up here, or you can click on the sides to navigate this well, pretty easy. If ever this isn't showing for the Assets Window and it may be on the packages, just make sure you click on assets the packages. I'm not going to help into too much at the moment about they had just custom add owns that unity allows you to install for each different projects that help you when making your games, but we're not going to touch them. For now, um, the other window we have alongside the bottom here is the concert one. So if you just click it, it will open it up. Now, I will explain this mawr a bit later on, but you should always know how to easily never get to this, because we will use the console to send a lot of messages, um, to us so we can see if we're having issues where they are end what the problem is sin, What stage of the game is causing this issue. Um, I tend to have clear on playing ableto What this does is every time you click the play button which runs your game, it would just clear the console from the previous test so that you don't have older. Is there confusing you? You can always pressed a Cleveland as well. If it does get to full on the left Here, we have out seen. So our scenes, um, like high levels, Um, generally in unity. Um, these scenes contain lots different objects, immunity, a call them game objects. So currently we have one game object on its called on main camera. So fighters click it, we could select it and we'll see. This is our camera. This is ah, what we see in the game as in the game view. So if you actually want to see what at the game will look like you just can click over to this game tab and we'll show you exactly what the player is going to see. Ah, when they play the game. But I'll just quickly go back to this scene view. So once we've selected the main camera game object, um, you can see on the right hand side this inspector window, so h different objects. You can add different things to it. So this one has a camera added to it. So that's the actual bid that does. The rendering has a transform, which is the actual location in three D space. So if you're familiar with programs like three D modelling programs like blender or Mayor Ah, then you'll probably be fairly confident in moving in this three d space. But as we're doing a two d game, it is only two dimensional anyway, so just as an example, if I was to move the exposition, you can see that it's actually moving the camera to the right. And if I went into the negatives will move it to the left, Um, and X is across. And why is just up and down, Um, again, Zed, we're not gonna worry about that more for three D space. So would just leave that for now. Um, each component has a lot of different values, which I'm not gonna go too much into at the moment. Um, when we sit out the game a bit further on, I will explain that a bit more. Amoros has an order, listener. This just allows any of the audio in the level to be played through to the players so that he can hear it. So if we would have disabled this, that would actually be no audio in the game. Um, it's also used a lot more for three dimensional sound. So, um, it would scare off the volume of the sound, depending on how far away the planner is from it. But as we're doing it to D came into some manner as much. Anyway, there's also this ad component button, which allows us to add lots of different things to it. We're not gonna worry too much about this, but you're gonna have a lot of different things, like gliders or again, camera components or your listeners. There's a whole bunch of different things we can actually add to it. So that's the different objects. If we wanted to add more objects, we can just right click in this hierarchy, and we would see there's three D objects to the objects, effects, lights, audio video you are, and a camera we can add so into the objects we could Adam Sprite, which would actually have nothing assigned to it. Um, we can change the name appear automatically puts position near a cosa. I could just change of active series the reserve, because it's got a sprite render on. What I could do is see where it says Sprite. I could click this right to this circle to the right, and it would open up a window that allows is to select the Sprite. And because we have our sprites or images from our assets back like would select car black . And you could see in the game view that there's the black car in this center off the screen . Um, speaking about this scene view to navigate around it, we can hold middle mass to, uh, pan, and we can hold right click to Ban as well. Mess well to zoom in and out and left Click selects things. Usually I prefer to select things just individually in the hierarchy. Also, when there's lots of different game objects, we can actually search terms. If I want to search for news pride, it would just search through all the game of drinks and show me the one I wanted as well. There are also lots of different modes. When selecting objects, for example, the handle is permanently for panning. Ah, we can use the move tool which gives us the ACSI survey of rights and left axes as well as the up and down ones. And holding this blue box allows us to free hand. So that's just for movement. Moving things are generally speaking. I will use the, uh, arrow keys to, um change between these different tools. So I you can, if you're less confident, click up here individual each time. There's also the There's the Rotates Tour, which we can use to rotate it, um, different angles. And then there is the scale tool which allows us to make a bigger into in direction. So come in your wider and we can make it longer if we see fit. So I'm just gonna resize this properly and there we go. There is also the rectal, which allows us, which just draws a rectangle around whatever we got selected, so we can change it to be easier. That's producing scale tool, but it's upto you how you want to use it and which one you prefer. And then there's this new one which is, Ah, combination of all the tools which tries to me easy to use if you want. A lot of people tend to stick with just this one. I personally prefer the individual ones but allows you to rotate and move the same time, which can be helpful. Um, and then the next window we can look at is the game window. Which is it? Eyes talk better before it. Some what we like to see as a person, I It's what the game is gonna look like when it runs. Ah, we can select out different aspect ratios if we want to. I have added some customers. You can just do this by pressing the plus and give you labeled, setting the type and the width and height. I tend to stick on my reference resolution of 12 80 by 7 20 just to make things easier, Um, and consistent, you'll see a bit later on. When we do, you are work. Why that is so. The next buttons are the plus pause and thes step on. I mainly used to play in pours bun, so once we click the play, but what it does is it starts running the game. Uh, this can take a little bit for the first time, and it will search automatically to the game window and the game will be running. As you can see, there's not much happening, so it's a bit difficult to see. But if I would add, say, a rigid body, ah, which adds physics to the object, you would see it starts to fall somewhere is when we get when we remove play. Any changes that were made, it went whilst in play mode get taken away, so this rigid body is no longer there. Where is it? Added it. Now you can see the game isn't actually running yet, so the object isn't falling. But as soon as I get playing, um, it's force and that leads me on to the next bomb, which the board. But it's pretty self explanatory, but it just pauses the game, and these step on would just move frame by frame whilst the game is paused. If you need to do that and to slowly find a special moment to find issues. So if I just samples that I will quickly remove this rigid body by just clicking this cog and clicking. Remove component. Um, and also handy tool is for three reset button on the transform that just says it back deserves there is er and a scale of one if needed. Andan There is also the assets or this is ah, a online store where people publish different game development assets like models or textures, and you can buy them or you can get free. I'm going to stay away from this because we're not gonna be using it. Um, but it is a very handy tool if you want to go down that path or if you're just beginning. So that's the basic loud of the unit he Editor, um, you can always save the project by con France, save, protect or save the current seen by pressing control s or safe, Um, and that will save the scene. So if you head on over to the next video, we'll start learning about out variable times 4. Intro To Variables: Hello, everyone. So now we're going to be learning our first basic hurting principle. So this is variables. So I'm just gonna stop by, um, explaining water variable is and giving some examples of it, and then we'll head on into unity and right, our first few lines of code. So the first thing is, what is a variable? Um, are good. Different variables are an interesting concept. Uh, you need to really, fundamentally understand what they are because they used almost every line in programming. So a variables are like a location for a computer in memory to hold information. Um, you Their sole purpose is to label and store dollar in memory, Basically a za that definition says programmers use them for any information that they want to hold or use in later lines of code. Um, and you see what I mean a bit later on about that. So, for example, a variable that we would use, um, would be if we needed to store someone's age for use. Um, So, for example, if we ask the person with the ages and we want to, uh, you tests were keep that in memory for the lines after we would have to store that input from the user into a variable so we would store it in saying age variable if that was the case. So variable is just a thing. Um, that is that is a local that represents location of memory on beacon. Give variables whatever name we want, Um, to make it easier for us program is to work out what they hold in. Ah, access and Ryan out code. Probably. So. There are different types of variables, um, variables in Seychelles, which is the language that unity users, um, needed time. Um, and we have to when defining the variable, specify won't type that's going to bay. So if we need to store words, letters or text, we would use the string Darla type, um, in more simpler programming languages, you don't have to specify data types, but in say, shop, you need to, or else you'll get errors and your code won't run. So if we need to store the name, we would someone's name, which is made up of words and letters, um, we would use a string data type for that. So the most here, the few most common Darla types that we will use in, um, say Shop and just programming general. So the 1st 1 is string, which I just talked about before. So that's your text words or letters, so an example of that would be a person's name so we could set John to be the nay. The john would be need to be a string variable type, um, and usually to represent a string. We put the letters in quotation marks, which says that it's going to be a string. So the thing with this, which can be a little bit confusing, is that numbers can also be a strength if they have quotation marks around them now. When this happens, uh, the computer doesn't recognize him as a number, just recognize them as like a text, word or letter. So you can't do addition or subtraction or any kind of mathematical formulas on them until you turn them into a number started type because it thinks that it's a word, a letter or a string. So just keep that in mind because issues can arise with that and things can still be his string. If they have quote numbers, can still be a string if they have quotes around them. The next data type is an indigent, so images are whole numbers. At 123456 Bubba Bubba, they can also be negative. And example of this could be a person's age, which would just be too now. If that, too, was in quotation marks, then it would be a strength because it's not represented as a number. But in this case, because it doesn't have quotation marks around it, it is an integer now, the last night of type that we're gonna be using a lot of is floating point or float. A lot of these do have contractions to make it easier and cutting. So indigent is just into, and a floating point is just float, so floats floating point numbers are pretty simple. They it just decimals. So 1.23 point five negative four point to anything with a decimal point decimal value is considered to be afloat. Um, an example of this is a person's body mass index 26.5. Basically any number with a decimal after it is considered to be a float, and we will use a combination of lots of different data types throughout our programming. So an example, um, of cutting in of setting available would be string. Using him equals Jack. So I'll walk you through how this works. Um, the first thing is string. So this is so this actual line of code would be used in a script or unity, so we would actually write this as it is. So the first thing that comes when defining a variable is the Darla type. So we have said strength, and then we put a space and we could give it a name of whatever we want, so I've just called it using them. Um, usually we try when giving variable to name. We trot. We don't use, um, numbers. Um, if we are going to use the number, we would type it out. So instead arriving the number one. If we needed to say his name one or something we would use use name. Oh, any because we prefer not to use numerical values in this. And this all has to be one word. So if it's gonna be multiple words we would use, we would not put spaces. We would just do a capital letter for the start of each word. Um, Sophie did first name would do first all lower case in the name with the capital end. All is one worked. If you wanted to go that route, and then we do place that equals. Now we are setting this variable straightaway. Um, usually you could end the line here. So whenever we end line and say shop, we use a semi colon. So we could just put a semi colon here, and then that would just define a variable is used name, but it would not have it set to anything if that was the case. So that's why we used the equals here. Um, and we're sending it to you. Need to be string. And so we've got quotes around the word, and I'm just sending it to Jack. Um, now, anything in the quotes could work here. Um, and it doesn't matter. And then we have our semi colon to end the line. So that's setting a defining the variable of type string or text. And we're giving you the name use name and was sitting equal to Jack. Now the next line is in age equals 23. So what? We've got is our data type, which is an inter job, but we've shortened it because that's the way a sea chart likes it. So we've written into which means a whole number age, which is the name we've given it, equals 23. And then we've got the semi column because it's the end of the line. So that's just sending available age equal to 23 it has to be a whole number. So if we put 23.1, it would not work. And then here it looks a bit complicated. But bear with me. So debug dot log is what we're using. You need to print to the console so that we can see a message. So don't worry too much about this. The way it works is it'll do sprint any, um, anything within the two brackets here. It's, but it has to be a string because it can only print text. So the way this works is we've used the variable using them, so that appears to find appear. So this would be Jack, um Then we're adding another string to the end of it. So we've got is now I've actually purposely put spaces before and after the is so that it formats of string properly and then because is an integer. We have to use the available age, but this would not work. It would not add them together because in order to add variables together, we have to. They have to be of the same type. So we're using. We're turning it into a strength. So this basically essentially puts quotes around the 23 and just, um, yet makes puts quotes around it. So it's considered to be a string. It is more complicated when going from a string toe when going from text to a number, but we'll talk about that later on if required. So then this would print. Jack is 23 without the quotes as in here, so hopefully that's in all right. Explanation. Try another example. Ah, so he would go string Side one equals 10. So the dark type of strings are need to be text, Which is why we've got out quotes around it. Um, and we've called the variable side one, and we've just done the same thing and called it side to which secret of five. And then if we tried to debug Love Side One, and then this asterisk is times so this would times that to number two things together. Um, if we tried to do this, we would given error because these air strings and because they are recognized by the computer, is words or letters. They can't be used mathematical operations, so this would not work and will given era. So we would first need to turn these into images in all of for them toe work, which is a common issue. And you need to try and understand this because they have both words, not numbers, because they have courts around them. They can't be added or subtracted or times together. So this would this line would result in error, and your program would not run. Okay, so I would jump into unity now handle skin will try. Toe used some variables, including Okay, now that we're back into unity, we're going to create our first script. So a script is just a portion of code that we will weaken then out onto game objects if we need to. So first off, what is going to right? Click here and click create a photo in our file explorer, and we're just going to call this scripts. Um, this you will have a lot of scripts, sir. It's good to keep them organized. And I'm just going to right click again Click Create, and we'll go see Sharp script. So now I'm just gonna give a name and we will call this, um, my first script. Um, now that we've done that, I'm just going to double clicked open it and visual studio will open, so I just quickly wait for that open. Um, and if this doesn't open, probably then you probably have an issue with the insulation, and you can check back in the first video onto how to install it properly. So I'm just gonna visuals. Juror is now open, so just quickly drag it over here. Now, I will just increase the text size Teoh, play something that's easy to rate. Now, this is really scary, and orders run you through what's happening. So first things first. Semi colons need to be at the end of every line. Um, that's a say shop thing. And so the 1st 3 lines here, um, live libraries are collection of code, um, that allow you to reuse code very easily. So we are just met saying to the program, Hey, we're going to be using thes sections of coat and we want access to them. So basically, don't worry too much about this, but these is just saying, Hey, we want to be out of use all the features that unity can provide. Tow us. Um, and we worry about this a bit later on, and I'll explain in more detail later. Now. Ah, the next line is public class. My first script column money, behavior. So public means that, um it can be accessed by other scripts, So we should always leave things as public. For now, I'm class. Don't worry too much about this, but this basically says that it's it's gonna be a script. It's gonna be a class that we can code in, and it will be separate from other scripts. Um, and this is followed by the name of the script and then a column on our behavior. This is just required for unity, um, and says that it can be added onto game objects and will confined to the rules of that. Now, double slashes are comments, so these are lines that we, as developers use to, um help us understand how code if other people looking over it. But these lines are just get skipped over and don't get run by, um, the program at all because of double slashes. So these air just merely to help us and explain things. So this, um, this doesn't do anything. It's just to help us. So it says style is called before the first frame update. So the way this works is everything within these curly braces will get run at the start of the game. Well, when this script is first started, so don't worry about the way this works. I'll explain the functions later, but we're just going to do a few about variable things first, and then we also her updates. So everything in the curly races will get run every frame which generally you have 60 frames per second depending on the speed of your computer. So remember, I'll start funked out, start everything inside these start curly braces will get run when we first from the current program. So we're just gonna be using here for now. Um, but first, I'm just going to so everything within these color braces, um, is for out scripts that we need to make sure everything's inside there as well. So to start with, um, I'm just going to to find a pretty easy variable. Um, and we're going to do the type of strength. So this quarter's two unnamed variable, so go strength. So let's remember, it's a Darda time will call it name. And oh, just put the semi call in there. So as you can see, registry, does putting a crane underline? Um, don't worry about that, but visual studio tries to help you with different bits. Now, Um, what we could do is say name equals lost right away, but we're not gonna do it. Um, you know, start function. Um, which is just in between her curly braces work we can say name equals on. You can just put your name in here and remember semi colon at the end of every line which make, which is this year will pick up on and we'll say semicolon expected. And if we just save the script, um, we can. So what will happen here? His name will be Eagle Jack. And to test this, we can. The next line we can write debug don't log and Would is right name. So this debug log Remember Prince to the console, and it will print our variable of name. So if I just go back into unity first we need to attach our script to a game objects. So if we click on main camera and we can drag out script over all, we could click Add component in search my first script, whichever one is easiest. Now, if we run the game by pressing the play button, we can go over to the console window and say it printed out Variable name. So that's good. Um, and you see that if we go back and we change it to, um, something else and save it and press play, you see it printed what? I changed juice and outsprinted Ryen. So to make it easier to test, I'm just going to copy this code and put it in the update function. And if we go back and press play, you'll say it's printing. Ah, the message, every frame. So what we're gonna do now? I'm just going to up the top when we declare our name, I'm going to try out the words public, um, before available a variable definition. What this does is allows a variable to be accessed by other scripts and unity. So now if we go into unity, would just your say in the bottom, right? It has the name field, so this means we can easily change it to whatever you want. So I'm just going to, ah, change the code slightly here and we'll say, Name defaults to Jack. And in the update function, I will remove this line and would just print name every time. And we'll see what happens now. So I'll just clear the console and press play. And you say currently is printing nothing because, um, it will keep whatever you said it to in the inspector window here. So if I change it to Jack, you see, now it's bringing Jack. If I change it to Ryan, you're seeing now it's printing Ryan, so you can now easily change the variable whilst the game is running toe. Whatever you want, this is extremely helpful. We're testing, and usually you can't. You can't do this in hurts a sharp normally. But unity allows you to do this which is extremely helpful. So I'm just gonna unplayed here and we can open up visual studio. So hopefully you have, Ah, simple idea of variables and obviously out Prince statement as well or of the above dot log ones, which send a message to the screen to the user. So hopefully in the next video, we can go into a bit more depth and learn about if statements 5. If Statements: Okay, everyone. So now we're going to be learning about if statements eso I'll explain this in a minute. But first, what we're going to do is just set up, out seeing a bit neater and nicer. So we will click file and I'm going to click new scene. We can save the common one, and then I'm just gonna click fall safe and we will give us in name. So I will just say main level and we'll save it in the scene's folder. There we go. Ok, now, if it's like the main camera, I'm just gonna add my first script back. And there we go. And then if we go to in a file, Explorer will go down to assets Saints and I'll just delete the old sample scene. Okay, Now what we're going to do is actually I like, I will remove my first script from there, and we'll just right click in the Iraqi quick, create empty and would just call this game object game controller. So and I'll just reset the transform two zeros Ozer, and had my first script onto this one. Nice and easy. Okay, so now if we open back up my first script. There we go. What we're going to do is just remove currently what we've got and we'll save it. So to start with what we're going to do is learn about if statements So what? The way they work is you will first write if and then we have opening your brackets and then we would do a condition. So what would you say is if one is greater than two and then we do our curly prices. And so the way this will work is with it would go if the condition in here is true and then we'll run this code. So I would say, if one is great into which it isn't, then it would run this coat. Whereas if it was, if to was greater than one, so too is bigger than one. Then it would run this code so this code will get run. Now, this isn't just hard code of values. This could be fair, was so we could say, like if name was a good Jack, then we could run this coat. And what you can also do is you could say else and then do color braces. You could so the way this will work is so this, uh, this would get run if two is greater than one. And this would run if two is not greater thumb one. So, basically, this will get run when this doesn't So you could say if name was equal to Jack, then let him in else, then. Obviously the name isn't a good act. Then you print a message saying Go away or something like that. Where you can also do is you could changes to be else if and continue down the cycle and add another condition, another condition of the conditions so on, so forth. So I'm just going to delete this and we will use this in a minute. But to start with, what we're going to do is we will say public game of Jet. So this is our data type for game objects or things inside of unity. So I'll explain this a little more in a minute, but you'll just have to trust me. And then we can call this one, say, red car, and we'll just send me, put a semicolon, and then I'll just do another public game object and we'll call this one black car. Okay. And then in the start, then we'll also do another variable. Sorry. Public string. Ah, chosen cop. There we go. Okay. And then what? We will say if chosen car is equal to so for if same is we if we're doing equal, we need to do double equals. That's the way ch out works so well. Say, if since chosen car which the string said I mean, we need to have quote so it can only be something in quotes. If chosen cars equal do will say red. Then we'll run this code else. If chosen car is equal to black, then we'll run this code else. Otherwise, if it's not a quitter rental back then we'll just print a message saying Sorry, no cause of the boat. So remember this just prints a message inside the brackets to the console. So the way this works is if l string for German cars equal to read will run the Konia. Otherwise, if it's equal a black will run the corny else villages print sign it has available. So what we're going to say is, if chosen Kaiser will read will say game object, don't Instead she ate. So this is, um, a some code we used to create an object into the game. So you uses a lot. Um, And the way it works is if we open our brackets now, we need to tell it what we're going to create a copy off. So if it's so, what we're going to say if the chosen kinds he could read will create a copy of Red Car and we'll just close the brackets there and I'll just do the same thing in for the black car. So it was a good a black car would just create the black car. Okay, so that should be a law code we need for now. And we just hope back onto unity and we'll go to us also. We're going to file manage victor assets now, understand a crane. Your photo will call it prefect. So the way this works is currently in house frights. Photo. We have all of our different pictures, but these aren't considered to be game objects yet, so we just need to make them into game objects. So first will make l black car. So to make it into a game addict would is dragging into the scene, and I'll do the same without red car. So now these game objects, because they have a transform and the in the same and we're gonna never get back to a previous for a and we're just going to make a copy of the so if I'm just gonna name this something nicer So this could be a black car, and this one could be red car, So to make it so we just want to easily make copies of them. So we'll drag this into pre facts and there we go. So what did the same for a car? And this means that we could make a lot of different copies off them if we need to. It also means that if we added the base one, all of the others will change. So what I mean by that is if I just move all of these other cars, um, so you can see them and we went to change the red cars, um, pitcher to say the green one. It would change all of the other ones in the scene, which makes it extremely helpful for changing lots of things at once. So If you wanted to say change your players appearance, you could just two in one spot and it would change everywhere else. You use the prefab Pitiful. So that's what progress for our just change back to read there quickly and will delayed all these ones in the scene. So now we have both of these in the pre fabs follow would just select our game controller and would just drag our red card game object of here and our blank art gallery over here. So this is just setting these variables. Remember these two variables equal to these two things? And now if we hit, play and go to the console, I will say Sorry, no cars are available because the chosen card is an able to read or black yet. But if we put in red and we hit play, there we go, the red car appears and the same thing should happen if we put in black. There we go. OK, so I'll just quickly run through. So remember, we have are two references 12 pre fabs, which there are copies on that we can make off the red and black car and then we have out string or out text off the car we want to create. And then in our start function, which means that all this code gets called when the game first runs, we say If the chosen car is the crew read, then we will. Instead, she ate or create a copy off the red car. Otherwise, if the chosen cars equal blank, then we will create a copy of the black car. Otherwise, if these two above aren't true, then we will run. This will print the message. Sorry, no cars available. Also, Um, if both if, like for example, the first condition was true and the same condition was true, it will only it will stop at the first condition. What I mean by that is, if we said like if one equals one and the 2nd 1 was, if one equals one, the 1st 1 will run. And theoretically the 2nd 1 should. But it doesn't because it stops at the 1st 1 That's another thing to remember. So that's, um, occurred for now. Internet out. If statements work in programming, we will use a lot of these that later on, OK, 6. Loop Learning: Hello. Run. Now we're going to be learning that loops in order to create our road or out level backgrounds so that we don't have to place lots of different objects, which can be a pain. So to start this, we're just going to go into a scripts folder and we're going to right click, create new script. Um, I'm just gonna call this one a levels phoner. Um, we won't put this narrows group just to keep things neat and clean. Um, and once we've created that script, we're just gonna go to a game controller claws and place that down. Um, and will also click this called next to out my first script and just remove it. Eso now will open up our level Spooner script, so just bring it up here, and what we're going to do is festival. Um, we're going to be creating our road to do this. We're first going to need the road object. So deuce will remember from last time we'll create our variable, which will leads access Hell road objects will go public so we can use it in the inspector . And it's a game object because we need to be a spawn it, um, and mortars call this one. Ah, road trial. Okay, so if we put back into unity, I'm just going to go into our script out. Sorry, not scripts. Photo our sprites photo, and I'm going to find ah, a road tile. So this one looks good. It's just a plain row tile. How distracted of him. And you can see it on the screen there. Um, and now, if we find out prefers horror, I'm just gonna name it something. So which is called a road tile? Pretty five. And we'll just drag it in here to make it easy. So hopefully if we what we ideally want to do is we will create lots of different road tiles and put them next to each other. Um, so that we can. So if we do x one x two x three eso fourth. So we then have a role of them, and then we would do it. So we put around top of them on and so forth until we have lots of different road tiles so that we'll have a plane road going straight up the middle for our game. So I just quickly delayed things. And to start with what is going to go into a gang control script and dragon the road tile object. Otherwise you'll get era. Sir, if we haven't backup asking, the first Englander is a four look. So in ah, visual studio. If you type in four and then hit tab twice, it will generate the for loop for you. Now, this looks scary, and I'll explain what it means in a minute. Um, but up the top would just create new variable Would call his public int, uh, road length. And we'll set this to be about 30 for now. And we'll also do a public into road width and hopefully five should be all right for now. So the way this four little work is s o, um, I replaced here with road length as well. Um, So what this is going to do is it creates this temporary variable. I've just called I and it starts off equal to zero. And then as long as I is less than in this case 30 it will run the code, and then it will add one toe I So for this will run when eyes equal to zero Isaac Little. 1234 all the way up to 29 then the old wept 29. Sorry, it won't do 30 because I, um 30 is not less than 30 so it will run it lots of different times. This is very useful for appearing different things. Now, I'll quickly explain how this will work. So what we're going to do, Hopefully this will help explain. It is in the fall. It will go game, object in san she ate and we'll put in our road time. And ah, I believe what we're going to do is put a semicolon at the end and we're going to keep her temporary variable here. So the way we do this is what game object? Road time is equally this. So this sets the local variable are already about something different. So stay in shaded talk so it will create a new road tall 30 times and set this local variable in instance of tile equal to the instant there created object, the credit copy of the object each time so that we can change some properties with it. And what we're gonna do is we're gonna go in style transform dot position is able to new Vector three and on the X we live it at zero on the why we're going to set this toe I and he would have said it to zero again. So what this is doing is where setting the transforms. So we're setting three objects position in the level equal to a new vector three. Sir. Vector Three is, um, the doctor type that we use for positions. So it has an X, Y and Z, so X is left and right, wise up and down. NZ is for three D space. So we just going to leave it zero for this. So we're sending it to be at position zero. So in the center of the screen and I so this when eyes equal to zero, it'll set it at zero. A position 00 And then when I z equal to one, it'll create a new one had 01 so it should keep going up the screen 30 times. So if we save this and head on into unity, hopefully we will see it work. If we play a vague so you can see that this has created 30 off the road tiles as expected and that it's positioned slightly on top of each other. So this is very helpful. And what we were used for our level. Now the only issue with this is that it is in a very wide road. So to do this, what we're going to do is each tire. So I'm just going to replace I with why, um, and wind here now, inside this first group. So for every time we move up a position, what we're gonna do is we're gonna go and inside another four loop. So I'm just going to make sure your semi colons like mine. So it was full up inside of fallen, and we're just gonna call this fable X and accidents be less than the road with. And where does he replaced it with X? I'll quickly explain how this works. Sought everything inside this loop inside things while it runs. H time we go up a while level. So each time we go up a tile, then what we're doing is we're going. Okay. Each time we move up one, we're going to go through five x spaces. So we're gonna move from left to right five times each time we go up and then we're going to create this new object at the exposition as well. So what this should actually lead to is a wide road, like, so as you can see, um, it's five times wide, So here we have one x 00 x 10 2030 for their 50 I was very 40 And then it goes to the next Y level and we started 01 11213141 and so forth. Sierra 212233 to 40. Um, like we need it. So what we can do now is in a can controller, we can say, OK, I wouldn't wanna road of 10. And if we do this, we should find that we have a wide road of 10 now. So this, um, can easily let us change the position of the road and make it work properly. Now in order, Teoh, Um, fix this. What we're going to do is we need to move the camera so its starts like the in the middle of the road. So that's a pretty simple fix. if we just open up our scripts, um, up the top here, we're gonna have a public, and we're gonna have a time, uh, game object. And we're just gonna call this, uh, camera Geo. I like that g onto the understand for game objects. So I know that it's a game object and not a camera component, which is different again. And in our starts, um, function what we're gonna do. Size, camera, dear dot transform Dark position is equal to new perfected three. Remember, this is what we use for our positions and for the X value, which is left or right. What is going to say road with Divided by two. Because the road will start at position 00 and go to five. Um, and her. And we want to be in between in the middle of it and just say wiser. And I said zero. So hopefully, if we hop into unity here, we can quickly dragging our main camera object. Um, and then we'll run the game. And if everything works properly Ah que, This is an issue which I get. So what's happened here is, uh, it's probably easier if I going to three d space to show this. Our camera is on the same. So I've just gone into three D space because technically this to Dave you is still Freddie . Our camera is on the same said Axes as the tiles, so it's not actually rendering it. So we needed to move it back so that I can actually see them safe from his position. Otherwise, it would either be in front and can't say them because it's from this position or it's on the same level as them and it's you still can't say them at that point. So obviously, and he's been moved back. So there's that needs to be like negative two or something along those lines will work. Probably. I'm just going to maybe put it negative 10 just to be safe. So in this code would set two new 10. Now, um, one thing I am putting is F after the end. This just indicates that it's a float or it can possibly bay. So therefore, it's a desk float. Remember decimal? Otherwise, it will try and ran it, 20 in, which can give us issues because five divided by two is obviously a 2.5. And, Diego, uh, we are at the center, off the I wrote. Now there is one other issue, which is the fact that our road tiles aren't exactly one across, one down, one up. So we're going to try and fix this now to fix that? What we're gonna do is just open up prefab. Um, and I'm going to just I will drag a prefab into our level. And whilst I'm hovering over same view, I can just eff and it will zoom in on it, which is very handy for finding things. And I'm going to right click, click to the object. Um, en Archos bright. Uh, Oliver three D Cube. And the reason I'm doing this is because this will give me an exact one with one one length inside so that I can see how we need to scale the road tile to fit properly. So that takes out one unit of space. So I'm just going to try and just the scale here until we actually have it so fits properly . 75 still sees was some five OK, 0.775 cents to be good. Um, now, if we, uh put out cube behind. Ah, you say that we can just the edge of the slightly overlapping. But that's not too much of an issue. So if now that we've changed our road tile pre fabs scale to 0.775 you guys can do this. You have to weigh about putting the Cuban if you don't want to. I'm just gonna cook the overrides put in here and click. Apply all Now, this will update the Rotel prefab down here and would lead that anybody it play. We should say that each of the road tiles are now has an issue because they're not white enough, actually, are they? No, they're perfectly fine. Um, I believe that's just a visual glitch by the looks of things. But if the road tiles is working properly and they are the one unit wide, uh, six, if I did this and then just slightly bigger, maybe we'll move the visual glitch night does appear to still be there, so I'm not gonna wave at that too much for now. But as you can see, we've got our road in the centre off the screen like we wanted anyway, So that is all good. The next thing we're going to do is, um I'm just going to go back to ask, right? And in our game, Odin sent. Yet we can give it a parent, just her neat and tidy up the hierarchies hurt doesn't become a massive mess and will just say this dog transformed. So what this does is it will get the transform component or the positioning component off the object, the game object. The script is on, and in this case, that will be gained control. So hopefully, if we run this, you'll see what I mean. But more it's added them as a child so that we can just easily minimize them as part of it . Now, one last thing I'm going to quickly do is you know, instance Tom gonna go instance. Style transform. Don't name is equal Teoh road time. And although bourbon bracket now go X plus. Come on. Plus why? You don't need to worry about this too much, but it's just a nice formatting thing for me. Um and what this will do is it will set the name of the object to be quite a road tile and then give the custom X and Y position depending on what the variables equal to, so that it just again Nene's up the hierarchy. So if we hit play here, I will say that we have zero improbable all the way up to, um 9 29 I believe Yes, because I've got a 10 wider rectal there. That's ah, or well and good. And what we can also do is potentially change the background to something that looks better . But, um, I will do that at another time. Okay, so, uh, we're just quickly finish up this episode. Um, and next episode will add, um, sign so that the strides look a bit nicer as well. 7. Generating Road Edges: Okay, I welcome back. So, uh, this episode we're going to continue trying to make our road look good. So what we're going to do is first open a basket, were editing last episode, our level support a script, and we're gonna add two new variables up the top of our skipped amigo. They both could be public, and we're gonna call them Sprite. So these are images, um, basically or textures. And our first text is going to be left road, and our next sprite is going to be right, Right. What we're going to do is when we are in a for loop at the end of after setting the name of the you are a copy of the road tar. We're going to say if X is equal to zero. So what this means is, um if it on, if it's the left most tile of the road on that section, we are going to say, insist on George, get component. So this is thistle. We got out game object and get component allows us to get the different things on it. And the thing we're going to try and look for is a sprite, renderers and we need to do to open brackets at the formatting for this, you just kind of need to get used to it. And we're gonna on that a sprite render component. We're going to get the Sprite, and we're going to set it to the left road. And what is going to copy and paste this same if statement below it. And we're going to say, if X is equal to road with road So road width mice one we're just said it to right road. So if we open up unity and I'll quickly explain how this works once we've done it, um warders navigato a sprites photo PNG tiles asphalt road. And here we haven't left and right roads left road in and right burden. And if we have played there you go, We see that we have our left and right hand side of the roads. So the way this works again is because of our loops. X is equal deserving. It goes all the way to in this case and nine. So we say if X equals zero, which, um, remember Ella X and Y positions the location of the world three game on the game object in the world. So in this case, all of the X equals zero locations. Um, we're going to set them equal to the left are because there's nothing to the left of them. And then we're going to say for the last if statement, if it's equal road with minus one. So the road with in this case is the amount of times that were looping and ah, the road with this case is 10 Bart's because it's the four loop is a less than and not a less than or equal to. It won't go up to the four road with. So we just need a minus one, which will give us the right hand side and allows to do this. So now that we've got out road functioning, maybe we might want to look at doing a bit of a background, um, or even a start. So the first thing we're going to do is ah, give a starting, uh, Stockmann, which is pretty simple. So what we're going to do is open up our script again, and for a start line, Uh oh, we're going to do is just manually do a another four loop and we'll double tab remember to do that. So we'll just call this one X again. And it said this one to road with And for all the different road wits, uh, we will. Instead, she ate a or create a copy of the object would call and others copy these three lines here . Actually, our copy all of these lines, and we will instead of, uh, okay, what we're going to do is up here without sprites. We're gonna another public sprite. Um, left left, um, start road, public sprite. Ah, start road and public Sprite. Right. Start wrote. So what these is going to allow us to do is we can say, uh, after this, um, instance style. Uh, we're getting it. Why is she here? Festival? We're just gonna put in negative one for all of these because that will be the These are the one start zero and negative one is just the why I below that and we'll get into title get component as Sprite ren dro and we're just a fault. The sprite equal to the start road a sprite and would exchange These two wants to be left start road and right start road pretty simple. So what we're doing here is we're just doing one loop for the start and will also eventually do an end one which will work perfectly. Um, and we open up unity again, going again controller and drag in the relevant sprites so that we have, ah, proper spots working. And we hit play, and they go with good out start, um, wrote working properly. And so that's Ah, starting. And what we're going to do is try and do a background here, Um, and this is going to be a combination of different grass and modern stuff using these tiles . Ah, I think we're just going to stick with this regular grass here and a bit of a variation of that so that it works properly. So in the next episode, we will add the grass 8. Adding Grass: Okay, guys, welcome back. So today we're just gonna be fixing up the levels and make it look a bit nicer and hopefully adding some grass on to the end of it. So the first things first, we're going to fix a few things. So to start with will try and fix those gaps that we're getting sometimes because later on , when we started moving, camera will get out. We'd artifacts because the tires aren't quite big enough. So it's pretty easy fix for just gonna goto assets pre fabs hotel prefab. And we're just gonna change the scout 2.8 on all of the axes and that should, if we're hit, play again and try to move our cameras by dragging the wangle fix that issue are now. The other issue we I encountered or realized is that this isn't exactly sentence. If we would owe Zoom in here, um, it's definitely not on center. It should actually be 4.5 now. The reasoning for this is if we open up our programming cause we're just dividing the road with by two. Where is the four Loop is only actually running nine times, not 10. So that's a pretty easy fix. We're just gonna bracket this road with up here and just minus one on the end of it so that it's actually doing an nine. And that should give us 4.5 on the X that if we do zoom in, it doesn't equally on both sides. Now the next thing I'm going to is if we just sit outside 23 If we never get to a camera over here and set the size 23 it will just zoom it in a bit more so that or maybe 33.5 probably looks a bit better. Eso that when we're going fast, it doesn't hurt the eyes too much. Um, and I'm just going to try and just the y value. So the finish line is right on the bottom, and it appears that, too, is the correct value for that. So if we remember, when we accept play, motor will get rid of all changes. So with the size of 3.5 will set the whiter too. So will change the size 2315 and the white, too. And that will probably get older by the code. So to fix that. We will just open up the code again and up here where we're sitting in our initial cameras position What has changed the Y from 0 to 2. So now if we, uh, open unity was here starts properly now, we could just change the background to be a green. Um, but that doesn't look very appealing. So what we're gonna do is, uh, at this grass sprite that we downloaded fry for your tiles. And then grass will at this land grass tile onto the edge of it, which should work. Problem? Hopefully quite well. So or going to do is we will drag the land grass tall over him. Actually, no, we're sorry. We will open up. Uh, official. How and what we are going to do is pretty simple. We will just I don't know the full on toothy end of this ah, start function. And it will just be a duplicate of these days to appear the four accent for what? Um, and you can probably just copy and paste to make sure to get the semi colons. Otherwise you will get errors. Um, and now we've done this. What? We're going to do is Ah, why is listen road length and we will add four to the end of word length and for road with , we'll add four again. Sorry, right length. We won't have to add foreign toe because that will be up and down, whereas with is that we want and the grass to either side of it. So that's pretty easy. Now we will just copy this camera objects these three lines from up here down into this full it and ah will add a new variable up new Republics. Price and would is called this from public Sprite grass tile at a semi colon at the end like we always do. And we would just say instance, style dot get component. Sprite renderers dot sprite is acquitted grass tile, and we need to make sure with the new rector three. That out said value is set, um, to be higher than the road thoughts behind it. And we'll just said that to something like two. Now, if we open up unity and we click on gang controller and set the cross, write properly and hit play, we should find that we aren't getting the Wright issue. The the right thing happening. Why is this? Well, it's because if we go toe across tiles which, um, still called road tiles. But if we find the start one I'm just looking here is going through them. Come on. If it have played, does find them properly. Hey, we were going to say the cause of going through Okay, The first grass stop starts at 00 We actually need to start to the left to so it negative to. Ideally, we would want it to be there. Sophie, open on that script with the X value would just say X is equal to negative two and will only add to share the road with because we're styling to to the left With that four lip I saw you play. Now we should find that that is working properly as we wanted. Now there's still one issue, which is that it's not starting down the bottom here. So if we just open up our script again, I will set. Why? Equal to negative one? And we will a community up. And if we play, we will find that it working perfectly. So we have how, uh, raceway looking quite good. It's all supporting properly. Um, and we can edit it when we need to end. Add lots of different things to it. So which we will hopefully do in the next few episodes. 9. Moving Our Car: Okay, guys. So now we're going to be putting our car into the level and trying Teoh, hopefully influence the movement controls. So the first thing we're going to is good. We assess follow and out scripts folder and we're going Teoh, create a new script. So we're going to right click create C Sharps groups. And what is going to name this one car controller? Because this is going to be responsible for moving a car. And I'm just gonna wait for it to open up, so free, Just double click it here. I always should find that opens right up in the visual studio, as expected. So the first thing we're going to do with their car controller is we're going to have at the top of variable, and this variable is going to be of type rigid body to day now. Ah, this is basically a component, or like a section of curd that you can add on to objects that allows unities, physics, system toe work with them. So this needed so that we can hopefully add physics to a car and move it properly. So we're gonna talk, Richard Pretty today and what is going to give this variable the name of R P and would just put a semicolon of the end and in the start function, or we're going to say it's a B is equal to get component rigid body to day. So what this will do is as soon as the script starts, it would just set our be equal toothy component of the rigid body component on the game object. Sophie Oven of Unity. Ah, we're gonna add up. We're going to drag in our prefab, and we're going to drag in the black car one and ah, in on this black car. We're going to add a component when we're gonna talking about car controller. And we're also gonna add a rigid body to day. So nice and simple there. And I was screaming. It was over a button click apply. Also updates the Prefect, Um and women have we dragged me once. It will have those components added to it. So if we open back up, well, I'm car controller now, um, the update function or this this section of code gets run every single frame, and what we're going to do is we're gonna do a few checks so the first checks gonna be and if statements were going to say if and but you need to make sure that this bit is case sensitive A lot of the things improving up input don't get K down brackets key code. Don't a actually serve algo que heard dot w and open some color razors. So what's this doing is it's checking if, um, player has is pressing down the W k during this frame. And if that is the case, where we're going to say is abbie dot AdForce and open brackets new defector to, um one a dash zero. And at a semi con to the end. And I'm near this rigid body variable. We're gonna do another on a public float speed and default that to one f and where we adding the force, we're gonna just times this. So that's an asterisk by speed. So what this is going to do is when we Kurt press the w K. We're going to add a force, Um, in the sorry, not the extraction. We're gonna switch this around and we're not gonna add anything in the extraction and in the Y direction, which is up and down by going to, um, at 1/4 of one times l speed so we can adjust this in the editor to make it faster or slower , depending on how, um, we need it to be. So if we ah, go back onto a car, what we're gonna do, first of all, is ah, turn gravity off. So changes gravity scale to zero. So nice and easy and will it save and play the game? So what we should see is not a car, because apparently it's known screen because the zed axes, um is making hides. It would set this like, maybe three. Um, and now we should be able to see the car. And I just moved across a bit toe very 4/2. So it's in the center of the screen. And if we w we should say that the why is moving. We can see this in the position up here, Um, but very, very slowly. So that fixes what does weaken just up the speed. Here, Sophia, keep holding. If we keep upping the speed, maybe 10. I will say that, Yeah, it's moving still very slowly, but it is still moving. So maybe something a little lines of 100 would be much better. Um, and we can say it's now going off the screen, but it's continuing to keep moving. So that's nice and easy. And if we turn on Okay, so now we're gonna accept a play mode and just update these few settings that we change. So setting present too negative, too. And the extra 4.5. And I'm also gonna change the linear drag to be one. So this means when we're no holding w it will still slow down. And we're just overriding. Apply all these overwrites that we changed as well as the gravity scale 20 snaffle play. We'll see out. Still haven't changed speeds of 100. And if you let go, it slows back down nice and easy if you want to make it slow down faster than what we change it to a to, um, it depending on how you see, um, how you prefer, So yeah, I'm gonna leave it at one. Judging by this and change, speak to 100. So, yeah, you can adjust that however you like. Now we're gonna be open about korean. Um, and we're just going to copy and paste these statements two more times, but changed the key codes to a and date. And we're just going to replace this in the opposite direction, like accented earlier. So it's affecting the X axes and not the Y Axis starts going left to right. Um, and when we're doing a one, we're just going to do negative one times speed because we wanted to be to the left and as such negative force to the X value. So if we saved that and hit play, we should say that left and right and all that is working, I not exactly as expected, but it's still working. Um, it does look like a bit of ah ah ping pong car at the moment, and it's sliding everywhere. Ah, we'll fix that in a minute. Okay, So, uh, to fix this bouncing around, which is gonna open up our cut again, and we're going to change this from get K down just to get key. So the issue we're having was a geeky down are only returns true during the frame that the kids first press down. Where's get K Aziz? Long as the case pressed it or will continue returning True self. We open up unity again. We should see with a speed of five that our car is moving a lot more smoothly and as expected. So fight, play the game and parents the camera onto the car, which will may not move that quickly. We should say that it's working as expected, um, speaking that will first fix up this camera. So what we're going to do is will go to our scripts folder, right click, create new script, and we'll just call this one camera. Ah, follow. And what we're going to do is just make it so that would at the top of here would do a public vectored to and we'll just call this one Offset. So what this is is, um, Vector to is just a position in two days space eso it'll have an X and A Y value that we can add it in the Inspector. Um however we like em. I was going to a public transform, which is just the component off the it's the component. So it always come Duo is a transform, which is a component off the at that the we're also gonna do is, ah, public transform, which is the component that's on the game job that that controls the position of it. We'll just call this one object or over J because object is a key word in visual studio. Ah, and you knew. Sorry. Um, and we could just delete this start function update function, or we're going to say is ah this or will say transform. Don't position his equal, do new, effective threat, and we'll say transform. Don't position X transform, Don't position. Ah, and for the why will say offset. I don't want plus, um object Daut position. Don't Why, um and I'll briefly explain to this, and we'll just put, uh, which is defective two. For now, eso What's happening here is we're creating a new position, and the expedition is just gonna be our current expositions or it's just getting our currents transforms exposition and the why is gonna be able to out offset for a while that we've set class the position of the car at the current time. So what we should say is if we open up unity and we go to a camera and we add our, um, camera follows script and we set the object to be equal to the black heart and our officer for the wide, just equal to two. Ah, and we play will find that it's not working because Al said isn't being set. So if we just quickly up back in another comma and type in negative 10 which is ours, that position over immunity, uh, hit play and we will find that it should be showing for the screen correctly. Um, and there we go, it's following it, but not doing it on the X axes because we want to try and only do ups and downs as we said . And if we want, we can make the track bigger by going to a game controller is setting the road length to be something like 500 hitting play and finding that we have and easy track that we can raise along that Ah, it's quite a fast pace. Obviously, at the moment, it's very difficult to see the pace because there's nothing really to distinguish up or when we at objects and trees and rocks and oil spills and that kind of thing. It will definitely be a lot more, um, faster pace and look a lot nicer as well, which we will start doing in the next episode. 10. Infinite Level Generation: Okay, guys, welcome back. So, um, now we're going to work on making the level infinite. Um, because at the moment, we're currently supporting a lot of objects, and it's quite limited, and we can't go forever. So the way we're going to do that is we're gonna learn about function. So functions. Ah, ah, portion of code that, like, it's where you group code together. And U s. So, for example, this is a start function. Um, everything inside this code is called when you run the start function in the same for the update one here, Um and you kind of say the use his function in a minute. But what you can do is you can pass variables into them, so it could be really modular. So the first thing we're gonna do is we're gonna create a new function. So just, um, after the start, when ends, we're going to do ah, void. And we can call a function whatever. So we're just gonna go Ah, spawn rope. Um, and we you Ah, what we're going to do is will do to color very selfish. And what we're going to do is we will just copy Ah, this first initial. Uh, sorry. Not this 1st 1 at this one. Right here. Theis loop in. But in the mid in between, Both the loop, sir, which creates a main road. And we can just get rid of this. Why loop entirely as well? So your copy and paste this loop into the spawn. Rohan eso your start function should look like this. And your spawn row one should look like this. So we're gonna do in us born row, uh, up the top, we're gonna we want to pass in a variable. So we're gonna try and pass in a float, and this is just got every the y value off the row, the the Y position of the royal. We're going to spawn. And that should remove any errors you have. And what I'm going to do is first simplicity where just going to comment out both these four loops. So remember, comments are when we put two slashes in front of them and the basically it doesn't read those lines. I've just use a handy button you can press up here, which comments out of selected lines of legal breast control, K and control. See consecutively and I would just comment out all of your highlighted region, which we're just gonna do quickly. Onda. Hopefully, if we move over the unity, we should find that we have no errors. If you do ever get errors, they will appear in the console and you can always double click them to find the line that's causing the issue. So, you know, start function. What we're going to do just to test it is will say spawn row and will say when y is equal to five arm and we'll put us in the corn after it. So what is all do is to call the spawn row function and set Why equal five on? And then what we can do is copy and paste this below and say something like, Okay, now let's born another role at six. So what this is going to do? This is just our loop that goes across all the from left to right of Thea tile and places the road tiles. So if we open up unity and here play, we should find that we have here we go at two of our row. Tile length at Wyche was five and why equals six? So this is exactly what we're looking for. Um, and the last thing we want to try and do is make sure that we're putting our grass behind that. So that's pretty simple where it's gonna uncommon this by pressing control Can control your hispana p and copy this middle for loop with this x on. Just paste this after ah, in the sport, Grow here and delete this. Why loop of this for look for the y axis. So again, this is gonna this'll Kurds born to road And this code spawns the grass So we can even change this so that the grass section So I'll just comment here. So this at this part spawns the grass. Um and this part spawns the road section at the given. Why value past into the function so pretty simple? Um, I'll even maybe just make it simple. We can rename these to be grass tiles so that they appear in unity as that Sophia play. We should see on the right hair left inside story that they could grass tiles now so you can easily differentiate them after they go. They've spawned the grass tiles properly as we expect them to s. And now what we're going to do is we're going to put a new variable at the top, and we're just gonna say, public float, um, Max y level and devoted to zero f um, or even negative one f and we will then ah, going us start function out. I Sorry update function. We're going the sorry when another variable which is going to be our car. So game object and we'll just call this one car over J eso. This allows to get the position of our car and in our update function, not going to say is remember up there gets run court. This code in here gets run every frame and there's roughly about 60 frames per second. So we'll say if, um car object dot transform don't position. Don't why is greater than current ice or what we call the y level Maxwell level. That was what it was. Um, So what is going to say is, ah, if the car's higher, then we have spawned um, el of backgrounds. Then we're going to go spawn row, and we are going to pass in the Why levels for a while the different. Ah, it's like the rose before. But to do that we need to have another public float, which is going to be, ah, out previous. Why level? And this will just be equal to Ah, negative one f again. Actually, we Sorry this won't be afloat. This will be an interview because we want y levels to be every even number. Not like 1.2 or something like that. So what? Just follow me and I'll explain the code. In the end, I can just minimize thes two functions to make it easier to rate on. What we're gonna say is, um we will go previous y level equals previous y level plus one. So this will just increments that counter. So this will go to zero and we will say spawn row at the previous y level. So nice and easy and what we should see. And we'll also say Maxwell level equals previous viable. There we go. So the way this works is it checks if the car is high up than the top most bro we have spawned. If that's the case, we will increments three y level by one and spawned a row at the new way level and just said it so that this if statement doesn't keep recurring in the start function, I'm just gonna get rid of these two hard credits partners. And if we open up unity, we should be every playing and find that Ah, the variable at here we go again. Erin, the continents is ah, unassigned reference exception. The variable car object of level sport has not been assigned. So if we are going to a game controller, we'll see that car project is not being set and was draggin black car there. Um and if we play now, we should find that as we ah holding w the ah background road is being sport, which is very nice indeed. Eso Obviously there's a few issues here. First of all, it's not taking up the whole screen, so there's a very easy fix for that. Ah, we're just gonna open up our code again. Um, And when we are doing this, if statement will say Max y level, um, if it's so, if the car's vision is greater than the max wide of all. Plus, let's just put in a number around about six that probably should work and we can open up unity. And and if we have played, we should find that it's not working because it should be minus six, not plus six. So we'll just do minus six here and open up unity play. And there we go. OK, so it's now if we go forwards, it's covering the whole screen, as expected. Ah, and you can say that if I hold open the game windows separately like here, you can see in the inspector that just out of our view, it's spawning all of them. No, Um, there's a few issues that we still need to fix with this if we put our game with their back to where it's supposed to pay. The first things first is that this bottom row isn't sporting sort of fix this. I would just say previous why level equals negative three and Maxwell level equals negative , too. And if we play, we'll find that it's going starting. Blow it now so we don't have that issue anymore. Um, and the last issue that we're finding is that all of these objects, if we keep going forever, it's going to start to slow down the game a lot, um, which can cause lots of issues within unity because we're sporting so many different objects. If I hit control a here, you really say that There's what doesn't actually highlight the more for me if I go to Why , Frank, you are to say all of these are individual objects that this morning and this can get very big, very quickly. S o what we're going to do, fix that is we just get out of this play. Modi change. Specter shaded. You need both with that. We'll go to our project or file Explorer. Good scripts, photo and right click and press create, say, sharp script. And this one is just gonna be destroying me. Destroy may place on what? Destroying the works. And if we just open this up in visual studio, what this script is gonna be do is basically check the y values the car and say, like, if where more than $10 blow, it will just destroy the object we currently Ron. So we'll have a public game object, and we'll just call this, um Carro BJ car object and move this. And we'll also have Oh, sorry. Yes. No That's right. Sorry. I would move this start function in the update Functional. We're gonna say if this dog transformed opposition don't. Why is less then? Car objects start transformed a position. Why? Minus, say 10. So if we calibrations. So if the current objects why levels up and down level is lower than the car objects minus 10 units will, just to say, just destroy, um, and will say game object. Okay, Sophie, sorry if we go back to a car controller script now as three levels levels for, um, we can go to where we're sporting the rose and fridge tar when responding them will just say instance, style, don't add component, destroy me and will say AH dot car object is equal to car object. So what's this is doing is adding the component destroy me or the script to the neutral with credit, and it's setting the car, reject it could be the car object and would just copy and paste this down for the cross tiles as well. So it's it's removing them properly. And if we open up unity Ah, we should be every playing and we should find Ah, that as we are going forward, we have looking the same view. It's the leading, the tars blowers as we want. So now we can go in fluently, Um, as far as we possibly want, without having to run into any potential issues in regards Teoh slowing down the computer or anything like that. Eso This is exactly what we want. So in the next video, we can start adding some obstacles and also collisions for the car on the sides, because currently you can go right off the sides. 11. Adding Side Collisions: Okay, I So, um, now we're going to deal with this collision issue we've been having. So currently, the issue is that you can go off the side of the map, so we're going to try and fix that. As you can see, the car is going completely off side of the mountain. Um, and it's causing issues. Sorry if we antic play, what we're gonna do is without main camera. We're going to just create right click on it, click, Create Empty and it will added, is a chart. If it doesn't do it is a china. Looks like this. Just drag it onto the main camera. What this means is that it will move the chart object will move with the parent. So the parent, in this case, being the main camera and put his name his first child left. Cool, cool left. Bora works. Um, and I'm just gonna click Add component box collider to day. Pretty simple. And I'm just going to change the y size to be something like, uh, eight works, and I'm just going to adjust it. So it's just slightly, um, towards the edge of the screen. So something like negative 6.5. Looks like it's gonna work very well. And I'm just gonna a copy this, um left border and right, Click and paste. Or you can just control saying good Robbie parented again and said this one to be right border and just said his position to be positive 6.5. So now if we play, what we should say is that we can't I'm sorry. We can, because the car doesn't have a Kalata, so Well, if we are indeed, play will go to a black car and we'll add a component box, Clyde a two day to it, and it should, ah, make sure that the shape gets it anyway. Override of play all. And we should say that if we play now, um, the collisions are working as expected. So that's exactly what we're looking for. You can always adjust the borders. Teoh fit. However you want to fight change to 75 you'll find that I can go right to the edge there, which may or may not be what you want. Sir, I may actually change mind to be 0.75 in both directions. Just because it seems to match up very well, with the screen size and the border and that kind of thing. It's so yeah, now, also gonna remove a few redundant things for my current. Um so if we open up our level, spawn a script out, we don't need the road length or very length. If the sorry variable anymore. Eso weaken. Just remove that and what we're going to do now is start making objects. So, um, to do this, I'm going to first any available and this is gonna be a public list, and then you do the arrows, and inside the hours, we're gonna have a list of game objects and we're going to call this Ah, road Kaleida Bubbles. Andi is gonna be able to a new list off game objects. Visual studio helps to order for Lois to make typing it faster as well. So make sure what a list is. It's just words pretty much a list off where the type is of game objects. So I'll kind of explain this as we go. But we're going to do is we're going to have a new function. So just before they will go, uh, instead, avoid this time what we're gonna say is game object. Um and we're going to say we're gonna call this function. Ah, get ah, Get up, G o twos, Geo to spawn. And we're gonna pass in an int which is gonna bathe the exposition. And then what we're gonna do is we will just, ah, change up thes, uh, code of it slightly. And instead, what we're going to do is where we say game attracted such it right road tile. We're going to say, Get Geo to spawn and passing the exposition. And what this one main is that if depending on it, we can easily change the object and we can get rid of this for loop. So what it will do is every time it's instead, see, it's goes to create a copy of the object. It's going to call this function which will return the object depending on the exposition. So what we're going to say is, if, um so it's instead of ah, this one's gonna be now in negative two and rode with plus two to make it easier. And we're going to comment. Actually, I'll leave this line will remove, will comment out this bottom follow up and in this get geo to spawn Functional. Going to say is if X is Oh, sorry. If underscore X is equal to negative too. Water. The assemble is these two straight lines. Yeah, if X is equal to negative one. Oh, if X is equal to road wit plus one if X is equal to road with plus two. So what this is saying is if it's basically supposed to be a grassed are pretty much, um So if it's two, if it's to the left or to the right off the main road, stretch them. We're just going to return. Ah, road tile. Actually, sorry we won't turn. We won't return the road tile. We would just, uh, return a Just give me a minute. I'll think about that. Yeah. Okay, So what we're gonna do is we'll just have before this return statement would just go game object. Joe is equal to Rotel, and we'll just say jerry dot Uh, get component. Sprite renderers dot Sprite is equally grass style. Pretty simple. And madras return this geo with the grass. Tass bright pretty easy. Um and then what would you say is else? Ah, so we'll just remove this a game. Objects bid up here and put it outside the six time. And so we don't have to keep repeating the same code. Um, and then we go else if, ah underscore X is equal to road with minus one. Then we will copy this code here. And but we'll set this price equal to at right road and would just copy this else of statement again and said it to left road. And instead of minus, it'll be a plus one, which, actually it will be a zero if underscore X is equal to zero and then we'll say otherwise, just return the regular game object. And now what we can do is we can just remove this uncommon itself and remove these two if statements here, so we'll check this works and then I'll explain it to you. So we've got a few issues here. Um, days can be pretty easy to fix. So thank you guys. So to fix this first off, we're experiencing an issue where the road was to the side. So you see that the grasses and even that's because our four loop isn't actually ending at road with clusters of ending it rode with plus one at because a less than less than and equal to. So we're just gonna change that two plus one and just rode with their. So if in that if statement self, we hit play, we should find that sits even again and fix his other issue. What we're gonna do is public public sprites, Plane road. Um and we'll just say up the top here, Joe, don't get components, right? Renderers, Um, don't Sprynet is equal to plain road, and we should find that if we go over to unity, um, it's just plays probably and should fix it. But we haven't said that a very also currently is not being said anything, but it should work if we actually go into our sprites PNG tiles at Fort Road game controller and dragging that the right has felt road sprite, they *** is exactly what we've wanted. Um, and we've migrated over to using the new system with functions which will then allow us to easily implement collectibles so well, we don't have enough time in this video. Put will to win the next one 12. Adding Road Objects: Okay, guys. So, Phyllis episode we're gonna finally be adding a collide herbal objects that will getting away as so would jump ride in the first things first will go to our as as follows brides pindi objects and we'll try and grab an object that's going Teoh being away. So I'm just gonna grab rock one and drag it into ah, the hierarchy here so we can see it. Um, And I'm just going to rename it to be, uh, rock collide Herbal. Um and I'm just gonna add a component to be a box collided to day. Um, actually, sorry about that. What box slider would do? A whole box ran it. Whereas I believe if we go a polygon clarity, it will be much more precise and hopefully fix any potential issues we have our Although we could have issues where it gets stuck on the edges, but we'll have to see how it turns out. And we're going to a prefect older and just drag out collide herbal rock in there. Um and we can just delete it now. So if you go into a gang of drug will say a rock collectibles list we created last time. So if we just change the size toe one weaken, then dragging a collectible rock and what we're going to do in a code, um is we will for each row we will generate a random number. So what to do that? Well, at the end of creating the road talk, what we're going to do is will say, uh, float, Um, underscore allergies. He could do random range between, ah, we will probably are just 20 to 100 to make it easier. And then what was Hey, is if RG is greater than, um, or less than Okay, so we're probably on 50% of all Pro. It was a ah, a quarter of the time. So with less than 20 foot less than or equal to 25 25% of the time that we're going to spawn a collectible rock. So what we're going to say is came object, um, collide herbal or yeah, called collateral object is equal to. And before we set off, we're gonna have to do another random number generator to generate the object on, and it's just gonna be equal. So, uh, list are injury. He's gonna be able to a random thought range between zero and road. Kaleida bols dot length, but don't count Serie minus one. So the reasoning for this is because road closes in list and each element that gets added or each thing that's in it arm has a index location. So, for example, the first thing you put in is at zero spot. The second thing you put in is that the first spot, the third thing you put it, is the second spot and so forth. Because of this, when we get count, it gets the total amount of elements the lists off. There's three elements in the earliest. The last index location will actually be at the 2nd 1 because they'll be one in the zero slot first saw in seconds lot. Hopefully, that makes sense. If you don't understand it, feel afraid to message me. So then, ah, game object. Collado Bull object is equal to road collectibles and will do square brackets and we will say list orange A and will say collectible objects don't transform. Don't position is equal to this new vector three It x y zero, and we're just copy and paste. This later to lions as well and replace instance style with collectible object. So hopefully, if we open up you now, we should be out of play and find that's how collectible objects maybe appearing but are hidden. So the reasoning for this is because it's at these depositions. Zero. So we'll just change this for something like negative to and will also change. The name we're giving it to her could be a something more descriptive, laudable tile Soviet plane it. Oh, we have a community now and hit play. Hopefully, we can say, um, some of these objects appearing, although it doesn't look like any of them are so we'll have to figure out why that is. We have been about code. Ah, we can see our energy is being set to random number between zero and 100. This could be because it's not an integer. So we'll try fixing that if we have been op unity and we can hopefully see if it's working . Um and it doesn't appear too, so I'll be back once I fix it. I was really stupid. Mistake Home. A couple evidence. See it? Ah, we forgot to actually. Instead, she ate the object. We're just getting a copy of it. So remember, we just add, uh, these lines onto the end of it, and there we go. So it needed were. Actually, we're setting this job ages to copy of it, but we need to instance E eight the copy to begin with. Sophie going to uni Male on we hit play. There we go. We'll see that there's lots of different rocks and that's causing a whole lot off issues as we can see, um, and hopefully we can fix it. So the first issue is that it's rotating like crazy, Um, but we'll also fix the amount of rock, so we'll just adjust this to something more reasonable, like maybe long lines of 55% of the time. End will also say that X has to be, um, greater than greater than or equal to road with. So we'll do brackets he, so it has to be greater than or equal to road with. And X has to be less than or equal to greater than or equal to zero and less than or equal to road with. This basically says the rock has to be on the road in order for it to spawn s so that it's not on the grass because we'll do other things on the grass, like trees and stuff that, um, more relevant. So there we go. Um, it is spawning again. We're seeing issue with them sporting on the right hand side here. Um, so to fix that, we just need to make sure it's left directional road with minus one. Um, and hopefully, if you're probably really and will say that none of the miss spawning on the sides um So next issue appears to be this rotating home, which is causing a few issues. So who easily fix that? It does look cooler. If you want that, you could maybe keep it. But what we're gonna do is click on our black car in in rigid body will see the constraints tap borders, click out this era Next info. Um, and in the constraint section, we can easily change. Well, we have to look on the constraints. Sorry, s o click on them and we'll click. Freeze rotation on the set axes onda click. Apply all. And we should find that it's no longer rotating because it's present. Um, and if we hit something, Yeah, it's no longer rotating, which is fine and what we want. So now we've got out collide herbal rocks to try and avoid, which can speed, which can make it a bit more challenging. Obviously, I'm not very good at this. Um, but you can You can adjust the frequencies, however you feel necessary. Um, and we can maybe add some other different variations. For example, if we drag in our book collectible precept for Dragon in here, um, there were We can adjust it If you just right click here and click unpacked proof of completely this Sargis dealings it on one a There's something like rock collide able to hum and we will congest set the actual texture of it to be maybe rock to instead, which is all well and good. And if we just drag rock laudable to indo prefects Photo good of gain control list and change the size to to We can hopefully see that if we tried quite credible to in, um, we'll get some variation between the different types of rocks as well, which can help and just make it look a bit different. Uh, for some reason. Ah, Okay. So the variable carded destroying me has not been assigned. I'm not too sure why this would try and fix that in a minute. May forget playing in, Uh, we can. Okay. Cheese pause. Yes. So if we ever in kind of issue, we can always passport destroy me Black car above a block. Is it the collectible tiles that's causing this year? Oh, jeez. So it appears that the collectible tiles are getting lots. Destroy me components added to them. So we need to try and figure out why that is, um and try and fix it. So is it on this prefab, normally which Oh, jeez. How did this get so many? I'm not too sure how that happened. Uh, they on the main one as well? Yes, I appeared. Every so I'm just going to delete both of these pre fabs by right clicking and clicking. Delete. And we'll try and fix this from the starting into PNG objects. Ah, rock. One broke to broke three and I'll select more click add component. Al, pull a gun collider to D. We should see that there are working now. Forget all pre fabs. We can make pre fabs of all the different types of rocks, and we can delete him from the scene and hopefully for good. At a game controller, a regrettable placed changed the three and we can set all three of them into the list. And hopefully, if we hit play, we can see them all sporting. There we go. That's why, uh, I was experiencing some FPs issues, but yet again, a lot more varied. Um, and we will eventually add lots of different types of Kaleida Bols for you to try and avoid , which will add fun like oil spills that slow down and that kind of think until you get to the end of the level. So, yeah, we'll do that next episode. 13. Implementing Hitting Objects: Okay, I So, um, now we're going Teoh, add ah, fair fume or different objects. And, well, maybe make them do a few special things. Eso First off, I'm going to do a few different objects that when you hit them it but first slows you down . But day you can then walk past them. Um, And they, for example, the traffic cone out. Once you hit it, it will fall over, and you can go through it, but it stops you temporarily. Eso would just drag this current straight image, um, on tooth out of the file, export onto the hierarchy. And we just had the polygon collider to a component. And what I'm going to do sexually add another polygon Clodagh to a component to it and we're gonna take this one is is trigger eso. This is definitely a crowd. You need two of them for this to work properly. Animal goto assets photo pre fabs and dragging the corn straight. Um, Now what we're gonna do is go to a scripts photo and we create a C sharp script. Ah, and we'll just call this one falling objects. So once we open this one up, what we're gonna do it will delete whatever is in it. Um, em will add a new variable at the top, which is gonna be a public sprite fallen sprite. So this is gonna be the image it switches to when you hit it on. And now what we're gonna do is put in public, avoid on trigger enter to day. And I'm gonna here, end up because this needs to be specifically like this in order for this to be triggered once that play hits it. Um, in order for this to work properly, um And then what we're gonna do is we'll say, um would would just say, get component. Ah, polygon collided today dot enabled it was false. And I'll just do that, actually. Sorry. Let me change. That will go for each. So this is a special loop. If we double tap tap, what this does is it will loop. It's basically it's him is the four loop, but a bit simpler. So we'll go forage and will say polygon collided today. Chris, remember, you have multiple at colliders on this objects or just otherwise getting the component would have worked. Probably, um, with the other method idea before on will say Get components. Now get components. Um, with an s will say, polygon collided today and we'll do two brackets there and we'll just say cold or enabled equals false. And after that, Luke, would you say get component Sprite renderers. Um, don't Sprite equals fallen sprite I case. And nice and easy now for you. Open up. Unity will just go to a current straight and drag this falling us object script onto it, and we'll find our fallen current sprite and just track the cone down one onto here. Goto overrides click apply all. And hopefully if we've done everything correctly, it should work properly. But we haven't added to our list yet, so we'll have to just changes the four. Um And then we got off pre fabs in a gang patrol drug in the corn. And if we play, we should see it appearing now. Ah, jeez. That was unlucky. Start in between both rocks. Um, so hopefully if we do see it kind um, we can get it. James, we're gonna really unlucky here. Um, I did at it correctly. Right? Okay, We we not Is this not working? Probably for some reason, um, we definitely have added it. Fighters had one manually on here and set the Y two 89 which is about where we're going to . And I just moved here, and we'll just test functionality first. Yes. See, that fell over once we hit it. Um, but why isn't this appearing like we wanted it to, um is the big question so will open up at levels corner here and one where picking the random number for the list were doing. Ah, sorry. This needs to This doesn't need the negative one of the end, because it's inclusive for the minimum, but exclusive for the maximum. So we just needed to get rid of that negative one there. And if we hit play now, we should say that our cones are appearing. Yes, that's what we want. So now they are, um, falling over when we hit them, we get one there. There we go. Um, just like we wanted. So they aren't slowing us down enough yet. Looks and things like to be annoying. It seems like when we hit them, they're just immediately falling over. So we do want to try and give them that bump first. So to fix that would just open up our prefab. And we'll see that yet Definitely is collide herbal. So this is a pretty simple fix where it's gonna open in visual studio Goto Well, well, falling objects script, and we're just going to do I, uh I enumerate So this is a special type of, um, function. Um, this one runs a seem curiously cause Normally, Um, the way it works is it reads your code line by line, and it has to wait until the previous line is run. But if we're waiting for certain amount of time, that kind of breaks things. We have to use an i e numerator. So we'll go wait for seconds, would just call it. And we'll just put in yield, return new wait for seconds and will just say 0.1 f We just need enough time for the carte , actually like fuel, like it's hit something and slow down. And then just on our on trigger enter will go start co routine. Wait for seconds. Um, and that should be all good. And we should see that if I spell it right, it doesn't give us any errors. We have just moved that card that was in Entre Ganda into Thea. I knew Maria. And if we open up unity, we should say that when we hit one of these again. Yes, they are actually stopping us for a short period of time. But then we can go through them, which is exactly what we want. You can always adjust that. There we go. See? We're feeling that stopped when we hit them, which is all well and good. So if you want, um, we can do more falling objects. So if we are just goto well, sprites again P and G actually, sorry. What we can do is get a proof. Helps Dragonair cone quickly click Compaq prefab completely. Um, goto a surprise for the pin J objects and some other objects could be these barrels. So if we just did this one to be barrel underscore red dragging the red bell upright track in the barrel fallen. Um and I just make a copy of this and do this from barrel blue eso We have a bit of variation and we'll just too upright one And the Fulham one And if you are are prefab so we can make it thes both pre fabs, the lead them both and Adam to at least so they appear like we want. And we should find that they are working the same way as we needed them to. So, um, one of the things I'm noticing here is that the falling sideways, which doesn't seem very realistic with the way that we're moving forwards, we'd expect them to go the other direction. That's a pre simple fix. All I'm going to do is just rotate this, um, barrel, uh, on the zed axes are 90 degrees so that it's is rotating properly select. Leave the overrides. Is it gonna let me save it? I'm not sure why That didn't It doesnt have here to be allowing Meteo override anything. So we'll just do it manually from the prefab here and just do the same thing with the red. So what that's going to do is just rotate the whole object by 90 degrees, which doesn't matter for the first image, because there's a circle. But it should mean that Yeah, there we go. The, um, barrels are actually falling properly. Um, and modesty red one for a minute and There we go. That's exactly what we want. Yes. So, like, um, we can see that the falling objects are working properly. The cones are or working. We could even rotate the cones that direction just to make them look a bit nicer as well. Um, and seem a bit smoother, softer hit occurring. Now you can see that it's full in the right way. Um, now that is one issue is that they are appearing over the top off the car, which we need a fixed. So that's pretty easy. Fix will just move the car instead of the negative to do, like negative five. And now, if we run over any of the objects, we can see that the car is over the top of them, which is what we want. Eso. In the next episode, we can deal with some oil spills and maybe some. So some slowdowns and some speed ups which aren't exactly collided. Yeah, 14. Adding Oil Spills & Speed Boosts: Okay, so now we're going to deal with some speed up and slow down in regards to the oil and the yellow hours on the track so that people can try to aim for things. And George, other things. Eso We'll just simply drag out oil in on the, um, track here and we will just add a polygon collided toe. I bow or going to do is make sure that we take the is trigger button on it and we'll get our preferred photo and drag you now oil. So it's got a prefab over it. Now we're going to go to a scripts photo in credit script for this oil. So we'll go. We'll just call this one slow down, slow down object, and we'll open that script up and again everything insider, and we'll do a public float. And this would just, um, b a slowdown multiply. Um, would default this one to be like zero point one f. And what's that's going to do is we're gonna do a public void on trigger enter to date like we did last time. A visual studio order complaints that make sure this is spelt correctly. Otherwise it won't work and the function won't run properly. And outside this we're gonna do in any murder, and we're going to set call this one slowed down time, uh, rockets again. And inside this, we're gonna yield, return new wait for seconds, and we're gonna have another public bearable. Ah, which is gonna be slowed down? Uh, slow time would default this to about, uh, three seconds and in this way for seconds will just put slow time. Um, And now in he will also one more thing which will display our public, uh, game objects. And we'll just call this one car, geo, and we're going in now on when we touch the oil, weight is going to start occur routine of slow down time. Nice and easy. And we, at the start of the current team, like wet before we start waiting. Ah, we're going to get our cardio and set its speed to be slower than it was. So to do this, we're gonna go over our car object and on it the way we have is a car controller. So we're just going to say car Joe, don't get component car controller dot speed. Um and we're gonna just said, um, both speed. So what is gonna do is we're gonna keep the value of the speed. But it was before, um, so that we can change it back to it once the oil in this case has worn off. So we've got the old speed, and now we're just going to set the speed to be out. Slowdown multiple ill. Um, Actually, what we're gonna do is, well, times the old speed by the slide out much play. Because what this will do is it Will, um if you hit multiple oil spills, it will slow down even slower. Rather than going the same slow speed, which could be really interesting on. And then once the oil is worn off, what is going to say? Cardio don't get component car control. Don't speed his ego toe, old spade. Nice and easy. And that should be all we actually need for this one. Uh, but we also need to set the cardio object. So the way we're going to do this, we're gonna go into unity and click on our car, and we're gonna set, um, click on this tag button over here and click ad tag and this will bring up a new window. We'll just click the plus. We'll just making new tag cool car Goto up that car and set the tag to be car. Could kiss will Then allow us to go to our script. Um, and just before the intricate enter will say cardio is equally game object Don't find game object with tag and we're just type in the tank we dismay which is cast this will find the object quite easily and make sure that variable is set. And we can also move this from big public so that because that isn't necessary, it's in Africa unity. We get our oil object and we can easily add our slowdown object onto it. Ah, well, could apply all on. Hopefully if we go into a game controller and Adah this oil spill to the list, it can appear and should work as expected. Okay, so let's try and find one of these oil spills. So we hit it and was slowed down really, really slow. Um, which is exactly what we want. So you really are. You shouldn't be trying to hit these, uh, those bills because they will slow down a lot, and then you speed up. Ah, and obviously you can adjust. Um, the speed changes in that kind of thing. If you really want what you can do, um, I think I'm going to do it is we'll open up our oil, spilling it, and we'll add another polygon collided to it. But this time we're not gonna take is trigger. So what this is going to do, it will actually, like, stop the car. Um, when you hit the oil, um, and we'll go to a falling object script and copy this for each loop. Um and then we'll put this at the At the start of this, I innumerable put a yield return New wait for seconds, 0.2 and then after this will add the for each loop. So what is going to do is it will wait and told the car slowed down because it hits the oil spill, and then it will disable their colliders to the cargo through it, but also make it to the cloud is slower. So this some because before what was happening was that we weren't actually slowing down when we hit the ice, but was find out slightly afterwards, so this will just look a bit more realistic. Proper, if you want to hear that. Yes. There we go. Slow down. What's be adores, Bill Most ever gonna find one that's gone. We missed that one. Which is I guess, what you want to be trying to do anyway. Um and, well, try and find another oils. Feel if they're going to spawn. Oh, say I'm meeting them all in the side, which is not what you want. Um, there's a barrel. Here we go. So we hit this one, we'll find that we slowed down once we hit it. You really, Um I'm trying, dammit. But yet once you hit, they owe you slow down. Actually, that doesn't. Now that I've tried that, it doesn't look like I actually want that. So I'm just going to change that to zero f. And that should fix some The collision issue and weaken. It'll just be like it was before, which I think works a lot better. See, there you go. You slow down with the oil spill and you slide, which is exactly what's the one. So I'm gonna go that will spawn. Just remove that second polygon collider. But if you prefer that method, you can always keep it there. And I'm just going to leave that old code that we had it as well. So now I'm going to try and do the opposite for a speed up. So hopefully this will work quite well. If we go to our objects out. Yellow error. Ah, very big. That's all right. And we'll end of the polygon collider component to it that is triggered to be true will go to a scripts for a and we'll call this one. Um, I'm actually going to use our slow down script on here, and what we can do is actually just change small supply. So it will be too. So this will double a speed and was at the in this case, slow time to be three seconds, but it's actually gonna be speed up. So this is a quite easy a pre named the script how I should have, um but anywhere that works without us having to copy the same coat into a different script . Selfie, open now. Now, if we see a yellow arrow, we should be able to hit it. Yeah, and then we go with speed up really, really quickly. Which actually doesn't seem to be a good idea because it's very, very fast. Um, as you can see there maybe, ah, times to multiply wasn't the best idea for that one. And I'm also noticing that a lot of the objects that are on the side of the track um I'm no really liking. Um but whoa there because a lot of them overlapping with that. But I will leave them there because otherwise you'll have people that stick to the side of the track cause there's no objects in their way. Ah, which can be common inconvenience. Okay, so what we're going to do in the next episode is at, uh, trees and stands and people across the sidelines to make it a bit more diverse because currently got a lot of things on the track on and not many on the sides so that people can just cruise up the sides the whole time, which we want to try and stop 15. Side Road Objects: Okay, so now we're going Teoh deal with objects not supporting on the sides. Ideally, we don't want trees and rocks and that kind of stuff that wouldn't have what we want. Trees and rocks and that kind of stuff, but not like barrels and speed ups like we have on the road, because that would be out of place. Eso to do that. What we're going to do is open up our level supporting script just over here and where we have our road collectibles list where it's gonna copy another one, and we're going to call this side road Kaleida balls eso that. We have a different list for these types of things. Um, And now what we're gonna say is, um, in this if same with just doing else, if RG is less than or equal to or greater than or equal to 95 or 90 will say, because we want more objects on the outside and sorry, and and I'll just go down to this if statement here and copy is saying conditions because this is the conditions we use to determine if the X value is the side of the road or not. Um So I just changed that there. So you can copy this out if you don't have that. Um, so basically, if an 10% of the time, um, if it's on the side of the road Ah, we would just copy the same thing curd here. But instead, a row collectibles would say side road collectibles. And we'll just say side Rick labels again here, um and I just change the name, so it's easy to identify them, and that's all you need to do. So I've just copy this anchored from there, and it's pretty simple. So now what we're gonna do is if I goto our sprites on PNG objects on Dragon say a tree, Smaller image there. Give it a polygon, Kleiner, Um, and put it in the prefect's photo. I can then go to the gang Androulla. See, outside road collectibles list. I said the size to be one and put in a tree there, which are working as expected so you can see the trees along, decides the roads. Ah, which are working as we want it. Um, Now I'm considering on and I think what I'm going to is, uh, well, move the rocks to be on the side of the road as well because they look a bit more, um, in like, they should be there a lot more than they do currently on the middle of the road. Eso that's pretty easy to do. And I'll just minus three from here. So the problem that this is now I have to change all this backups or just at the barrels here the cone, um, and the oil slick. And that should be over waiting for now. We should find that we have rocks and trees on the side of the road. Um, and we have al cones and barrels and that kind of thing on the ah, middle of the road on the actual word eso it looks a bit nicer and, like everything fits our Remember, you can always add your own different things to this to make it easier or harder on. The next thing we're going to add is people, because if you hit, people will probably have a game of a condition, because hitting people is probably not one of the good things you would want to do. But before we do that, I will add some tires on two year because I reckon I want to have objects that when you hid them, they go flying because that looks really cool. Um, is the main reasoning for that. So we'll drag area. I'm gonna choose this time here, This tires red alternate, and I'll give it a polygon clatter. Um, and I'm also going to give it a rigid body two D, and I'm going to make sure to ah, turn off the gravity scale. So it's at 10 And if you go to a pre fabs, I'm just gonna drag in the twice here. The lady from the in the same good game controller. I had one more to the very collectibles and drag in our tire him. So what we should find is that if we hit the Thais, they can. They can move out of the way and go flying is such however jeez, that's going really fast. Um, but yeah. So if we I am no hard trying to bring him another tire, um, and we can adjust it. Why is the uh okay, So the issue you've got here is that the speed stuck at 0.8 um, which is nowhere it normally is. And that's because we're getting the old speed. Ah, from in a slowdown object before we're changing it. And if you get multiple oils like that, old speed's gonna get Lauren. Lauren are So I'm just gonna hard code in that old speed. So currently, our speed should be at five. So instead of getting the old speed of retire, I'm just going to set it to be five eso that should hopefully fix any issues that we're having in that regard. Because currently, if you sometimes there's an issue where, like you get stuck really slow forever, and then it kind of screws you over, I said, they go. If you hit the hum tires, they go flying, which is quite an interesting thing to watch. And they're working as probably You can always adjust the gravity on the rigid body or sorry, not the gravity of the mass to make heavier and that kind of thing in the drag to see how far it will go on that kind of those different settings all here you can change. So the next thing we're going to add will be some people on the style Besides some people on the side of the road watching the race s o. I just dragged them in. Now they're really, really bigger. Will have to justice size in a minute. I'm just going to quickly change the rotations that they're on the sides. Um, and the issue we're gonna encounter here is that the house one on both sides facing the wrong direction, which will have to fix in a minute, sir. I'll add the polygon Claudia to this one, and it puts the box around it. Forget all pre fabs dragging the tribunal full on and added to the list will say that we're having an issue with the Tribune's. Oh, geez, that was in the wrong mom, wasn't it? It needs to actually be on the side Road one, But again, we can see that they are way too big. Um, so we'll try and fix that in a minute, but I'm trying to show you the issues that yet hellspawn hum both way too big and on the side of the road so we'll try and find the right size for them by Justin Scale. So maybe half scale may work. We'll see how that go Is that looking quite good? I That's still, I think, a fair bit to it. It needs to be further that way and long ways. But lest needs ideally, it should be there shouldn't, um which I think was an issue. But you look at the size of the people versus the car, and you see a few issues there. Um, but I'm gonna try maybe a points six on all directions. That could look quiet. Okay, so we'll just change that in the pre five reference and we can see that's they are a bit smaller and hopefully feeling in a bit more. Um, but now they're on the sides that roads as well. Still eso we'll try and fix those. And also they're taking up two spaces, so I would deal with those later on. For now, I'm just gonna move because they would be be more of a pain. But we will add up people now. So pretty simple again. What is going in? 12 sprites. Photo. PNG characters. I've got a lot of different people that will be standing in the track waiting to be killed . Um, you may be making wanting to make a game where you hit them. You get points for that. Um but I'm going to make it so that if you hit them, that's the value condition. Otherwise, you can keep going on forever. But it's up to you what you do. So I'm just gonna drag in. Ah, some characters blue, black, green, white. Um, and we'll select amore and click out of polygon Kaleida, and we'll go to pre fabs ends. This polygon collider for them is all going to be a trigger so that we can make it trigger a win or lose condition. Um, and we're going to go to our scripts older. I'm gonna create new script, and I'm just gonna call this at failure. Object? Um, because you can put this on whatever you feel you should hit. Ah, and the game will go over if you want that to happen. So over the top here. Remember how I spoke about libraries? We're gonna add another library, but having using anti engine a dot seen management. So this will allow us to reload the game when we hit this value objects. So we're gonna type in enough I object. Classroom environment. Avoid on trigger. Enter to day as we did before. And we're going to to say Seen manager dot loads him and we'll say, Seen manager I don't current sing our daughter gets get active scene story, get active saying AH dot name. So that should just easily reload the current scene. Um, when we hit the object so that you'll go back to the beginning so we'll just drag the failure object onto all those, and you say it's all on them. Uh, add them as pre fabs and we can add them to the list. And we should be all good to go later on. I'll do a bit of balancing with Thea. I'm sorry the chances of the percentage chances because obviously you want these to be a lot lower than some of the objects and maybe a lot less speed bonuses in that kind of thing . Ah, hit. Wow! It appears that the people yes. So when you hit them ah, you quickly result the level and you can keep playing again, Um, which you may or may not want Now that there is one issue which is the blue background as the level quickly generates Ah, we can fix that with a short transition and that kind of thing in a game over screen, which would be pretty easy. And we'll do that later. But for now, you currently have, ah, game where you can lots of objects generated. You know what? To Mr Hitting. You have speed, bonuses and slowdowns. Ah, which and hopefully have a good understanding of coding. But we'll add some more features through this later on in the next video. I'm just gonna quickly make these people a bit bigger. It's a very more like 1.5 in each direction. On def we hit play. We should say that they're a bit bigger. Yeah, they that looks a bit nicer, more reasonable. Like they're in the car as well. Um, and that sort of thing. So now freedom, they go, it's still restart home. And we will add some more features. Um, in the next episode, 16. Object Spawning Chances: Hello Runs. So now we're going to work on adding weight of percentages. And basically, what I mean by that is that eyes making some objects appear more often and others not so often. So the first thing we're gonna do is open up Visual studio here, Going toe are levels pointed out? CS class. Now it is going Teoh scroll down right to the bottom and just passed these last curly brace We're gonna hit enter twice and we're going to try public class. And we're just gonna call this one collide herbal object and will do open to color races. And inside this, we're gonna have a public game. Objects, maybe J and we will have a public, um uh, into Joe. Um, chance and we're going Teoh, Just put a line above this variable for this chance number, and we're going to dio square brackets and we're gonna type in Ah, slider. Sorry range. Sorry. Ah, and we're going to open two new brackets and we're gonna type in zero and ah 100 and we'll just save this and we'll go back up to the top of our levels corner script and where we have our two lists. We're going to change these from game object lists to collide Herbal objects lists. Ah, and you'll see what this does in a minute. So make sure just change wherever it says game objective collectible objects. Now you will get a few errors here. Ah, the way we're gonna fix this is the 1st 1 when we're in Stan Shih ating our object just after this square record will do a dot and will do Adobe Jack and the same thing it. Now I'll quickly explain how this works. But if we open up unity Ah, we will say that we now have a list. Actually, we don't because we need to go back to our script and just of the top of Thea Pliable Object class will dio system dot serialize a book. Okay, this just means that this new ah class can be read by union shown in the Inspector. So now, without lists and at and T park, if we add a unit to them, what will say is that it's adds an element one, and it will give us a custom chance for h different elements with the game object on it. That is because Ah, we have made like, a structure or ah, class, which has these values in it. So why adding Dover Jetty and he worked is because we're getting a copy off this global object. And this class, this gets the copy of the class and we need the dot object. But we can also do the dot chance if we need that as well, to get the spawn chance, the object. Um, and the way we're going to do that is with how are G generation here. So the moment this is how it had differentiates between whether it's a side road collectible or regular road collectible. Okay, so now what we're gonna do is we're gonna go up to the top of a script, and I had a new variable. This is gonna be an indigent. Doesn't need to be public this time because it doesn't need to be seen in the Inspector. And what is going to call this weight total? Uh, and I'll just call this and Paran Road Main Road on the end because we're gonna need a different injured for the side road in the main road. So I'll just copy this and change this one to be side road as well. So there's two variables that are very similar. I'm gonna open up our start function and just get rid of this. Commented that code and all. I'm gonna say it's for H collide herbal objects. So double tap space when you type for it. Remember, we'll just call this one over J and road collectibles. I'm just going to say wait, total main road equals weight. Total main road plus over j dot chance nice and simple. If you want a short on this, you can do wait hurdled main road plus equals over Jadot chance and that will do the same thing as the cut above whichever you find more readable works. Um, and now I'm just gonna copy the same code, except I'm going to change it to be a side road side road collectibles. Um, and instead of white, total main road would do wait hurdle, side road, eso that should work all good. Um, and just with these images of the start, we'll just default them so that equal to zero to start with so that we don't have any issues there. Everything should be good. So we go back down to our, um, just blow out spawned roh function. Uh, I'm going to write a custom function for getting the way to value. Um, this involved a fair bit of maths and stuff, so just make sure that you are type it correctly s so I'm just gonna type int waited. Random mandatory is what we're going to call it. And this function is just going to return. Thea result. Basically, eso start with gonna start without variables, and it's gonna be in result equals zero in total equals zero, and we'll say int random value equals random dot range, and it will be between zero. And the main will also pass in Ah, the different totals. So we don't have to copy the same function over and over again. Eso In this case, it would be either the main road type total or the side road total, depending on which what we're using in this case. Yes. So the next line we're gonna do is a for loop, so we don't have a tab. Um, and we'll just do for each result, um, which is equal to zero. We don't need to declare it again because we're going to use their variable that's already up here. And as long as result is less than what? Wait, It's a total dog. So we also need a passing another variable, which is gonna be a list of collide herbal objects. Um, and in this case, it's gonna be court would is caught list. I will say, Let's start camp because we need to know the amount of possible values in this one, and we'll just I wonder the results. So this all keep looping until we have gone through everyone. Every element in the list in this case, all the possible objects we could spawn. And we would say Total and I'm going to use plus equals. Remember, this is also the same as title equals total. Plus, whatever, I'm will go list and I'll say results don't chance. And also, if total is greater than random value, break. Don't worry too much about what this does, but basically exits the loop and doesn't keep running afterwards. And at the end of this weight is going to return results. Um, nice and easily, And this will just give us the element for the list. Um, but I'm going to change it so it just returns the game objects that we want. So it'll return a game object and said and will say return list results don't oh VJ So what this is doing is we will call this function animals initializing variables, choose a random value and go through all the different elements in that we can possibly spawn and then, um, test. If, like with their way to percentages, which one's the most likely if it finds it, it will exit out of the for loop or wait to the end, and it will just return the copy of the object that we need. So now all we have to do in this game object, of instance. Yet ah, is we can get rid of this list RG line and in where this says row collectibles will just delete this and we will change it, too. Random waited, um, and the total for this case because this one will be on the main roads that will say, ah, way to total main road. And we will also do a comma. And the list we're gonna use is going to be at Maine Road or is it just rode collectible stroke levels. Okay. And that should be all we need. And we'll just do this, copy the same code down to the side road collectibles and just change it to be waited side road and side road collectibles. And we can get rid of this line here. So what we're doing is, um we're just using our new ah function we made to determine what object we're gonna sport . A malt spawned this 5% of the time and as we did before, and the other 90% will be on the side road. So if we open up unity now, ah, we will have to re add out different roadkill audibles onto the track. Um, I just do five for now. Um, with just the main road on the main road can have cones. It can have. Ah, blue person. It can have red barrel cava speed boost. Um, and that's what we're gonna do for now. So we'll say it's for the cone. I will say we probably want a lot of these. So, like a 70 per 60% of the time, we'll use cones at people, maybe only like we were very little of them. so 5% of the time barrels. We want a fair few. So, like to 20% of the time, these aren't exact percentages. But basically, the higher the number, the more likely it is on. And then yellow arrows will probably do just a bit below barrels. So we'll give that value of 15. Remember, you can adjust these however you want. I mean, closely put slider there. Um, it will, um it's shows the slider in inspector, which just makes it easier to edit different things. Ah, but I think we're gonna get issue because we haven't got anything in us. Are sorry. It's because we have our fourth element with nothing in it. Eso I just add an oil slick to it. And again, I'll probably say 15 for this one because probably wanted to be a is equal. Odd. We still having issues? That is probably because we don't have any side. Rick. Laudable soldiers Quickly at one, I'll say Trey small and oh, just put in, like, 50 here and there we go, so we don't have any issues. Um, and as you can see, there's a lot of Cohen spawning the occasional person on And you can always old to these values, too, how you see fit. So, for example, if I want oh, they hit a person. But if I wanted to change it so that, um, cones were like to and we keep going, we should say, hardly any kind of whatsoever. Um, but we are seeing lots of weird artifacts here, probably because I tried to change it. Ah, without the title updating. So you must. I'm just gonna make sure, um, pause the game now that I'm gonna change itself up to now. Ah, we should, uh, see that there's hardly any exactly so hardly any cones whatsoever. Um, and lots of oil spills and other things. So, um, now you have your way to percentages. You can add back all the different objects and given percentages how you say fit and go from there. So I'll just quickly add a few rocks, and so that sort of thing to the sides rocks. Maybe you want a few less so, like, 30% of the time and on 25 for rock three. It doesn't really matter. Um, maybe at a tire to the main road back again. Um, and ties could be fairly common. Was that like 30? Just because you can go through them and maybe a bluebell as well. So h time just upping the number on the list and changing will say, maybe bluebells could be quite rare. So I'll give you five. Ah, a justice How you see fit So you can see there's lots of current because we have a month 60 . Um, obviously, I think maybe, maybe too many. But if you would like to change the overall chance, just objects sporting in general. Ah, you can just open up the code and see where this is less than a good five. Um, you can change that. So, actually, what quiet quickly will make it so that we can edit that a lot more easily. So do a, uh, public in ah, road off road collide Herbal Sporting Chance. And would they felt that to five? Um, and just before this would dio square brackets would It was sly starting that slider range between zero and 100. And I'll just call me the same thing and change it to side road, collide herbal spawning chance and said unequal in 90 to now if we go back to if statements instead of lesson ergo five will say less than or equal to road collide herbal a sporting chance and this would be greater than equal to spawning side road collectible sporting chance. Um, in this case, it's 90 eso If we open up unity now, we should say, uh, these chances here I they're obviously the names a really long, um, but if I wanted less objects, I could change that down to, like, 2% and you'd see a lot less objects on the Well, obviously, they got a lot less things in the way. Ah, which is probably how you want it so that the play can get a fair bit of speak going and try and dodge different things. So I could even change it down to, like, a 1%. If I really wanted at this, you can change on the fly. Doesn't really matter too much, but obviously can see the chances are working here. There's a lot more cones compared to oil slicks and that kind of stuff, um, and ties. There's the casual one, um, and people were saying very, very rarely because they're the game over condition. Remember? But you can obviously change him. Say, if I want more side road. Claire was I would just drop that number and you'd say a lot more. Here. Look, you can see trade overlapping and everything, Um, because they're a bit bigger and you see, hips trace, I bet on the opposite drag opposite, um, condition. If I wanted tapes of road Clyde A was I could have that to, like, 33. Um, and you say that there's hates of different things. And if you want a game like this shore But that's how you do it, so you can easily adjust these different elements. And, yeah, there's my first person that took a while to get anywhere. Ah, yes. So in the next episode will continue working on the game. 17. Bug Fixing: Okay, guys. So, um, today we're just gonna fix up a few things. The first issue is when we're loading, there's you can see the world slowly generating, um, and this and look very good eso There's an easy way to fix it. So if we just open up house, create on, we go down to out they function where this is fewer members getting called every frame. And we're checking to see if we need a spawning you rock issue. This is the start. It has to do this about eight or nine times. And there's that small amount of frames where it's rendering wake and see that there's nothing there. So what we can easily changes to is a wallet. So this is like our four routes, and the cone in here will keep running an infinite amount of times until this condition is no longer true. So, for example, um, this, uh, this just means that during one frame it will spawn everything that needs to up today. So if we save this and open unity, um, and we had playing, we should find that it's year instantly. Um, if we respond, you can say it's instantly reappearing back on and it's working, probably in as expected. So that's exactly what he wants. A fight to see. The person will say yes. Generates super fast, Super quickly, um, and works perfectly. So the other thing is, ah, without oil spills. What was happening was there were some instances because remember, our oil spill resets the cars speed after a little bit amount of after amount of time. Um, but sometimes if we're going fast enough Ah, the oil game object gets destroyed because where it's fine off away from us. So that means that it's not actually recently, cars speeding it sometimes can get stuck. Really, really slowly. So to fix that, what we're gonna do is we're gonna just open up our car free click on my car here. We're gonna open up our car, control a script on dime, just going to do another public that bull. And I'm just gonna call this 10 sorry. Public float. I slow down. Actually, I went to a public variable. I will do a public i e numerator. I knew Merida. Ah, slow me down. And in this orders have a speed equals 0.1 of yield returned new white for seconds, however long we want to slow down four. So forget how long do we do for our slowdown? Three seconds is what we did before. Um, and we would just actually I have its opacity in. So would work float slow time and would just wait for the amount of time that we pass in. M will set speed back to just one. So what this does is in a slowdown object. Um, instead of doing the i e numerator in here, we can delayed the I in you right up, and we'll just say with a cardio cardio Don't get component car controller. Don't start, Koerting. Sorry. Not start, Koerting. We need to put that at the front. So we'll say. Start, Kurt. Car of routine tied Daut. Uh, slow me down, okay? And we need to give it the time we're going to start down force. That would just say slow time. And we also don't need this slowdown multiplayer anymore. So what this will do is we'll call the, um this function, which sets the speed to 0.1, waits the amount of time we pass in. So in this case will be three seconds and then set the speed back. But this script is on the car so it won't ever get destroyed. So we won't encounter that issue again. So that should hopefully fix those bugs we've been seeing. But currently we're really, really So for some reason Ah, that's because ah sp isn't getting set back. 21 It should be getting set back, Teoh. So it should be five, I believe, is what it actually is. And maybe it's a slow down. We can do 0.5 instead. So if we open up unity now, hopefully because you are speak was five. There we go, going faster. Um so now if we hit um there were always now was that's an issue is that was slowing down. When we're hitting the speed at once, we can easily fix that. We will just go back to a slight an object and will bring back that variable. You know, car controller will also as well that float tumble Atacama and was so we want our speed multiply to be passed in as well. Um, equals speak with spade times out, spade multiply and also passing out speed multiplier or slowdown multiplier inthe e slowdown objects a script so that it's working. Probably cause we're using the same script for us fast ones and are slow ones as well. So if we quickly just make sure that set the right value So now yellow arrow so they don't multiply. Should be more like five. Um, And in our oil spill, it should be like 0.25 So, for you hit play. Now, we should find that. Yeah, we got really fast if we hit that. Well, that's ridiculous. Okay, Jason. Ah, I see a few issues with this. First of all, maybe don't times it in the car controller. Ah, I just said it equal to this people to playa because that was what happened. There was I was constantly hitting more and more speed out ones. And it was Yeah, it's loaded her a lot. So hopefully we did this. Then we'll find that if we had a fast one. Um, if I actually get once morning, it's speeding up. Oh, because it means that the five not like, uh, 15. And for the oil, that should be one. So let's try this again. Oil slows down on and then it will speed back up again. And, oh, where is one of those? I'm just trying to look for one of the speed up ones that one is, but I'm gonna miss it. I'll not go toe would speed up. And well, I hear the person. So every star Ah, what also having is when we got really fast, we went off the edges. So to fix that, we're just going to click on a car and where it says, collision detection, We're just gonna take change that to continuous. So what that means is, if we just override it used, it does a lot more collision checks. Ah, and it is worse on performance. But because we only have one card doesn't matter. But it's if you have a lot of rigid bodies, sometimes you want it. Just It doesn't really matter if it's checking that often, so you can afford to let it not check as often with that discreet option. But make sure change out so you don't have that issue again. Um, and we will keep adding hopefully at some you. Why in the next episode 18. In Game UI Design: Hello. Run. So now we're going to be moving into some You. Why work? So I'm just gonna open up Google and we're going to go back to our Kenny Assets page. So can Kini Don't Okay, And then he wider n l four slash assets. And once you get there, I'm just going to head over to the U Y section and I'm going to download a few different things. So the first thing I'm going to download is the u AIPAC. Um, I'm also going to download the game icons one. Ah, and the fonts one. Ah, you can use whatever images as that she like, I'm just going to get him off this website because they afraid for commercial and educational use as well. Uh, and they are easy to get and look quite good. So of course I'm just going to download them and import them into unity once we're done. So, uh, I just ah, open up purity again. We can go to out sprites photo. All right, click those shown explorer, and I'm just going to make a new photo called one. Could you AIPAC and maybe one court fonts. So, you know, fonts one. I'm just going to extract, um, the moment on the monitor and our drag the fonts into him so we can see that the different fonts are copied over and the u AIPAC. I'm just going to, ah, mix both the game will cons and the regular one into the, uh, your wife act photo that I made just to make it easier. Okay, so we have no unity would say that it's importing them. Ah, this may take a little while, but usually it's not too long. Um, and it's just preparing all the different images so that you can use them in your game if you are going to use funds, which I highly recommend. Ah, you need a don't Tate here for truth type font file. I think it's caught, um, in order for unity to recognize it and you to use it. And so, as you can see, I've got my front in here on, and we've also should have you AIPAC's game icons, black and what as needed. So and also we should have our other you aipac. So, um, we got some sounds in there some fonts again. Ah, and the images we have been looking for. So that's all well and good. So the first thing we're gonna do is create a uh So yeah, this is kind of a sample of what the u I can look like if you make it that way. So the first thing we're gonna do is right. Click in the scene, view here and go to you I and click Canvas. Yes. So, uh, now you need a canvas for all of you different. You are Ah, in Munich. Um, and this kind of groups and is used to render them onto the screen. So the first thing I'm gonna do is go to the canvas scale and see where it says scale with green size, I'm going to set base resolution, cause your game needs to be able to scale, and you need to set your your eyes anchor points and that sort of thing Eso that it works with different screen sizes, which can sometimes get quiet. Complicated. So I'm gonna sound like my reference resolution 12 81 7 20 And in our game window, I'm gonna make sure I've caught my custom resolution. Or you can add one. You can add 12 a where 7 20 here is well and selected so that you're always on that. And then you can use that as a baseline in case you'll see what I mean. Later on about the resolution changing mixing up your, um You Why layout? So first things first, we're going to try and add a timer to the screen so we can see how long we've been going for, um, and maybe a strike counter. So I'm going to try and find to background colors. So I'm thinking something on lines of this great panel should work quite well. So I'm just gonna drag that over into the canvas, E um and it's actually taking it in as a sprite render. So I'm not gonna do that to lead it on this canvas or right click. So you want and go image so that if we go to our scene view against your image now, I really prefer the rectal for modifying images movie or so you I and moving them around. So I'm gonna set this sprite to be the great panel, and we can see that that's working properly. So if you look in a game, you can see the panels there as well. Um, but you can choose whatever it would you like. I could do that One for all that really mattered. And I'm going to add a my counter in the top, right? And kind of try and size it pretty appropriately, depending on how much I live. Bit of space, I think. Um, now these anchor points are used Teoh resolution scanning eso. If I play here, you can see what it looks like with the game. I'm going to come back quickly and think of a look that I find appropriate otherwise. So, um, what I've done is I've got my original background image from the U Y pack. So that's just if I find equally, um I've just grabbed this panel image, and I've just stuck it. And then I've also put another child image, which is just this slider divider underneath it, um, which just kind of gives me to separate sections that I could just play different information on eso what we're going to do Well, what I'm gonna do, you guys can adjust Ur however you see fit is with this image. I'm going to right. Click. Go your wife, and I'm going to select text text mesh pro here. Um, And what this will do is it will bring up a window. Um, you need to make sure you click the important text much per central's button, which should work quite well. Once you've done that, you can just exit out of it. Uh, apparently, it hasn't worked properly. Uh, because I'm in play mode. Okay, here we go. Okay. Um, and now I have, Dammit, My actual image I did was in play mode, so I need to quickly riyadh this slaughter anyway s sir, I'll just join it back on here. And as long as that's looking relatively, okay, I'll try and stick it in the center and just give it a little bit inside it, sticking in the center. And I'm just don't worry too much about the anchor points for now. Um, I'm just and I'll put it as the child for this one. So there we go. If we play now, we should find that it looks right yet. That's good. Um, if you're textures aren't looking very nice. Press control a and change the filter mode. Two point this room normally Guinea tries to, like, blur between the background and different parts for, like, picks a lot. It can cause issues. So if you do that, it will just make it look a lot more sharp art. And if that's the look you're going for So now we've done that Retry. Add this right click you white text much pro. It's adding the text to it. Um, Now I'm just going to try And, uh, first of all, change that takes color to black so I can actually see it. And then I'm just gonna just the rectal So the Texas fits into this top section here, and I'm gonna set it sort of lines to the center of both pieces. Um, and for this will display of time. So I just put in a default time of value to kind of say how look and try and find a size that seemed to work quite well with it. Um, obviously, uh, this, uh, font doesn't look very good. So what we're gonna do is go Well, font section. Now, you may notice if I go to the text and try to change the front at these ones aren't showing up The reason for that is we have to convert them to text much pro France. So we will press the window button up here, go to text, misspoke. Click front asset Crader. And now here you can select what you want you want to use, so Ah, may I might try and use the Kenny high front. Choose which of when you want and you just click teret front Atlas. Um And you can save it however you want start to save it in the funds for again as the custom fund all the front that text miss per likes and it's pretty much identical. Now you'll see that I can actually change it to the front that I want. Obviously, that one doesn't seem to fit very well with this, so I may try toe quickly find one that looks a bit nicer with this one. Ah, maybe, uh, I really I haven't found one that I'm in love with yet. Um, So, uh, this I think maybe this rocket one looks all right to be difficult to tell because their bit blurry in that image for change, that obviously that one's a lot bigger. So I would have to change it. But I think, uh, in this case, um, is that the future one? I'll try the future fund. No surprise importers. Hurry. Thanks much pro fun asset. Create up future. That one does look a fair bit nicer. So maybe we'll stick with the future one. There we go. Yeah, that's something better that I was looking for. So now we've got a time of text here. I'll just call it That s so it's easy to distinguish. And below this, what I'm going to do is instead of having it, So when you hear one person you're immediately at, um will give him three different, like, three different lives as such eso if I go to the game icons pack, hopefully we can find some icons that may work quite well for lives. Um, or something similar. So ah, stars will probably work, uh, warnings? Um, yes. Or civic And find one that sits. Are you choose anyone that you think works best. Um, for me, I think I'm going to just stick with, um uh, i'll do three warning images. Eso you can so, like once you know, you're on the third warning. After that, you're dead, so we'll go to image. Right click. You are at another image. This one could just be a warning signal. Um, and I'll just hold shift. Um, what's doing this? Capes. The aspect ratio showed Looks quite good. Um, and I would just copy and paste this three times trying to make it look fairly scented so we can see in the top right now. Will have, Ah, three different warnings, um, and so on. So hopefully if I can get that in the center, but clearly sentenced Yeah, does seem to be a bit more of a gap between that one and the right one. Um, not too fast at the moment anyway, So if I just put them a ticket So now we got our three warnings. I just name them warnings. Just there. Um, I spent warning or hung, but that doesn't just quickly fix it up. So what we're gonna do is we're going to have a new script, and we're going to call this one U y comptroller. So we'll go to a scripts photo, right click. Great new Say shout script. And I just call this one. You are controller, and we'll double quick. This one. Open it. Now, up the top. Without libraries, we're gonna add a new one, and it's gonna be using unique engine dot You. Why this Allow us to have access to the different you I features when we need to. Ah, and the first thing we're gonna do is have a public float Time left out. Well, so I cannot, um, and would default that 120 f and then we'll have a update function. So this gets run every frame, remember, and will say cannot plus equals, um, time dot delta time. There we go. So now what we should find is that if we put our you, I control a script onto a canvas and we hit play. We can watch this Kanneh, and it's counting the amount of time we've been going so or surviving in this case. So what we need to do is try and display this. So the first way we're gonna do that is have a public text measure. We also need to use another library. So using TM pro, which has takes mesh pro, which is what we're going to be trying to use because it's an upgraded version of unions. You are eso We're gonna have a public text. Most pro you gooey which is basically text on we call this time it txt and we'll just say time attacks dot text is equal to math f dot round, um, and will say out Ah, not cannot Will not your counter that works. Uh and we then need to turn it into a string. So what's happening here is meth after around around his float at two, a whole number, and then we're turning it into a string so we can set it to be equal to the u I, um Well, just dragging out time attacks variable here. Eso that it we don't get errors, and we should find that there we go. We're seeing Al Qaeda, and we will edit this bit later, so that displays the minutes and seconds. Probably like we need it to. The next thing we're gonna do is selected our warnings, and we will take them so that they're hidden. So they're disabled and will have ah, three new variables would have public came object warning one. I probably should just use text because ah, numbers in variable names is not recommended. So try let's stay away from them. So I've got one in one wanting to warning three. Um, And in a stop function, we are just going to set warning one dot Said active to be false warning. Two dots active to be false warning. Three don't set active to be false. So this basically hides a more. Um, and what we're going to do is have a public into lives left at defaults into three, um, and will say in our stop function, to begin with was a lives left equals three. Um, and we're gonna do is create a new function that's called ah, public. Avoid update lives. So this will be used, Teoh. We'll just depending on the amount of lives you've got left, we'll hide at the warning science from on the U. S. I just copy and paste this, um, code here and will say, Ah, if lives left is less than so is equal to it is less so. If there's two lives lefts, then we'll show the first warning. So is less than or equal to two. Then we'll show the first warning. And if there's less than or equal to one live left. Um, we'll show the second warning ending for honor last life. So it is less than or equal to zero. Lives left will show our ah, 3rd 1 else. We will disable the 1st 1 so it would just copy the same thing but say is set not active. So, basically, if there's less than two lives activated otherwise, hide the image and basically do the same for the other ones as well. So just coffee. It's just a lot of copying and pasting here, so I'll just put some whites face in between them so you can see it. So it's if we have less than two lives, enable the first warning because we've lost alive. Otherwise don't. Because obviously, we have full lives on and so forth. Um, and in our start function would just call the update lives, uh, function Anyway, um, And now when we lose our lives, uh, in a car controller, the way we're going to do this, it actually when we hit a failure object. So instead of loading the same, what we're going to do is get out. You Why control? So the way to do this is will open up unit. We'll go to our canvas, will get about tags and we'll add a new tax. So we'll click the plus like we did with the car. Would just call this one. You want, um, goto a canvas? Uh, give it the u Eyetech and we will then instead of in a failure object class instead of loading with scene Ah, we will. I'll just comment that out for now. We will say game object. JIA equals game object don't find game object with tag and the tagger going to give it is a you I because that's what we just caught it and will say gear don't get components. You are controller Dart update lives so that will, um, update the lives. But before that will get the component you are control and say, uh dot lives left minus equals one. So this will remove one life from it. We can also say that lives life equals lives, lifts minus one. But that's just a bit longer and not needed in this case. Um, but you can do that if it helps you understand. So if I go to are you I control now in our update lives function. What we're going to say at the end of it is if lives left equals negative one actually would say is less than zero. So, basically, we're dead. We will just copy this reload seen code, but from the old failure object. But we need also put in using uni engine. Don't seem management for it to work. So this is the line we added. Um, And that will restart. Restart their level when we have lost out three lives. So hopefully if we open up unity now, um, and we drag in out three warnings, image warning images, we should find that we have zero to start with. But if we hit a person, which I looks to say, there's not many of them. Um, Why is this car so slow? Um, seems to be an issue. Okay, so we'll go to a game controller. People will just ramp them up to 70% or something. Ridiculous. Eso Hopefully we can say a few more. Um, Okay. Two lives lost. Um, obviously cause we hit two of them, and if we had 1/3 their lives lost. Now what we're gonna do with our failure objects script is well, at the end of this will say came destroy this start Ah, game object that will just delayed it when you are hit the person as well So that we know so that we don't know absolutely lose two lives because we've hit them twice if that makes sense, Um so hopefully ah If we play now wish when we hit someone Think yet they dive, they die And if we get 1/3 Sorry. So we're on our third warning And there we go. We die because we hit the light We lost all of their lives Pretty simple First life first so to Mabel, third person. And if we had the last one, we restart the game nice and easy So in the next episode will work on more you I features and hopefully had more things to the game 19. Creating A Main Menu: I gave Run. So we're gonna continue working, are you? Why? The first thing we're gonna do is format our timer. So what Just opened back up? How you I control the script and in our update function, when we're sitting the text, we're just gonna add Ah, new line here. That's what we're going to say is after a counter, we're going to say float minutes equals math floor. Um, which returns the largest indigenous smaller than are equal to the value we put in. So we will say timer 0.60 So what this will do is, um, it's like basically, it's around. It's the opposite. A rounding. So it'll always round down, Um, and the most dot to string 00 So hopefully Ah, so it can't is what I meant to put in there. Ah, and I can't convert string to float. That's because that needs to be string. There we go. OK. And we will do a similar thing for the seconds that will say string seconds equals timer. Ah, modular 60. I don't 60 f What is not time? Sorry. Canada, Um don't to strength. So now we should go to say time attacks equals, uh, minutes plus a colon plus seconds. And if we open up unity, we should find that that is hopefully working so few issues here, um is that the seconds are being rounded so we can easily fix that. Um, the where we do that it was Well, just open back up, out second script and we forgot to add the 00 in the two string functions. I'll explain that in a minute. As long as it works. Eso Here we go. That's working properly. The only issue is that it's a bit too big for the actual text. So if we just brought it down a bit So something like 52 and I changed that whilst not playing in the editor, we would hopefully find no still of it too big. So maybe 50 Um, there we go. So we can say that that timer is working properly and as expected, So the reasoning for that the way it works, is where sitting on minutes equal to wait, always running down. So, for example, what we're doing is we're dividing the seconds by 60. So, for example, if it's 61 seconds, then it would this would return one because it's getting the minutes. Whereas if it's, ah, 59 seconds, it would return zero because it's always rounding down because it has been divided by 60. So this would be equal to leg 0.9 if it was like at 15 if the time was at 59. And we're just saying we only want it to be We wanted to be formatted to, um just two characters, Um, and with seconds, we are dividing theme the amount of seconds by 60 again. And then we are just returning that because that gives us our seconds. So we're getting the remainder because we're dividing by 60. So it removes the minutes from and we get the remainder, which is the seconds left. And then again, we're formatting it. So that's how we now have our, um, time. And we also have our, uh, lives which it displayed, which is all well and good eso The next thing we're gonna work on is adding a main menu to the game. So that's pretty simple. The first things were going to do is goto our scenes and right click go great. Ah, new, sane. And what is gonna call this one main menu. So if we open this menu up, we'll get a prompt to save the current scene will save it, um, and will notice we got brand new scene with nothing in it. So this is nice and simple. What we're gonna do is we're gonna add a u I canvas. Now, we're gonna make sure to set a scale with screen size and said it 12 80 by 7 20 And we will add a U Y panel to it and will make sure if we go to our rectal that's filling the hallway around. And this will just be our background color for whatever we want to set the, um, game to bay. So I'm gonna make sure that the Alfa, which is the transparency set to full, um, and you can change your main menu to be whatever you want. So I'm just going to change my background colors to like a green, as if it's the grass of the raceway track. Ah, and that should work out all good. So the, uh, I'm gonna design a menu pretty quickly, and hopefully it looks all right. So we'll go through our you AIPAC we download and try and find different things that should work. Um, and I think the first thing I'm going to do is we'll have to have a title so we can pretty easily good. I seen rightly that panel you I takes much pro will set the font to be what we wanted to be eso I believe we use the future now said at the bolt, and I'm just gonna set it order size, Which means it'll fit the text into the, uh, rectangle air. It's been given, as you can see here and will give a game name Eso we What are we gonna call this game? Um, I'm just gonna call this analyst race because I'm no very, um, creative as such. And I'll just put the title right at the top here and here we go said it's a center, and I can also maybe put a black, but I think why it looks the best and we can add Ah ah, buttons. So what we're going to do is we add a new U Y button. Uh, I'm gonna use a text much per one. I'm going to make sure it's so the bun image here. What? Calabar, Andrea, Maybe a ah, blue one. So if I said it to the blue button, it's not gonna like it because it wants an image. Um, maybe this one work. Here we go. Sorry. So you have to make sure the drag it onto the source image here in order for the button toe work properly. Um, and we can adjust this button to fit on. It's obviously this doesn't appear to be stretching quite good. So I'll try and find one that looks or works the best. Um, which this one does not seem to want to. Maybe these longer buns work. Yes, they look much lighter. Eso Hopefully one of these will work. And if we expand a button here, we go to the text inside it again. Change the font of it and change the color to be white. And this one will just be a start button. And I think maybe what? I'm a do his change The panel background here to be a black or ah ah, like a light. Why? So that the text, um can be like, um actually, what I'm going to do is set the background to be transparent, and we can a copy and paste racing background later on to this, which should look quite good. I think so. If I said the text to be white, um and then I'll change the y pack here to be, uh, green for the button. And we will deal with the button a minute. I'll just create a short background. So this looks all right in a minute. Okay, Dokey. So I've just copied in some background elements so that it looks a bit nicer. I've changed the button to read, and what I'm gonna do is to animate it instead of changing the transition to colored in because of the moment it does like when you hover over it, it will change the color of the object, which does look all right. But I'm going to try as Sprite swap. So what that will mean is that we just putting different spikes for highlighted and pressed and so forth, which should hopefully work selected and press should look all pretty similar. Now what happens is and they select when you pray. So I've just changed dragged in this difference frights that we need, um, and they're working as intended. It does appear that all of these destroying me scripts that from me copping it, causing issues or just a little those on and there we go. So hopefully Okay, we gotta stop on working. Ah, maybe does seem like that Harvard sprite. Um, red buttons here, one that one should bay the Where did the button go? Here? Us red buttons or twos. So one should be there. And red button 02 should be that bump. So hopefully now it looks like I'm pressing it down. There we go. There we go. So as you can see it when you press that it goes down and looks more like a button eso just just the sprites until they look like you want them to. So now what we can do is create a new script. Ah, this is gonna be called main menu. You want just gonna open this one up? I'm gonna do using any engine Don't you want and would late what? In here? And we'll have a public void start. Baden pressed, and we also need to put in using unity Engine dot Seen management eso When we go to a stop button press, which we need to say Seen manager don't load sink. And we it needs I've X. So there are multiple things we can put in here. We can put an i D and name. So where is gonna put in the same name? Ah, and we need to confirm what we caught it. So I called Ah, level saying, what do we call it? Main level. That's all right. So we'll just type in Maine level here so low that so now that we've got a function in this script that we first need to add the script onto this object that may men, you are No, no, no bottom. We need to scroll down to this on click um, be it and want to click the plus. And we're gonna drag in where we put our script so on to our canvas component, we're gonna try and find out Scared, which was made menu I and we wanted to call the start button pressed function when we click a button. So now if you click stubborn, it gives us a narrow and it says main level couldn't be loaded because it has not been added to the buildings. So what this means is that when we go fall build settings, a window will pop up and we just need to click. Add open seeds. You can see a sample seemed in that would remove that, Um, and we need to go into our main level one and do the same thing in the build settings is quick at open seats. So you need to make sure these are both in. If you ever want to export the, um, game. This is where you do it as well. You go into the build settings and click build nice and simple. So now if we click playing, I'm going to start and click it End. There we go. The game starts right away, as expected. So Ah, there you go. If we get our three lives Ah, gonna kill two more people. One and two. It restarts as expected. So, uh, we may want one more button on this main menu, so I'm just gonna copy the button and paste it. And I'm just going to press f in the Inspector to find it again and just move it down a bit . And this button is going to be Al, um exit button. So it's just gonna be a quick one. Quit nice and easy. O que doesn't look quite good. Very good in this. Oh, jeez. Needed his ex. Okay, So what is slated? It quit. Um, And we may be that part needs to be a bit higher, and they go. So what we're gonna do is we're gonna go to our script again. We're gonna add a new function public, avoid quit button press and in here, we're going to just say application. Don't. Ah, quit nice and easy. And that will close the game for us at this weren't working Unity editor. You have to actually build it to test the game. But it should work either way anyway, um, so you can always go far buildings build at to test the quit button works. But it obviously can't quit in the editor because it doesn't work like that. So that's our main menu, Finnell. When we click start, we can see that we've got game working. One last thing we're gonna do is going to remain level, and we're going to go into a black car onto our car control double quick. This any now update function. We're just gonna have a permanent outside of our if statement AdForce at New Vector to and it's going to pay zero in the extraction and just 0.1 f upwards. So what this will mean is that ah, our car is always gonna be moving because the moment it's a time game And if you could just stay like it's obviously how long you can last with the time eso If you can just stay still forever without the car from moving, then, um, you can easily win. So obviously I might You can adjust this value to be whatever you deem necessary maybes There were four might work. Uh huh. And it should Hopefully, yes, I can see that it's starting to move and obviously in this case, maybe even, uh, changing it so that you don't need this. If statement w on that w always pressed. So I'm going to do that. So what this will mean is that you can only move left and right, and it's always constantly going forward so that you don't have any saying that eso it's more of a dodging game than anything else. So I'm not actually pressing any of the keys now, and you can see that they're different things to try and dodge. But you can't change the speed of the actual car because it'll go faster or slower. But depending on the oil spills, eso that's something you may or may know want to do. Ah, but otherwise we have how main menu working. The last thing we're gonna do is in the next episode is add just escape on to exit the game and also a, um, audio filly came and sound effects. 20. Creating A Death Screen: Hello, everybody. Welcome back. So now we're going to be implementing a death screen which will display ah persons final score once they're died or hit their allocated three people. So we're just gonna jump into our main level. Seem we can just do this by double clicking on the scene in the file Explorer, and it will bring up out game view. So what we're going to do, first of all, is we're going to go into a scripts photo, and we're going to open out failure object script because remember, this is the foul, the script that gets run on the people and what it does is it goes to the U. S. Comptroller and caused the update Lives function. So ah, handy trick in visual studio that you can do is you can right click on function and click good definition, and it would take you right to the script and where exactly that is, so you can see the bottom of this update function. We have the restart level when we have lost our three lives. Uh, line of code. What we're gonna do is I'm just going to end to here to form a little bit better or press control K controlled Ito order format everything. And I'm just going to comment out this line here nice and simple. So this will mean that the levels no longer race that straight away. Ah, the reason for this is because of the top of U Y. Comptroller. We're gonna have a public game object, and this one's going to be a death panel. You name whatever you want. I'm just going to call it that. And in this statement, instead of restarting the scene, I'm gonna say Death panel Don't say active troops. And just to make sure that the death screen is an active otherwise, I'll put an else statement and say, death panel dot said active to be false. So what this means is that if we're number, last life will just hide the death panel. Ah, like it should bay. So now when we hope back into unity, weaken, go to well, you. I control the script and we will notice that it has our public death panel. Here s so what we're going to do is create a u I for this death panel. So on this camera, So I'm just going to right Click go to your eye and select panel and we're going to a scene view here, and I'll just press f which will select the object on Jameah accordingly. And I'm just going Teoh scale this panel, Uh, how I think it will fear in the center of the screen. And I can just align it with into the center using the alignment tools, and I'm going to find a background image for it. So what else? Sprites U Y pack. You are packed, fixed in here. We can find something that will look good with a green background. So I'm probably going to go for another orange background. So that's easy out. Select the panel on Dragon. The red panel. Ah, here we can say its currently transparent sort of fix that I'm just going to select the color icon and changed the Alfa or transparency value to bait 255. Um, there is a few issues here with this panel. It does appear to be scaling quiet badly, sir, If there's anything else that can fix, this may be the bottom. It does appear that this panel gist isn't meant to be like that Yeah, it's causing issues, no matter what the images. I guess so. Organ news. We're gonna set it to this Fred panel here, and I'm going to choose the sliced image type now to give us an error here at the reasoning for that is because we need to define where the border is, so it can scale it properly. So we'll select the red panel here. I'm gonna change the mess type to be full wrecked. And then I'm gonna select the spider editor. This will asked me if I want to apply the import settings. I'm just gonna hit. Apply, which means basically says confirm. Ah, so now it'll open up the spider to hear Alfred zoom in a bit with the mouse wheel. You can say that. It's how image what we're gonna try and do is drag these grain edges so that I'll be inside the shape and were essentially selecting where the border is here. So I'm just going to try and make it is even as possible. Um, that looks quite good. And I just hit. Apply. So now if we are pressed across here was say that definitely our panel is looking much nicer and the edges a lot smaller. And it's scaling properly. So I can just the pixels per unit multiply here. I want a bigger Ah, but this I'm gonna leave it 0.65 which looks quiet now, I So now that we could have background panel, I'm just gonna rename it to death penalty. And I can go to a canvas on Dragon. This is the game object. So you'll say that if we play now, we should find that the death panel isn't appearing. But if we hit our designated three people, which apparently I'm really bad at okay, I'm just gonna quickly make them a lot more common character Blue 100% of the time, 12 cheese even then, because everything else is such on day, we will see it appears once we hit l three people there. So what we're gonna do is I'm gonna add a bit of text to it. So the 1st 1 will be some text saying you died or something along those lines. So you crushed with some exclamation marks and are sent to the text here and maybe you fry yellow with some big with a bigger, fun size will look quite good. The foam we used for the all the campus. Let's just check what that was. That was Kenny future narrower. So I'll just keep the same thing for this one. Now you can see it's scaling. Quite really. Ah, that's just because we need to drag the border. So that fits so far. Put it out the top here. Um, we can see that. It should be. Yeah, we got you crash text. Maybe it needs to go to the right a little bit more. Um, and that looks quite good. Uh, that you can obviously change this to, however you want one. Um, I think I might leave it a desert white s. So what I'm gonna do is, uh, just name. It's a title text, as that's always gonna be there. The next thing I'm gonna added a few stats, so I'll just copy and paste his text. Ah will make the next line of its smallest. I'll give it a size of 40. Um, and I'll just drag this down so it takes up the majority of the middle section of this and I will align it to the top but in the center still. Ah, And this will have some dummy text, like distance traveled. Um, 280 meters or something like that. I might remove the bold here, and maybe is her better fonts. Yeah. I think maybe living it is future now it might work. Ah, and that that's gonna be as good as, well, get it for now. And that's all right. So what I'm going to do is it will have a different set of Web. Justin traveled at a time taken, Let's say, like, one minute, 20 seconds or something like that. Ah. And will also have a overall school, and we'll say, like, whatever they are, 7397 is the rule school. There we go. Um, and that's all centered properly and working nice. So we'll also rename this just to be stats, text, and we'll right click on a death panel again. You are and will add 12 more buttons. Sorry. Ah, the first buns gonna be a escape to main menu fun. So this could be world. Disclose one except one. And we needed funds of sprites for it. So maybe blue button or Ah, yeah. Blip on will probably work. Here s so it's nice and easy. Artist changed his transition to the experts. What set the basics right to be that one. And we should find that that's looking all right again. Maybe the longer bun would work better with it. The one? Yeah, that looks a bit nicer. Um, and I dragged the different sprites. What? I icons in here so that it looks tropper, and hopefully we should find that if we quickly else, like, death panel and enable it, uh, hovering over it works and pressing. It works as well. Ah, and I'll just change the text on this sole expand. It changes tax to be exit. Ah, and maybe give it a white, um, bold. And I'll said's I'll set. Maybe the font size to be 40 45 seems to be quite good for this one. Ah, and our copy and paste this pardon now and put another one over to the left inside, try and align them so they look reasonably OK on this button is going to be retry an or just rename it to retry. But so there we go, with good at us total text out stats, text and I'll retry and exit buttons. Um, in the next episode, we will work on implementing them into the game s O Currently, when we die so free hit l designated amount of people. So one more person at this panel appears We need to work on stopping the car, hiding this menu in the top, left on getting these violence toe work properly. So currently they don't do anything if we pressed them. So that's all well and good, and we'll work on that in the next episode. 21. Implementing Death Statistics: Okay, so now we're going to work on implementing your buns from you white we made yesterday and getting the stats to work properly eso and stopping at this time. So just remove out of play mode. And what we're gonna do is we're gonna go to our canvas here, final you. I control script and double click it. Open it. So there's a few different things we need to do here. Up the top, we want to have a public text mesh pro, you gooey and this is just gonna be caught Stats, text Nice and simple. Eso If we go into unity, we can go into this canvas and find that there should be a stats text here. I wanted updates and will just drag that writing. It's now back into a script. What we're going to do is we will say, uh, that's text don't text is equal to I guess the first thing we're gonna have its distance traveled and we'll have the distance. So this is just going to be equal to ah, our car controllers y position. Because remember, when we are playing the game, the cause y position is just going up The whole time. So, uh, this is an easy value to get, um, and shouldn't be too hard. It'll eso we're going to do is remember how we have out tag for car in our script. What is going to say? It doesn't travel, plus game object. Don't find game object with attack the tags gonna be car and would say Don't transform dot position don't. Why? Because this is up and down, and then we'll just add made his own to the end of it. And now to go on to a new line. Ah, in a string or anything and encouraging, you do a back slash, and then you do it. So this will always go to a new line. Um, and for now, I'm just gonna put a semi corn onto the end on, And if we test this out, we should find that hopefully this is reporting the distance traveled, so we'll try and hit out people. And we need to get one more. No, very good at this. Uh, where There we go. It isn't traveled. 5 56 64. Okay, So what happened? There is we hit another person and updated the text. Eso once we make the car stop once we die, that'll fix that issue. Ah, the next issue is that, um it's giving us this big decimal afterwards. Eso to fix that will just get out of play. My going to, uh, visual studio. And with this we're just encloses all in records. Hopes Ah, put brackets in front of this, uh, function that gets the Y position. And we'll just say, um, method don't round two in the front eso if we open up unity again, we should find What that's doing is it's just going around it to the nearest whole number said that we don't encounter those issues we had before. So I'm just gonna go into a canvas here where it says lives left. I'm going to say Let zero now forget someone. It'll instantly appear they go to traveled 42 meters. Ah, maybe we do want to space there so we can just go into, uh, you need and just put a space here and there will fix up the formatting at the next thing we want Is the time taken? So to do this, we're going to do a few things this count of arable. We've got out the front here, so that's easy to fix. Um And I'm just going to copy these two lines here from my update function. Ah, put them above our stats, text here and just paste them right into this. Will give us our minutes and seconds. Are using the same math. Waited before, um and then on to the end of this backslash and I'm just going to plus and I'm going to go to the new line. Just +24 minute. Nicer. But this will continue working. It does go across lines. Eso open up, Teoh. Quite sequence. It's a word, remember? And will dio time taken. Colon can the mood of space. Um And then we will do Plus, and then we'll go minutes and then we'll put em after it. Space plus seconds plus s. So what that's doing is if minutes is equal one and seconds Eagle 23 I would have time taken comma plus one m space and then 23 s or seconds. Eso that should work properly. If we have open up unity now we can it play. I just quickly set the lives left to go to zero now, if I hit someone, it should appear. And they would get time taken. 00 minutes 07 seconds. Ah, what we can do here to fix this slight issue, um, is we can open up video studio. Ah, and we can go and say in our to string method, we can just remove the value we put in here, and we should find that it's no longer forcing it to be 00 But we will have another issue where it still will appear. If we were at your minutes eso weaken. Easy. Fix that, though, Uh, I want to see the heat A person. OK, there we go. It's no longer doing the double zero, but we still have zero minutes and 13 seconds here and the way we're going to fix that eyes Quite simple. We would just do another very one need his strength. And this would just be called time formatted. And this is just going to be equal to what we did here. Uh, but instead, uh, of so then, instead of doing it down here, would just paste in the time formatted terrible we created. But what we're going to say is uh, if below time for minute if minutes is ah, less is equal to zero. So if we've got zero in the minutes, I would say time formatted equals time taken colon space plus just seconds here. So we're just going to say seconds plus and s on to the end. And we should find that this giving us new issue because it's currently a word. So I'm just gonna put commerce around the zero. It'll still do the same thing. So we should find if we open unity. Hopefully, this is working again. Once we hit play, we should just set the lights left. Zero. So it appears they go 03 seconds. So again, we can get rid of the, uh, the variable being passed into the string here. If we want to, uh, fix up, uh, the seconds there, uh, hits. Ah, but this is giving us the large seconds. Sort of fixed that we do have to put the double zero there, which is in the end of the world, we can do a few statements similar to the way we did this to remove that case scenario. Ah, but we'll work on adding the last one, which is the school, and you can basically make this score be equal or whatever you want. But I'm first gonna add a new line. So backslash and, um and then a little plus and I'll go to the new line just because it looks easier. Ah, and what we're gonna do is whatever this score is equal do. So I'm going to say that if I do, a variable up here will say ah int score is equal to I'm going to say the amount off S. O R Kanna is the total seconds. So I'm going to say it's equal to the total seconds. Ah, times the distance traveled. So that's the distance. Travelled is quite easy. It's just the position we got here. So I'm just gonna copy and paste this here, um, and then put a colon on the end here. So this should hopefully at will result in a floats. That's pretty simple. Just do afloat, actually. Sorry, Kate. There's an interview, and I'm just gonna put math f dot round to enter out the front, which will rather toe a whole number because we don't want any ugly scores, and then we can just go back down here and will say, score Colin Space. And then we'll just at the score dot to strength. And that should work or nicely. The issue here is that I haven't looked at this calculation before, so maybe a really big number out of fixed that are just divided by 10 or something. If it is massive or if it isn't big enough, they go score 1 20 Actor. That's actually all right. Um fixes so fire, huh? If we play again Ah, and we go for quite a large distance. So fire change my cars. Why? To be something like 500. We are going to get any massive numbers. Are we? I find changed The lives left to zero on. We hit someone. Okay. Where s so actually our pauses for a second? I will change the counter to be something because I change the wives and 3 500 change Canada Be something will raise will, like 89 seconds. Eso if minute 30 we hit someone. Ah, that school number isn't actually too bad. So I'll leave that calculation as it is. But you can adjusted however you see fit. If you want to make it smaller or bigger. Ah, no. Currently, we're having issues where you can still control the car. This is appearing on these variables updating because we're hitting and moving still eso to fix this Ah would just go into visual studio. And what we're gonna do is we will simply say festival. Ah, we don't have a current came object for Ah, the's panel. But we do have the warning text. So what we can actually say in our code here is we can say warning one don't transform. Don't parent. So this gets the basically the panel Don't setback dot game a direct dot said active to be false on that should work quite well. And now I just do the opposite here in this l statement, and I'll set it to true because we wanted to appear when we aren't dead. So we should find that when we go and play the game and we hit, uh, someone. Yeah. Hipfl are We should find that this panel disappears. Yeah, as it does. That's what we want. Our next thing we're gonna do is make it so we stopped moving or actually, what we're gonna do is make it so weak and stop hitting things as it is quite easy. What is going to disable this collider on the car so that it can't hit things? Um, and as well as that, we're going to to say, Well, the cop control of because that is what's moving it and allows us to move left and right quite easily. So to do this, we will copy out code here to find the car post down the bottom. Say, first of all, get component. Uh, we're going to get the car control and we'll do two brackets to get it. Ah, and we'll say, don't enabled equals false. And we'll do the same thing and get the component this time. It's going to be of Thai boxing latitude box Kaleida today and we just set Don't enabled equally false again eso that war should work quite nicely. I won't put this in the else statement. Actually, I will. Sorry, because this isn't getting run too often. If this was in, like an update function or something, it could cause lots of issues with having to find the car each time. But this doesn't get run very often. All it only gets run four times each game. So there's no problems that Ah, and we'll find that we hit out last person. If they appear. There you go. The car stops up, but it is still moving momentarily. But because the colliders disabled, we won't get this update anyway, even if we do hit one of cars. So that's all well and good. Um, and what we can then need to do is get these buttons working, and then we should be all good to go. So what we need to is without retry buns, we can add to his own click event. Ah, and we'll drag in the canvas. But we need to actually make a function for this to run. So we'll just open back up power. You I script and will make two new functions. Ah, public avoid These do need to be public. So make sure you do that Public void, Ray. Try button pressed two brackets when I'm gonna pass in any parameters and then to curly braces and I'll just do another public void Exit button pressed and again, um, we're not passing any parameters in our curly braces. Eso with this exit of retry but impressed. Um, you can see how we had our see manager load seem to reach start the level that would just copy and paste that code. The retry bond I can remove from here. But if you don't have it, see Manager Load seen. See, managers get the current scene. Don't know. So it's basically getting the current Lowell seen him and just reloading it again. Ah, but what we can also do for the exit bun weakens a scene manager dot load saying and will say, uh, seen would just pass in the idea of it. So I'm going to say, Sorry, I'll pass in the name of the same. And if we're going to uni weaken goto our scenes for and will say that the main menu once just called me a menu. So if we press the exit button, we just want to go to the main menu once again here. We could change this to be equal to the main level, but it doesn't really matter. So if we hit play now, we should find that, uh, when we hit us, it lives zur on they were going to hit him. Time taken while those fires, in essence, look very similar. If I hit retry, um, nothing's happening. The reasoning. That is, if we exit play mode, we go to our retrial and we didn't actually set the function to call. So we dragged in a canvas subject, Go to you I controller and set the retry. But to call the function and the same for the exit bottom was selected. Press the plus dragon, the canvas game object. Select the function to be that you are controllers exit button pressed function And now for your play Quickly sit the lives left zero and we will We hit an oil slick there, um, heat a person. There we go Kart stops this disappears. Retry Barton restarts level, as expected us. And now if I just change the canvas two lives left will just test the exit button quickly. Let me try to find someone there Go exit button and we're on the main menu on We can start the game again if needed. So that is our finished level screen. You can obviously make a lot better and nicer looking than I did happen. Mine was fairly quick and easy. M and I will do later videos later. Other tutorial Siris on animating you y elements because that takes a while in its own so in the next episode will start adding audio and sound effects to the game. 22. Audio & Sound Effects: Hello, everyone and welcome back. So now we're going to be implementing our games. Sound effects. So this will be some background music on DSA hitting sounds as well as like, a steady car engine noise for when the cars going. Obviously, I've just quickly found some sound effects. They do not fit well with the aim, but they are temporary. Ah, if you want download them, just visit this bit Lee Link and it will bring you to these Google drive photo which just contains a whole bunch of different Where Farr's that of sorts from different places that you can use in your game just for testing this all out. You can always see you change them for other wildfires later on. If you see fit So once you don't know that is it far? Just open up unity and I've just extracted it right here. Sir, we have the audio clips. Ah, here. And we can listen to them if we need to sew file isn't It's you I bun sand. We can hear it very quick, but it's plays are like the object He it sounds you can hear, um, works as well. So what? We're gonna dio to begin with. It's creating order your control script. So if going DesCartes photo, we're going to right click create a new C shops. Good. And I'm just gonna call this one audio controller. So where is going to use this? To play all of our sounds? Eso will open it right up now. First things first, I'm just gonna delete whatever's inside it and we're going to start a new function, which is gonna be a public if I spelt public right, public void. Um and this one is going to be just called play audio and what we're actually gonna We are gonna pass in a private here. It's gonna be Cordoned. It's gonna be a type and audio clip. And what is going to call this one? The local variable to be underscore clip. And all we're going to say is cam object underscored or gear equals new came object, and I'll do two brackets. So what that will do is it will create an empty game object in the scene. And what we're going to say is, we're going to add a component to this game. Object is gonna be an audio Ah 40 0! Ah, Give me a second. It's going to be and audio source. That's right. Okay, so we're gonna add component of type audio source, and ah will put two brackets in a semi colon after that. And now we're gonna say is jeer don't get component. What is your source and was, say, brackets don't clip is going to be able to homeschool clip. Okay. And in this function, what we're gonna do is pass in a bullion or true or false value. We're just gonna call this one loops. Um, and we're going to say Just copy this line and we will say dot loop equals loops. Ah, and what we can actually do is different this to fall. So we don't always have to tell us if it loops when we're calling this function. Ah, and what we can do here now is will say public audio clip Ah, backgrounds. Ah, meet audio and we'll do a now start function. Remember this case called ah, When the game first runs eso we all will say it's play audio and we're gonna play a background music and we wanted toe loop be true, nice and easy. So if we open our unity now. Ah, we need to find add goto our gang control objects and we're just gonna Addo audio control a script to it on. Once we've done that, we'll just set out background audio by clicking this circle here, or we can drag it in via the file Explorer so I could just drag in the clip from him. And we should say that if we hit play, um, a new game objectives for it has an audio source and it has the background music clip on it . Now. Currently, it's not playing sort of fix that we need to open back about script. And at the end of this, we're just gonna say, Joe, don't get component. What? Your source. Two brackets don't play and another two pirates and semi colon. And that'll just forced him to play. I usually will play when it's first creative, but, uh, because there is that minute time difference. Oh, and you can say that the Beckham is his, uh is playing very loudly Now there's a volume so sorry. Ah, which you can adjust. Ah, yes, sir. That is playing. Probably now what we will do later on is. Make it so it stops when the car hit something because obviously it doesn't seem very appropriate right now. Ah, by and also implement a global volume variable. So in our script will have a public float volume on. Would they fought this toe one f and it just above this. What we'll do is our square brackets and will say range brackets 0 to 1. What this will do is do the slider in the inspector that we can edit, which just makes things a lot easier. And we're in a player order your function or gonna say is at geo dot component dot volume at defaults to volume. So all of the audio in the game we play will have that volume that we set in this script here, sir, I'm going to lower it down to something a little bit more is warning. You see that 0.5? Um and it's a 0.5 right here. And if you did want to mute the order, what you would do is just said that to be equal to zero and you would say that it doesn't play because what the volumes that desert so that works quite well. Next thing we're gonna do is implement Ah, clicking sound effects. So we'll go to our retrial on. And just where we have our own quick, where does the retry but impressed? What we're gonna do is that new plus, um, and will drag in our gain control here. Okay, So what we're gonna do is we're gonna open up hell, scoot. And we're just gonna create a new function. It's gonna republic avoid that. Play you. Why? Audio button pressed. I just like to get them descriptive as possible on and at the top will have public water. Your clip. Now I'm going to do a list of audio clips. Eso Well, in this case, I'm gonna do in right to do so. I just put two square brackets after it, and I will give it a name off your Why, uh, Boston woodier? Because we have a few different variations for the button press because you don't want the same one every time as this is a nice and easy way to do list of it. And what we're gonna say is, uh, we'll have new variable here. It's gonna be an audio clip, not just to underscore. See is the name Uh And it will be equal to ah the you I but Nodia And we're going to get the elements while the we're going to choose a random number. So it's gonna be random dot range obviously between Zur and you I bought an audio dot length minus one so that should ah work properly. I don't believe we need the minus one, actually and then all we'll do is say play audio under school say And that will default the looping to be off anyway as so, if we go back into unity, we should find that we can add this function in the audio controller player. I order Bun and I'll just do the same thing for this exit button drag in the gang control script pay you I audio bun. Now we will say one slider ship which is when we die. If I just change the lives left zero I am dry when we pressed this ritual. So if I press if I turn the volume up on our first of all in one index out of range because we didn't set up out Hugh y quips So we gotta again control, etc. Where you gotta Uribe on nuclear? I would just change this to be a length off five look things and will drag the different boyo clips in. So we actually haven't, uh, because it's giving us an error there and always saving play. But the era was looking for ah, which we should hopefully say is if we hit retry. You hear that? That ah button press is getting cut off because what's happening is the scenes reloading before the actual clip is getting fully played on. And that's destroying the game object that's playing it because we relied on the scene. So to fix this in our players order your function or we're going to say is ah geo dot eso don't destroy and load. And then we'll drag Energia. Um and we'll add another variable here, which is ah well, just said it to be billions or true or false and will say persist at default that to false . And we'll just put in a statement here so says if persists, then we will don't external, which means that when we change scenes, the object will stay. Ah, but what we need to do it. Now we're having a background or we want to set persists to be actually assist What a man will be false But you know, you y audio we want to set persists to be equal to true Sir, in here we will say false for looping But true for persisting Because what would happen is if we made everything persist This background audio would cape Ah sporting a new on age time we reloaded the same died at which would cause it to sound horrible network Probably so now for hit play, I said the lives left zero turn the volume up and we hit a person and we listen out for retry and you hear the full sound playing properly. Obviously I've got the volume down so that the, uh, background music isn't very loud. But you should say that Ah, it's currently working as expected. So if itis that someone, uh the background odious to playing pot the as you see cause every learning the same. The music back on musical also does restart as well, which is something you may or may not want. Ah, you can fix that accordingly. So what? We're going to do now is we've got one more audio clip here, which is l object Hit eso. We need to play this whenever car hit something. So that's nice and easy to fix, because remember, all of our different objects have pre fabs here. Ah, and all we need to do is create a new script. So what? They scripts create the shop script and we'll just call this one. Ah, objects hit. What do you and will open right up and we'll get rid of what's in here at the moment and will just say, avoid on trigger enter to day. Ah, we would just play the audio clip that we need to to do this. We need to have access to our warrior controller. So we're going to go back into unity, find out audio control and its own. I've gained control, which doesn't have a tag at the moment. Ah Weaken said this to be a gain control attack there. If you don't have that, remember, you can add a tag. Um, and now we have to do is open up our script. Goto object here O'Dea and will say, Ah, game object. I don't find game object with tag game controller Don't get component and it will have our audio control on it. And we will say Don't play audio and we will dragging out object it audios of public audio clip object hit. So this will also allow us to have a custom object like different sound for the different, um, objects if we want to do that, which will be quiet at which you can do the later on if you have more variation in your sound. So if we get all our pre fabs press control a dislike them, we can just click one and hold shift. I'll add a component and where is gonna have our object hit audio? And that's what may take a minute animal on. And we need to set the audio clip to be object hit. No, I said ese Ah, And again, if we play now, we should find that whenever we hit something, Yeah, you can hear the ah sound playing which you can adjust. So if you want to like a squeaking tax, I sound for rum hitting oil and like a speed up one, you can change that accordingly. However you see fit No Ah, What we going to do? We have one more sound. Sorry. Which is going to be a current. And sandwiches just gonna loop the whole time on the car. Eso All we need to is we're gonna add a component to our car. It's gonna be an audio source, and we're gonna drag in our Corange in here, and we're going to set the we contract the object, it audio off the car. Sorry, we don't need that. Obviously, I'll set the this to apply to the prefab. I am well, actually wanted to loop as well. Such playing awake and loop on. What we're going to do now is in our audio controller in the start function or going to say his game object dot find game object with tag car. Don't get component. 40 year old clip brackets drove volume. Also know that I should be working. That's so no audio clip or your source bracket stop. Volume is equal to al com volume, so that will just make sure that the volumes right. So that would just make sure volumes right. Ah, and we should be good to go. Uh, we should find that the current and sound is looping. We can't hear very well over the background. Audio farthest. Okay, it is very quiet. So you may or may not wanna have individual volume controls for that. It's up to you. And so that's a bit loud. And now, um, but obviously you can have whatever sounds you would like playing now. The current issue is, if we kids, if we die, we can still hear both the background audio and the car running. So we want to try and stop that. That should be relatively easy. So we're gonna do is open up, escaped and we'll go to a car control. I'm sorry. No, that car controller now. Ah, game controller. So I can open. This are so if we go toe again, control the script. Here was the level spot of another class. And remember where we have a difference disabling of the different parts of the car when we die, which is actually sorry in the u I control up there were here. So how is setting the different elements? The car to disabled? Unable. Ah, we can easily just add a new line for both of these women. Where now you are controls, grit are just copying pace, uh, these two lines and instead get the component of audio source eso that that's enabling and disabling the at the car's engine sound whenever we're playing the game on up. So we should find that if we play now. Ah, once we die, uh, once we die. So I said lives to three seeking and then the car engines and a stopped. And if we retry, we can hear the courage and sound again, which is exactly what we want. We now need to do the same thing with the background or to you. So I'm just going to go toe all you control the script again. Ah, and the way we're going to do this is I'm going to set a custom tag for so that we know this is the order controller and we can just easily door game objects tag search, eso the weighing induce. Just add another bullying here and say disable on ah level. And they felt false. And no, I have to tell you that the ends if disable on level end Ah, then we'll say geo dot tag He is equal to, um disable on level and and nice and easy. We should find that if we open if we go you I control. Now all we have to do is ah, go to where we're disabling everything here and at a new for each loop doubled up twice. The tabs twice, sir. On it will be game object. So for its game, object will call them underscored here or game. Modric works in game object, Don't find game objects with tag, and we called the tag. What did we call it? We call it disable on level end. Make sure it's spelt exactly the same. Ah, and then we'll say for this game, project would just set active to bay False. And I put one bracket extra in there. So what this is going to do is it's gonna final the different objects in the level with this tag, and it's just going to disable them. Obviously, I don't I think this local variable work because it can cause some issues. Ah, what? Yeah, because that game objects also a local variable, so you can't have that be defined twice. So I just got underscored G instead. That's unity. Think now we should find a different play that Sorry we forgot, You know, order controller, that when we're starting the background audio to set it to this set the actual variable to be true. Asagoe persists falls several on level and true on. Now that should set up properly. So forget play. Now we're getting a compiler era saying call. Ah, Bracket expected. So it just wants an extra bracket here and now, hopefully how we shouldn't get the errors. And we can it play tag disabled level and is not defined. So all we need to do is go toe attacks here. Quick ad tag. And at a new one called Disable on Level and Safe. Okay, play now. And we should find that the background audio has this tag, but none of the other ones do. And if we die, the background order gets turned off as expected. And when we hit retry, it starts full over again. Eso this audio is working as expected now to make this ah game work with the background music a bit better. I'm gonna have it to the cast speeds up faster and faster every time like the song does. So to fix this, I'm just going to go to the car controller and I'm gonna do a new variable. It's gonna be a public buh ah hits oil. That's our public float speed multiplier. And I'm gonna default this to one If, um and all I'm gonna do is in the slow me down a bit in said of setting the speed directly the speed multiplier. I'm just going to Oh, Jesus, the same variable name. So I almost had an issue there, uh, s multiply, I'll say so what? This ah function gets called whenever he had an oil patch for speed up instead of directly affecting the speed, I'm going to set this new variable we created equal to the spade multiply, Uh, and what this will allow us to do, uh, is have it. So over time, our car gets faster and faster and faster. So when we're adding our up and down spade, I'm gonna times it by, ah, the spayed, multiply os multiply in this case, eso But normally it'll be one. But if it's oil, it'll be like 0.1 or something like that and so forth. And before that all I'm gonna say speed plus equals time. Don't delta time. Ah, so what this will do, uh is it will, uh, make the speaker faster and faster and faster each frame. But I'm just going to time this by something like 0.1. Uh, actually, what I might do is have another variable, which is gonna be a public float. Um, speed increase. Ah, And he felt that to your points to if and instead, uh, times by the speed increase value so that we can see how we can adjust it to be held fast. We wanted to be increasing so you can see the speed increasing the speed multiply here. This will also mean that I will have to change the are the area yellow and the oil pre fabs slowdown multiply to be something like So for this one, I would say something like, we want it to be three times speeds are do three. How could getting times remember? But with the slowdown, I'll say something like 0.3 times the speed because we wanted to slow down that bit more. So we hit play now. Ah, we should find that at the spade is going up. Obviously here. Ah, and I think that is probably a pretty reasonable value because it will get very far us very quickly, um, and so forth. But you can obviously make the speed increase be a lot more dramatic. So if I wanted to do something of value like 10 you'd see that it's just going up a lot faster and you can't really go be that, uh so you can try your best. So if I did five here, you can see that again. The speed way too fast, Um, and gets out of control quite quickly. I mean, you can't see what you're doing. So I think my original value of, um 0.2 It was actually quite good there. So yeah, that's what we need to do for our audio. And we've implemented our sound effects. Obviously, you would just do the same thing in your main menu. You just copy over your audio control script on, and then go to your buttons and add the on click event to them and so forth are here this scene, you just press plus and then drag in your order controller. And that would all work. Probably So yeah, that's it for this video 23. Building Your Game: Hello, Run and welcome back. So now we're going to be learning how to build our game. So this is relatively simple, and this is what you want to do if you want to give it to somebody else or you don't want to use the unity editor when playing it. So it's pretty simple or you have to do is go to file build settings. And you should say that both your scenes in the bill Now you want to make sure that your main menu one is above it. If it isn't and it's like this, you can easily just drag it above it. Um, and that should be a well you need to do. If both you're seeing down here, then we have to do is click at open scenes when it's open, and then you would just go to like the other scene and click the same button. So, for example, if I was missing my Mame anyone, I gotta my main menu saying I just click add open scenes and I'd make sure that it was on the top because we want the main menu to launch first. Now, if we do this, there's a few different things we can change. So if we go to edit projects headings I was she a new window opening as this waken set a few things like a company name your product name s If I wanted to call mine racing game, then that would appear in certain areas. You can also set the icon for the file. You can set things like the different full screen modes and lots of other settings that you can change. I'll let you go through these on your own. You can do slash ng reserves. So when the game first launches you, you'll see a screen like this with made with unity. But you could also at other, different ones for your game. If you want to do that as well, so you can just so settings just worth mentioning so close and go back 12 buildings on. All you have to do is select your target platform. I installed the Mac and Linux libraries when we first created. If you don't have those options here, open back up unity hub and you have to reinstall the Mac and Lennix build support. I'm going to set the architecture to be times 86 underscores 64 as this will mean that'll work for 64 bit computers. End 32 big computers. What this basically means is that newer computers and 64 bit older ones of 32 most computers nowadays are 64. Ah, and you shouldn't need to touch any of these other reference development. Build allows you to the console in the game, but I don't want that. So we have to do is click build. Ah, and you select your destination away. You want to export the far So I was going to do a build for a and I'm gonna call it from Windows on and select that folder, and you should see that it's once you selected the destination. Your game will start building on, and you should have everything working properly anyway. Usually, it shouldn't take too long. Um, but it would depend on the speed of your computer as to how long it takes. So once that's done, you should see the file open up here, and you should see your phone exit out, which you can just double click if you're on other platforms. It maybe it's slightly different. Ah, and As you can see it open straight up is a full screen game. We have a main menu here. How quick Bun works, which didn't in the editor. Okay, so you can see that the games working properly you go. You see here and all you have to do is send them all these files or suggest just putting middle 14 and then zipping them up in. I use a program called Winds Up, which is a bit faster, but you can use windows and anyone. And you can understand this file to anyone you want and they can easily run it. And it should work perfectly. So that's the end of this episode, and the next one we will be wrapping up the project. 24. Project Wrap Up: Hello and welcome back. So now what is going to be wrapping up the whole project on and finishing off this destroyer Siri's eso first things first is ah, skill share does a class project Vigia, which I think he's really awesome. And it allows you to share what you've made with everybody else. And I can give you huh? Ideas or feedback If you need that or if you just want to showcase what you've done, everyone else can see it. So basically, what this means is we're love for you to, uh, build your game or take some screenshots of it. And I want you to see if you can publish them on the class page s so we can see what you're doing on and see how you're going with all of this. And if you have any issues, feel afraid of post them on the community section as well. Eso yeah, you just have to scroll down to the projects and resources pay section, click on it and you get the whole project description that I've written out for you on and it tells you what to do, how to upload it. And we've also got download off my final project. If there's something wrong with yours, you can see where it is, or we can use this one to expand and do your own thing from it. So I would love to see what you do. Just hit the Crate Project button. It's really simple. Upload your building, your game, preferably Windows or Mac if he can, Um, and I'll try and download it and play and give you some feedback and see, I just want to see what you guys are doing. Eso. Hopefully this project you made throughout the whole series ends up looking quite good. Ah, the other thing is that I do other courses on the platform. Eso I do currently have another unity to the game development course, uh, which you can go and check out and say it's a shorter one. Um, and hopefully would yield a different type of game. It's a puzzle game. Ah, and I hope that you like it is over 280 people in it at the moment, um and really positive reviews you as a loving it. That's why I'm actually making this other one. So please go check that out. If you haven't already. I would love to see what you think of it as well. Uh, so I'm just gonna go through everything that you've a lot in this course because there's a lot of different things. Eso the first things. First, there's the uni and visual studio installation. You know how to install unity now, nice and easy. Just downloaded from the Internet. There's also other things, like navigating and usually using the editor, uh, learning about all the different functions of the different file Explorer, the inspector window, Same view, all of the different things that uni has with their loud Obviously you haven't gone to in depth, but you have the basics down pat. Hopefully by the end of this, with the different navigating in three D space and that kind of thing as well. Ah, the other things. There's steps quiet for gang creation. What we did from start to finish. We've built the game for the ground up, started simple and added onto it until we got out finished game. We also did lots of different programming things. Eso we learned about variables if else statements at foreign for rich loose a raise lis functions at all of these things. We did a fair bit. There was a quiet, intense bit of coding throughout the course, but all games, if you want them to be in depth, will require a lot. A fair bit of programming at some point on dive tried to hear a minimal, but hopefully you have the building blocks to go and make your own game. From here, you know how to design different obstacles with the pre fabs. You would just drag him in and then I'll list system. You just add a new number to the list and dragging. The pre feminist was born, and we have a way to percentages that we did eso that should all work properly and you can add scripts to the prefab. Remember that drew custom things like our oil spill or the cones that fell over when you hit them. They would just scripts on those pre fabs that work to trade. You had to import out assets. The website we use Can Edo nl. I was extremely useful for that. You know how to create you wise and menus. I will try and do a more in depth to drill on this later on. But yeah, we know the basics for how buns work and text and that kind of thing with text Most pro in Unity Out, which is really cool and new feature that makes it really easy to get started with menus. We have our score, distance and time taken our warning lights in the top left How we disabled them, depending on the lives left, That kind of thing. Ah, you know her build. Next, Put your name to send it to other people, which just did that So you can build for Windows Mac clinics and a 1,000,000 other platforms and tender switch Xbox, PlayStation, whatever you see fit. Apple TV If you really want to go down that path and then there's always the android IOS, uh, section as well. You just have to do a few different things. Those different platforms, you know how to implant music and sound effects were that towards the end as well. Hopefully you come up with a sound affects united that fit a lot nicely, but yeah, it was quite difficult to find ones that suited this type of game without having to make them myself. There's also basic enzyme principles that we did from start to finish different aspects of game design. I wouldn't go too in depth into it. But things like how the play feels when they're playing at white. They want to keep playing the game, those kind of things. Hopefully, you can kind of extra plate what you've learned from this type of game and just essentially thinking of all the different aspects against on art audio programming, Um, level design of you. Why just all the difference areas of design coming together for game design, those kind of different things that we've done throughout. So, yeah, if you, uh, I would appreciate it if you head over and hit the follow button, which is just underneath the title of the page, you can get updates from May and see any new course. So posts. Ah, and it really, really helps. Ah, and you can also save the course if you want to come back to it later or you want to check up and see if I've changed some things or come back to it later on. So yes. Please make sure to challenge yourself. Try and improve your game. I've made a list of something that you could add, but you can always go and have. Mawr doesn't have to be on this list, and I'd love to see what you can do with it. So more sound effects, obviously better ones. And I have a two level system, which wouldn't be too difficult. You could, For example, different levels could have time limits score score requirements to finish them. Distance requirements are lots of those different things in a level. Menu wouldn't be too hard with the system we've made. You just have different scenes and inhumane men. You'd have buns that load the different scenes room with a scene manager code library that we imported at the top of our script. You just go see Manager, don't load scene and you say, like level one, level two, level three and you just hit the plus on that button on the main menu, and it would just take you to that scene and then in the different scenes you could at different objects. So some be harder than others. Those kind of things, and you could easily in your level spawn er class, I believe, is where finishes the gamble you are control. One is test to say Okay, if the distance is greater than 500 meters, then side a past. Otherwise making retry the level. Those kind of things wouldn't be too difficult. And hopefully you can do that. With what I've taught you, you can create your own art in style. His version of the game. Obviously the out we used was free and from the website. But you can make it your own. If you're an artist, do that. That would be amazing. I love to see your projects. If you do go down that path, I would be great to see. I have cast selection menu so you could have different colored cars of different speeds handling steps, those kind of thing which make it more of a strategy like what you're choosing. You could have it so you get queens or something. We unlock new cars or it just really wouldn't be too difficult because you just be adjusting the variables and the sprite. Remember that images. So if you have a red car with a speed of 10 or something like that, versus a black car with the speed of five. But I battle handling or that kind of thing so you can adjust those values and make it a bit more interesting. More obstacles. Obviously we got, um, a variety in their at the moment. You can always add new ones, do different things, and you just change the pre fabs Adams list. As we said, Um, and you have your object in the level are nice and easy. Um, multiple hosts. That's amore advanced. When we haven't talked about that too much, you could even have ah, computer controlled crowd a race against now. Obviously, that sounds rather scary, but a Iot computer controlled. Oh, that sounds really scary. But really, in games where you have a computer racing as you, it's really not that difficult. Um, all you would have to do is have it so day of checking to see if there's an object in front of them. If there is, then you could have, like a random number generated 01 They go left or right and avoid the obstacle, and you could just set it so that their speed is matched with the game or something like that. And they just always could be going forward. So on this type of game, a computer controlled of opponent wouldn't be too difficult. You could even have moving obstacles to like racing bikes moving back and forth across the level that you have the door draw that kind of thing, really not that difficult just at a script them Put him in a prefab Adam to the list and you could go, Ah, levels with turns of corners. Obviously, with system we've got, that would be difficult, but something like a temporal in style level where you have different turns and corners and stuff that makes a lot more interesting would be really good to see. And if you could do that or want help doing it, um, you can always missing your school share or sent me an email after and the latest light. But I would love to see someone do that if you can. Uh, I won't go into too much detail as to how you can do that in a moment. But, yeah, feel free to send me a message if you want to. Um, also make sure to please leave a review positive or negative. Give feedback. I'd like to know what you think about that class, um, ends. They really help to let others get an opinion as to whether to go through with this course has a fairly long one as well. So, yes. Thank you for listening. Um, enjoyed it. Remember to follow me to keep up to date with any other new causes. You can check out my old ones or other ones with on similar topics. Um, and feel free to tell anybody else about it. Stick to gain development. Um, number one thing, that's all I really care about is make games that have found that you enjoy and get into it . And again, if you want a message. Me messed me on skill share. You just go over to my profile and click message. Or you can send me an email jack turns and 005 a gym and try to get back to you. Um, as fast. Just please try. Make sure in that if you do emailed me to say that you're from school share class because it's difficult to tell who you are, that kind of thing. But I'll try my best to help you. Uh, however you go. So Yeah. Thank you for listening. And I hope you enjoy it.