Unity® 2D Mobile Platformer Course: Part 4 - Sixteen Game Mechanics You Should Know | AbleGamesDev . | Skillshare

Playback Speed


  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

Unity® 2D Mobile Platformer Course: Part 4 - Sixteen Game Mechanics You Should Know

teacher avatar AbleGamesDev .

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

18 Lessons (2h 23m)
    • 1. Simple Coins Pickup Behavior

      4:36
    • 2. Create the SFX Controller

      8:03
    • 3. Enhanced Coin Pickup

      9:10
    • 4. Toggle SFX From the Inspector

      1:54
    • 5. Improving SFXCtrl Script

      5:10
    • 6. Spinning Coins

      5:33
    • 7. Bullet Powerup

      9:35
    • 8. Dust Particle Effect When Player Lands

      7:12
    • 9. Breakable Crates Setup

      4:42
    • 10. Breakable Crates Scripting

      18:24
    • 11. Create the Game Controller

      2:01
    • 12. Create the garbage collector

      3:26
    • 13. Reload Level When Player Dies

      7:22
    • 14. Splash Effect When Player Fall in Water

      5:49
    • 15. Dropping Platforms

      13:38
    • 16. Moving Platforms

      14:50
    • 17. Scrolling Backgrounds aka Parallax Setup

      4:54
    • 18. Parallax Scripting

      16:33
  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels
  • Beg/Int level
  • Int/Adv level

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

83

Students

--

Projects

About This Class

DOWNLOAD LECTURE RESOURCES

Some Game Mechanics You'll Learn:

  • Simple Coins Pickup
  • Create SFX Controller: Professional Way to Show Particle Effects
  • Sparkle Effect When Coins are Collected
  • Spinning Coins
  • Bullet Powerups
  • Dust Particle Effect When Player Lands
  • Breakable Crates
  • Water Splash When Player Falls in Water
  • Dropping Platforms
  • Moving Platforms
  • Scrolling Background aka Parallax
  • AND┬áMORE!
  • learn the step by step process to create these game mechanics using Unity┬« game engine
  • covers helpful tips and tricks
  • Scripting is done in C#

If you want the sprites and assets used in this course, you can download them for FREE here

Meet Your Teacher

Class Ratings

Expectations Met?
  • Exceeded!
    0%
  • Yes
    0%
  • Somewhat
    0%
  • Not really
    0%
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. Simple Coins Pickup Behavior: collecting objects in a level and then getting points for it is an important part of platform and gaming. In this video, I'll show you how to make a simple coin collecting behavior for the player, like So All right, so let's get started. Go to the coin tree fab and we're going to add attack. Hold coin. So let's see how to do that. Go to level map in selection mode. Now let's select this coin and click fine prefab. Make sure the Project window is highlighted. In this case. When you select a game object and click fine prefab, it's gonna pop out. Here it is. Now I want you to click on ad tag and let's name this tide coin See of the project selection mode Fine prefab and now changed the tag to coin. Save the project. Next click add communed, typing coin CTR elf hit, return to times and double click to edit the script. Let's begin by writing the summary hand Luz de coin Behavior Wendy Player interacts with it . Now let me show you the man we're process first and then I'll tell you how to get it done through scripting Okay. Immunity Let's get back to the coin prefab. So let's use selection mode. Select this coin click. Find prefab. Here it is Now notice. This coin has a circle clatter, tooty, and this is a trigger. His trigger has a check mark. What this means is if I select the cat game object and the move tool and I start to move the cat in the horizontal direction like so notice at this point because the coin has a trigger, the cat will be able to pass through it. Okay, it's as if there's nothing. It's invisible, but something really important happens as the cat is passing through the coin. The on trigger enter tooty method is called. So let's write it back in the script. We're not going to need these methods. So let's to lead these guys and let's write void on trigger Enter tooty. And now here's a quick trick for you. If it's a trigger, it's going to be a collider. OK, it takes in a parameter cool, lighter tooty there and let's name the parameter other. So the trick is, if you're writing on collision, enter tooty. Collision will always be a collision to the on collision introduced e will be a collision to re and on trigger. Enter Tory will be a collider Trigger Collateral Trigger Collider. Try to remember it in this manner. If you're writing a trigger that is going to be a collider. Okay, so let's check if other dog game object. Ah, compare tag. And I told you compare tag is better in terms of performance, and we want to check if its a player remember to close these brackets. Okay. These are common editors. Well, in this case you wanted, simply destroy the game object. Now, make sure you're not using the class. Remember, these icons tell you if it's a class or it's a property. Okay. In this case, it's a property game. Object. Okay, this is what we want. And this is pointing to the current game object. Okay. The game object to which quien control is connected, which is going to be the coin. So the moment the player and turns the trigger, this method is going to be called. And then we're checking if the game object which has entered the trigger is a player in that case, destroy this game object. The one that's connected with the equine shoulder script. That is the coin. So destroys the coin and that's it. And now if you test as the cat passes through the coins, the coins disappear. So it looks like the cat is collecting the coins. Right? So this is a simple coin pickup behavior for the player cat. Now, imagine if the cat is speaking of the coins and you see a nice sparkling effect as the coins disappear. That would be cool, right? That is what you call showing a particle effect in unity. So there's a standard, best practice that I use, and I'm gonna introduce you to this practice. Using this, you can start to show particle effects in the most effective manner in your unity games. All right, so join me in the next video and let's get started. 2. Create the SFX Controller: in this video, I'll show you a best practice that I use for handling particle effects or special effects. So let's get started. Select controllers in the hierarchy, right click and select. Create empty. Now let's rename this guy to S F X Ctrl now click. Add component and type in SFX. See TRL. It returned two times and double click to edit the script. Let's begin by writing the summary, Let's say, handles the particle effects and other special effects for D game. Now we will need a public static instance off this class or in simple words. We want to be able to call methods in this class from other scripts without making an object of this class. Okay, this is classic, object oriented terminology that I've been using. So it's recommended that you go through the object oriented programming concepts. Otherwise, all of this will not make much sense to you. So essentially, we're going to declare a public static variable of type SFX controller and named the variable instance. And let's give a comment allows other scripts, too, access public methods in this class without creating an object of this class. All right, and now we're going to declare the week method. This awake method is a part of model behavior, and we're going to say, if instance is equal to know in this case instance equals this. So with this, if condition were checking, if this variable instance does not have a reference to anything right now and is no in that case, a sign, a reference to this class as of X controller and stored in the variable call. Instance here and this variable instance is a public static variable of type, as affects controller. So now what's gonna happen is other scripts can simply call in a suffix controller the class using dot notation type an instance and then using another dot when we have more public methods in here, we can involve those methods very easily. Okay, so we don't need to create objects of this class. SFX controller. Let's declare a public method so it becomes clear to you Let's a public void. And let's call this method choke coin sparkle like So Okay, now, if I use or if any other script uses SFX controller dot instance dot now, watch you have a show coin sparkling here. If you remember from previous videos. In this course, I showed you a best practice off using a summary. This is where it comes in handy. Imagine you have a method show coin sparkle. You haven't specified a summary, so you don't know what this method does. Rights always try to use a summary here shows the coin sparkle effect when the player collects the coin. All right, so throughout this course, I'll be using this standard practice. It's a best practice. If you start to use this, your coat and your project will become more professional. And other people, when working in teams, will be more comfortable, and they will find your code easy to read. So in this case, show Klein Sparkle has a summary shows the coin sparkle effect when the player collects the coin. So now you notice you did not need to create an object off SFX controller. Using a public static instance off a class and then invoking public methods is a standard best practice, which simplifies a lot of things for you, and it is generally recommended to use this practice for controllers like your game controller. The ads controller yard, SFX controller, your audio controller and in this course you're going to create all of these controllers. OK, so we're going to be using this approach. So it's important for you to understand this technique. Okay, watch this video. Ask questions in the discussion forum. I'll be happy to help you out with this. Get comfortable with this concept and you'll find it very handy in your project's. Okay, so delete this piece of court from here, save the project. And in the show coin sparkle method, you're going to show the coin sparkle which will be a game object and technically, a particle effect. How do you do that? You used the inst enshi eight. Any time you want to show something. Orenstein. She ate a game. Objects you use in Sten. She ate and station three parameters to game object to shows. Let's say SFX underscore coin pick up and then it needs a position. So we're going to say pos, and then we don't want any rotation. So this is it. Now notice these two variables. SFX coin, pick up and position. They are in red, so let's begin with position. So let's pass in a parameter of Tig Lecter three and call it position. This will be the position where the coin was before it disappeared. Okay. And instead of the coin, you want to show the spark low. Next, what effect do you want to show? So I have this naming convention where I prefix the kind off game object it is. It's gonna be a special effect, writes I prefix s FX. And then an underscore. And then I named a variable coin pickup. Let's copied from here and let's declare a public game object this time, remember, it's gonna be class. Okay? It begins with the capital. This is C sharp convention game object, SFX coin pick up. Give a calmed the particle effect to show when the player pigs the coin. Okay. From this part of the course, I'm going to start using all of the standard best practices, like commenting the code using the summary. So if you're serious about unity as a career and if you really want your code to look professional, start using these best practices. Now I want you to do lead the start and update from here for the moment, it's not needed. And then when you go back to unity and you highlight a suffix controller you load issue have, ah, public field, SFX coin pick up and currently enjoying none. So we need something in here, Right? So what do we need? We need a particle effect. Okay. The coin sparkle effect for that Want you to click assets DoubleClick, pre fabs and in here, right click select Create folder and called his guy SFX Double click the SFX folder. And now please go ahead and download the particle effect. The link is attached with this video, and after downloading right click select Import package Custom package, select an open and click import in case you see that this is unchecked. This is because you already have the spark a lot PNG in the sprites folder. OK, select import. And if you notice you have this hair, don't worry. We already have the destroyed a delay script. So it's a duplicate. So click project under script here. Selectee helpers folder, right click and delete it. They're not takes care of their next I want you to select SFX Controller. Click this icon and tie band SFX And this is the coin pickup sparkle that you just downloaded and imported selected and we're done. Now save the project. And this completes a basic set up off the SFX controller. In the next video, I'll show you how to use the SFX controller to easily show these sparkle effect when the player collects the coins. Right, So see you there. 3. Enhanced Coin Pickup: adding particle affection. Your games is a great way to improve the visual appeal off your game. By the end of this video, you would have learned how to create a nice sparkling effect. When the player collects a coin like so you know it is thes sparkles. This is the effect you will learn in this video. Okay, so let's get started. Select the CAD game object and in the Inspector double called the player controller script to edit it now in the player controller script. Have a look indeed. Document outline. If you scroll down, I want you to click on on collision inter duty and this takes you directly to the place where you have the method blue. This method I want you to write void on trigger Enter two d now using mine demonic. I told you, if it's a trigger, it's going to take in a collider. Tooty Okay, Trigger Collider Trigger Collider collision will take in collision tooty because this is trigger enter tooty. So we have a collateral tuti and let's name the parameter other and let's open and close the brackets like so And now this time we're going to use another conditional called Switch . And when you type in switch, intelligence gives you two options for court completion. So let me select this guy and you hit Tab Key two times. You can have this cold completion for the parameter in switch. We're going to pass in other dot game object tot attack like so, and you can hit the escape key to remove the green color. Now let's copy this and paste it here and let's select the case. The cases when the player and terse the trigger off the coin. What do you want to do? In that case? Well, the coin has a coin controller, so that'll take care off, destroying the coin or deleting the coin. But now we have a specialist taking care off your special effects or particle effects. So we're going to tell the specialist Hey, SFX Controller and notice. You have the intelligence telling you that this is the guy in charge of particle effects. You want to hit return and then, using the doc notation, call it's instance. This instance is a public static in distance, and here's where the magic starts to begin. You can use another dot notation and call the show coin sparkle method because of the summary, you know that this is the method you're interested in is gonna show you the sparkle effect it returned, opened the bracket and it tells you it takes in a vector three position. So let's enter the position as other dot game object. Ah, transform dot position and that's about it. Now, let me explain how we got this. Okay, This looks quite a big piece of the puzzle, but it's not a puzzle, really. Once you understand the mechanics behind it first you need to begin with the other. What do you mean by other? If you notice this on trigger Inter tooty takes in a parameter called other. The type of the parameter is collider toady. Now this player controller is attached with the player. Okay? And here's a coin. The coin has a circle collider tooty, which is a trigger. So if the player interstate, this trigger on trigger enter tooty will be called right, this other will refer to the Circle Collider Torrey off the coin. Now you know, the other is pointing to the Circle Collider tooty. Now watch using the doc notation Your writing game object. So we're asking Hey, I want this collateral tooty to figure out who is the game object. That's cool attitude he belongs to. So here's the answer. This is the game object. So we got the reference to the game object using other dark game object. Now, from this reference, we use another dot and say we want an access to the transform off this game object. So here's to transform component off this game. Object. So up till this point, other dot game object are transform. We got the access to this transform, and now this transform has a position property. So we right, doc position and bingo. So this is a step by step process off getting to this part using the Circle Collider tooty or a collider. So from clatter, you went to the game object from game object you got to the transform and from transform you could get to the position. The reason you wanted this position is this is the same position where the coin is here. When the player enters the trigger, the coin will disappear. OK, but you have access to the position of the coin and this is where you're going to show the sparkle effect and the method used is show coin sparkle off the SFX controller. Okay? And you may also asked me, Why are we using a switch case instead? Often if then else block. Well, that's a very good question. And this is because which case is better in terms of performance as compared to an if then , else block. You'll notice that as you progress through this course, you'll have a lot of conditions. In that case, switch case will be better. Next. I want you to open the coin controller so you can open it from here. You can type in the search, say coin controller, and you can click this icon with the sea and the coin controller opens. There's another shortcut. You can hit command dot that takes you to the search. You can type in coin, shoulder and hit return. Now the reason why I came to a coin controller is let's get back to unity and let me show you this coin. This is an instance, so let's get to the prefab by clicking on select and here's the prefab. Imagine this is D coin controller script. What if you had a drop down here, which could allow you to choose the effect you want to show when the player collects the coin, because up till now the coin simply disappears. It's like kind of vanishing effect. So is there a way to add a drop down to specify the effect? That would be cool, right? So it's very easy. Let's head over to the coin controller script and in here, let's type in public in, um and let's say, coin effects open close, Parenti says. Let's type in vanish and fly as the two options save the script. In this video, I'll show you how to use the vanish effect, and when you reach the section on creating heads up display, then you'll see how to create the flying coin effect. Next, declare a public variable of type coin effects and name it coin effects. Like so and now back in unity, have a look. The Coin Controller script has a nice drop down called coin effects, which has two options vanish and fly. Because Vanish was the first option in the script, it's checked by default, so we're going to use vanish. Using this variable we're going to check something in here. We're going to check when the variable kind. If X is equal to coin effects this time we won the een. Um, Remember, this e signifies an in, um and in, um has two values fly and vanished over. Interested in vanish. When this condition is true, we want to destroy the coin. Okay, A simple, vanishing effect. And that's about it. So when you reach the section on heads up display, have a look at how to do the flying coin effect. Now, at this point, there's something else you need to do. Remember the prefab Pacific's folder? We haven't SFX coin in here. There's a script that that is missing right now. So I want you to click this and typing destroy with delay. And that's it. The error messages gone. Okay, Have a look. This sorting layer here, see, is unknown there. If the's properties are not visible, you can click render and it shows you the properties. So have a look for sorting layer. I want you to change this to foreground and then save the project and that's it. Let me select the cat and move the cat a bit to the left and let's go to play mode. And now you'll notice as the cat collects the coins like so we have a nice sparkling effect , right? This is pretty cool. Well, now, if he exit d play mode at this point, here's something that I want you to know. We have included a dependency in player controller. Let me show you. Let's go to player controller like so and have a look. Any time the flare picks the coin, the SFX is being shown. And for this SFX controller, you would also need access to the SFX controller game object here. Okay, so this is a dependency. What if you just wanted the player to pick up coins without showing the SFX? My question is, do you always have to come here and maybe commented out like, so well, turns out there's an easier way, and I'll show you that in the next video. So see you there 4. Toggle SFX From the Inspector: Now there are situations where you may want to use SFX, or you may want to turn it off from the Inspector like debugging the game or exporting the cat game character as a package and so on and so forth. By the end of this video, you would have learned how to turn off special effects on and off by simply using a check box like So it's easy and let's get started in the player controller script. Let's declare a public Boolean variable called S FX on like, So save the script. Now when you go back to Unity and you select the cat Game character and have a look at the player controller script notice you have a boolean variable called SFX on It's a check box , right so you can check and uncheck it. All right, let's move on in the on trigger. Enter to re block, we're going to write. And if condition if s FX on like so then you want this lying to get executed and that's about it. This is the one line of code that helps you toggle the special effects on and off from the Inspector. Let's have a look Let's head over the unity and I'm going to go to the play mode. But SFX on is unchecked, lets you what happens? Here's the cat. The cat picks up the coins and the particle effects or the sparkling effect was not sure. Now I put a check here back to the play mode. I hadn't noticed. Thes sparkling effects are back on. All right, so now you know, it's easy to turn the SFX on and off from the inspector, right by simply checking and uncheck ing a public bullion field. So join me in the next video. We'll show you another best practice to improve the SFX controller and give it a more professional touch. Right, So see you there. 5. Improving SFXCtrl Script: There are some courting standards and best practices which make your code and projects more professional. In this video, I'm gonna show you one of them. This is related to keeping a script neatly organized in the Inspector. So first, let's begin by seeing the problem that this technique solves. Highlight SFX controller and have a look at the SFX Controller script. If you have less than 3 to 4, public feels to get references for particle effects. It generally looks okay, right? Have a look now over in the script. If I copy this one time like so, and I paste it maybe three or four times Now, if you head over to unity, you'll notice you have about 3 to 4 Public feels like, so this generally looks OK. So far, so good. But in big projects there can be easily more than 10 to 50 particle effects, and you'll have a huge number of fuse, which kind of looks clustered. You can see this is what it's gonna look like. And this kind of looks clustered right, especially when you start adding more components. All of this doesn't look good, So is there any easy way to kind of organize all of this stuff, maybe have a narrow like so which I can click to expand and collapse the items. If you had noticed. If you click edit, go to project settings and input. We have exactly that kind of an arrangement. These arrows allow you to see these fields, and then you can collapse and expand them according to your preferences. So how can we implement it for these big amount of fuse? Well, it turns out it's very easy to do that and let me show you now. The first step is to create a new script called SFX. Now go inside your Scripts folder and right click and select create C Sharp script and named this guy SFX and then double click this to edit this script. Let's begin by writing the summary group's de particle effects used in the game. Save the script. Now I want you to delete this super class from here. The reason is this will be a simple C sharp class. Okay, so we don't need these two methods I want you to do leave them, and instead I want you to declare a public game object Field called SFX Underscore coin underscored Pick up and let's write a comment shown when the player picks coins. Okay, so this field or variable will store particle effects. Now, I also want to use an attributes, so I want you to change this to using system. And then when you hit return, let's do leave this and open and close the square brackets and typing See realizable And this serialize herbal attributes allows the SFX class to be displayed inside the inspector along with this public game. Object as suffix coin pick up field. So before you can do that, there is a small thing you need to do because this is a simple class. Let's declare an object of this class so here lets a public S f X and let's name the variable SFX. And now you'll notice when you go back to unity and you highlight as affects controller notice you have the class SFX with the public field SFX coin pick up right. This is cool, right, because we also see an arrow. You can click the arrow toe hide, ensure this public fuel already looks like we're getting there, so I want you to delete this now your Lord is the woman's. You did that. This guy turned red as if it's angry. Well, it's not really angry. There's no variable of that type in the SFX controller script. Instead, I want you to write sfx dot as suffix coin pick up. So far, so good. Let's get back to the Inspector. And now instead, we have only one arrow, which is called SFX, and it has this SFX coin pick up field neatly organized. Let's typing this affects select this guy and looks like we're done because if you go to the play mode, notice the cat is ableto pick up the coins and the sparkles are being shown. So now just assure you if I have about 2025 feels in here, it's gonna look good. Let me show you. They're notice. We have about say, 2021 feels and if you kind of and if you want, you can hide and show them as you want. So there you have it. Now you know this technique off neatly organizing your script within the inspector. Now let's go to the play mode and show you something Up till now, these coins are stationary. They're not moving right. What if you could make them spin around? That would be cool. Right? So in the next video joined me, and we'll just do that, make these coins spent. 6. Spinning Coins: adding animations to collectible objects like coins can improve the visual few off your game. So in this video, I'm gonna show you how to easily add the spinning motion to the coins, and the cat can collect the coins. And there's the beautiful sparkling effect. All right, so all of this is really easy, and let's get started. Select the level map. Make sure it's activated. Now select selection mode. Then I want you to highlight or select one of the coins and find prefab, and it starts toe jump at you. So select this coin and I want you to duplicate it. And let's rename the duplicate to spinning coin like so. And you may want to increase the preview size to have a better visual feel of what's going on. So here's the spinning coin. I want you to drag and drop it here onto the scene. View notice In the hierarchy, a game object with the name spinning coin is created. This is the first step. The second step is click window and open the animation window and then drag and drop it. Here. Next to the scene view, select spinning coin game object. You'll see a button like this click to create the animation clip and controller and we want to save it under the animations folder, create a new folder, call it Coins. And in here let's name this guy spinning. Okay, this is the name of the animation you're creating. So here's the spinning animation we're creating first. I want you said the sample rate to 20. Now this is the starting frame frames Euro. We're going to keep changing the scale for the X value. And doing this will make the coin spin around. Let me show you in the scene view. OK, make sure you have selected the spending coin game Object. Now, if I had started changed the value for the X notice. The coin is kind of spinning right? It looks like it's spinning. So simply by changing the value for the X weaken, simulate the spinning motion. So let's restore the Studio one. Save the project, get back to the animation window. Now we're going to divide the animation sequins into a number of key frames. 40 key frames, to be precise. Okay, and I have attached a pdf along with this video, you can keep it for your reference. I've given you the key frames and the values for scale. For now, I'll show you the man were process. Frame number zero will have value one frame. Number five is going toe. Have values or a point hate notice. Every time I make a change here, a key frame is automatically created by unity. Next, let's go to frame number 10. The red bar is here, but there's no key frame. I'm going to make change. You're change this. Is there a 0.6 hit return and their moment? You had a change. Another key frame was created, so we'll repeat this process. Still frame 40. Let's go to frame 15. Change this toe. Zero point for frame 20 Change This does, or a point one there were going to repeat the reverse process. Now, from 25 we're going to increase the value. This gets back to a sore point for frame 30. It's 0.6 frame. 35. It's gonna be 0.8 and the final frame frame. 40. We'll see the coin returning to the value off one, and that's about it. Switch to the game view and slightly increased the size so you can watch what's going on, all right. Making sure the spinning coin is selected. Now I'll press the play button and watch what happens to this coin tear. Notice. The coin is spinning. It's showing you the spinning effect. Perfect. So you just learn how to create a spinning coin. Now, I want you to stop the recording and the play button returned the game view to its normal size. You can close the animation window now you can go to the play mode and notice this particular coin is spinning while these coins have no spinning motion. All right. And when you collect this coin, there's gonna be the sparkle effect. Perfect. And the last step that remains is you've been making changes to the instance here, right? This guy, this is an instance you need to apply it back to the pre FAP, so I want you to select apply, and that's it. All the changes you've made to this guy here the instance have been applied back to thes spinning coin prefab. And what this is gonna allow is all of these guys will have spinning motions, go to the play mode and notice all the coins are spinning and they have the nice sparkle effect. Right? And this guy over here exit the plane mode. You can delete this spinning queen game object, and that's about it. You have a nice spinning coins effect. Now. Notice We have allowed the cat to fire at will. Okay, Now we're going to make changes to our game so that in the beginning, the cat is not allowed to fire bullets. There will be a bullet power up which the cat needs to collect to start firing bullets. The next video, we'll show you exactly how to get this done. All right, so see you there. 7. Bullet Powerup: up till now, you've seen that the cat can fire bullets at will like so when repressed, eat space bar. But in this video, I'll show you how to change the rules of it and make the game a bit more realistic. So let me show you what I mean. I made some changes to the project. And now when I press the space bar in the beginning, the cat cannot fire bullets. So what can we do about it? You know what is this key up here? This is D bullet power up key. The rules of the game are now that the cat has to collect this key by jumping up here. And only then the cat can start to fire bullets. Like so. Okay, so this makes the game a bit more realistic. So let's dive right in and see how to get this done. The first step is to create the Bleidt Power prefab. For this will be using the pre fabs boater, and I want you to drag and drop this window here. Next, I want you to look inside these price folder and you may want toe increase the preview size and let's look under the items folder and ah, we have this red key. Okay, I'm gonna use this for this example. So select this key and click. Create pre fabs. One prefab is created. Click OK, select the level map, and if it's not active, you can activate it. And now you'll notice you have a red cutter key here, and it shows in the preview. But we want to change the name of this key. So look inside the L B Tiles folder. It's under Editor Defiled Resource is L B tiles. And here let's change the name of this key Teoh Bullet key. Like so. And if you go back to the level map now you load is the name is bullet key. So far, so good. Make sure you've selected this key, and it's time to paint it somewhere. So how about this place here? If you switch order that game view and have a look already this looks good. Bring the game You back to the original size. You might want to close this window under L B tiles. Let's make some changes to the blood key Prefab. First, I want you to click tag an ad attack and let's call it power up. Underscore Bullet. Save the project. Go back to the L B. Tiles folder, Select the bullet key and select the power up will attack. Save the project next. I wanted to change these sorting Lear to foreground and save the project and click Add component type in circle Collider tooty and said this too is trigger. Now select cat and double click to edit the player controller script in here. Let's write a boolean variable which will be a public bullion I can fire. This will be used to check if the clear can fire or not. And we're going to use this in the fire bullets method. So we're going to copy all of this from here and put it under a statement. If can fire only in that case, allow the player to fire the bullets. Okay, so when you go back to unity, you load is the player controller Script will now have a can fire check box, which you can put a check like So Okay, so I have unchecked can fire and let's go and test it. No notice. I am hitting the space bar, but nothing's happening. Okay, so this means it's working. If I put a check here and I go back to play mode, the player gets the ability to fire bullets. Now we want this. Boolean variable can fire to be set when the player collides with the key, so let's see how to do that. Let's have a look at the tag for the key so you can do it directly from L B Underscore tiles, and I'll select blood key and notice the tag is power up. Underscore Bullet. I want you to write this down on a piece of paper or make a mental note. If it that the tag for the bullet key is power up, underscore bullet. We will need it shortly. And I also want you to see that the key has a Circle Collider tooty, which is a trigger. Okay, so when the player comes in contact with the key, the player will enter the trigger off the Circle Collider toady and on trigger entered hoodie method will be called Okay, so let's use all of this information now in code. Let's go back to the player controller script and click on Trigger. Enter Tuti and let's copy all of this from here, and we're going to write another case. And this time the cases power up Underscore bullet. Okay, save the script. Now, if you have enabled the special effects for now, let's keep this show coin sparkle as it is to do some testing. Okay. And below this, you can say, can fire equals. True. Also, let's declare a vector tree power of position like so, And let's get the position off the blood power of other dot game object Doc transformed Dog physician like so And now we're going to destroy the other dot game object like So now the reason I created a vector tree is because before I destroyed the key, I wanted to get its position because this is the position where you're going to show the particle effect. So let's say power up position, like so save the script, have a look at the can fire here, let's uncheck it and let's go to the play mode. And now the cat can collect all of the coins. That's good, but let's see, the cat cannot fire at this moment, okay? And I jump. And there we have some nice sparkle effect and there the catch starts the fire. It's perfect. So the bullet prefab is working now. Exit the play mode and back in the clear Controller script here, in this case, power. A bullet noticed at the SFS controller is calling the show coins sparkle. We re used the show coin sparkle for testing purpose, so let's create its own method. So first, let's open up SFX this class because we need another public game object field. So copy this field from here, pace it here and simply changed the name to bullet pick up. And that's changed the comment showing when the player pigs, the bullet power up saved the script. And now let's open the SFX control or class. And just like the show kind sparkle. We need another method for D bullet power up. So copy this and pasted here changed to some re shows. The bullet sparkle when the player collects the will, it power up like so and simply changed this a show Bullets sparkle that should do the trick . Okay, Another thing. You want to change this from SFX coin pick up. We have the appropriate variable bullet pick up. Okay. So far, so good. Save the script. And one last thing that you need to do is you go back to Unity, select SFX controller, and you load is under the SFX. We have another public field, okay? And in this public field, regard to specify the bullet power a particle effect, we need to download this particle effect. So go ahead if you haven't already done so there is a link with this video. Download the particle effect and then once you've downloaded it, click the assets and let's have a look in the pre fabs SFX folder, right Click Select Import Package Custom package and select the Bullet Power Up Unity package and click open. And then all of the new items will be imported for you. The star dot grade are PNG is already available. Click import, and that's about it. Now select the SFX power up, and I want you to have a look here. We just need to add the destroy with DeLay script, and this error message goes away. Next, click on render and you'll notice the sorting Lear has unknown earlier. Once you change this a foreground and this should be good to go save the project. Then go back to SFX Controller and in here. I want you to drag and drop this SFX power up like so and back in the player controller script. Now, all that remains is in the power up case here, instead of sure coin sparkle. We're going to change this from show kind, sparkle to show bullet sparkle. And we're going to pass in the power of position, like so save the script and it's time to test. Okay, select the cat game object. Make sure can fire is unchecked and then go to the play mode. And now watch. The cat can collect the coins, but it cannot fire. So the rules are simple. The cat needs to jump get to the key like so And this time the particle effect will be different. There. That's much better, right? So I'm sure you must have played games where the player jumps and when the player lands, you see a nice dust particle effect, right? So if you want to know how to do that, join me in the next video and we will see how to get this done. All right, See you then. 8. Dust Particle Effect When Player Lands: using particle effects adds to the visual appeal of your game. And by the end of this video, you will learn how to make a nice dust particle effect when the player lands. All right, like so this looks cool, right? So let's get you started in the SFX script. Let's write a public variable called SFX Player Lands so you can copy this entire line from here and paste it here. And let's simply change the name from here Player lands and we could update the comment. Showman The player lands after jumping. Now, when you save the script and when you go back to Unity and highlight the SFX controller, you Lotus, you have another public field called SFX Player Lands. Now in here, we're going to add the particle effect, so we're going to download it first. There's a link in the description. If you haven't already done so, download this particle effect and then once you've downloaded it, let's look in the Assets folder under pre fabs under SFX in year, Right click and Select Import package custom Package the Clear Lance Unity Package and click Open and click Import. If you notice you haven't error That's because we already have a destroyer delay. So don't worry. Go to these scripts selectee helpers folder and delete it, and thats takes care of the error. But before you can carry on Goto SFX, select the player lands and have a look. There's a missing script, so let's say destroy with delay and the first error is taken care off. The other thing is the sorting Lear has an unknown layer, so I want you to fix it by selecting foreground. Now save the project and now select US effects controller and drag and drop. The SFX player lands right here. Next let's open SFX controller like so and now let's copy one of the methods and pace here and simply changed the method named ah, show player landing. Let's change the variable Aim to SFX Stock Player lands. Save the project and you also wanna change these summary shows the clear landing dust particle effect. Let's begin by expanding the cat game object and selecting feet. Now let's zoom into the cat game object. Let me drag it appear and zoom into the feet of the game object cat and you may ask me, Why am I showing you defeat because when the player lands or the feet of the cat touched the ground, we want to show the dust particle effect. So for this, we're going to do something to defeat. Now click at Component Type in Edge Collider Torrey, and it returned. And now you notice on top of the white line, you have a green line. What's going on? Well, this is the edge clatter, tooty, click Edit collider. And now notice on the green Line. You have this handled. Okay. When you click and hold this handle, you can drag it around. Okay, Like so. But I want you to keep the green line on top of the white line and position it to about this position here. Okay, This looks about right. Do the same to the left side and position it about this position right now. To turn off the handle, simply click. The Edit Collider. Andy Handle is gone. This is the first step. This edge clatter will help you in detecting when the feet of the cat come in contact with the ground after the cat has jumped or returns to the ground. So we need a script. Click add component, type and feed Ctrl and it returned two times and then double click. Now delete these two methods. They're not required. And let's begin by writing these summary helps in showing the dust particle effect when the player lands. Okay, so we're going to use void on trigger. Enter two D and this is taking a clatter to D, and the parameter will be called other. And we're checking if other dog game object docked compare tag turns out to be ground. Well, in this case, what do you want to do? We want to call the SFX controller and say, Hey, we want to use your sure player landing method and this method takes in a vector three position, So we need a position. So let's enter a public trance form and, let's say dust particle position like so coffee. This from here and pacer here dot position put a check for his trigger. All right, now, select feet, right click and create empty. And I want you to rename this toe dust particles position on there. But where is it? So like the move tool and they're here it is. I want you to drag it to the right. And this is the position where the dust particles will be shown. So I want you to move it up, like so. And this position looks about right. Okay. The player lands, and then the dust particles are shown here. Okay, Now what remains is we have a dust particles position, an empty field. So you want to drag the dust particles like so onto this field? All right? No, back in the script. Everything looks good to go, so it's time to test. Now, let's go to the play mode. Okay? Did you see that? Whoa. The player lands and you see a nice dust particle effect where the player landed there. Okay, so now you know how to make nice particle effects, right? Here's a mini challenge for you. The first many challenges. Try experimenting with the positions where the dust particles are being shown. Right now, it's somewhere around the feet. Okay. And the second many challenges. Right now, the particle effect or the landing effect is being shown every time the player jumps and lands, right? So, using the techniques that I've shown you in the previous videos, I want you to make some changes so that you can turn the landing effect on and off from the Inspector. All right, so I hope you'll have fun doing these challenges. And now it's time to introduce you to another cool, special effect. All right. Giving the cat the power to break stuff in the level. Now it's quite an interesting effect. And I longed review more join me in the next video and don't miss out. All right, I'll see you there. 9. Breakable Crates Setup: the breakable creates effect will be explained to you in two videos. This first video will show you the basic set up required and the next video. We'll show you the scripting. So by the end of these two videos, you would have accomplished something like this. All right, it looks cool, right? This is the breaking creates effect. So let's get started. Let's get started by looking in the Assets folder. Double Click Editor Default Resource is L B tiles. Next, I want you to know increased the preview size so you can see the tiles better now select, create and duplicated. And then I want you to rename this toe, create breakable and now drag it onto the scene view like so you'll notice a game object with the same name has been created. OK, now let's get the cat to another position. Select and drag the cat, maybe to the ground. Now I want you to select or double click that create breakable to zoom onto it Now, right, click and select. Create anti and reading this guy the bottom. And then here's the bottom. I want you to drag in. Place it somewhere Here. Next click odd component type in edge collider tooty. And here we have unethical attitude E. But it's kind of not visible because of the green background. So Goto main Camera, Select Beauty Jungle Turn it off while you're designing this stuff, and now you can see we have a nice edge. Clatter tooty Now click Edit Collider and you'll see this handle. And if you move the mouse on the green line, the handle it starts to follow the mouse. I want you to hold left, click on the mouse and dragged to the left like so. Okay, essentially, we're resizing the edge clatter tooty. So place it some way here so that it's in line with this screw here and list of the same for this. Okay, this kind of looks good. Click the edit clatter to turn off the handle, and now let's look in the tag. Click this dropped out. Select a tag and we're going to add a tag called breakable. Save the project. Select bottom click D. Drop down and select breakable for the tag. Saved the project and now select, create breakable and then I want you to click, apply and also let me tell you the reason If you go back to L B tiles, you notice this create breakable. This is a prefab. Okay? But when you dragged and dropped it onto the scene view, we created an instance off this prefab and we started making the changes to the instance. The changes have not been reflected back to the pre fab yet. Okay, the changes are pending. How do you sink the changes or make the changes happen to the prefab here? Very simple. You select the instance and you notice these three buttons you click apply, and the changes are made here. Case will click apply. And there we have the genius sink to the create and notice. We have this arrow and you load is all the changes you did created the bottom came object have been reflected back now, double click do highlight the cat game object and click this arrow. And now, right click and select, create empty and named this guy head. Okay. And let's drag the head up here and you may wanna drag defeat right here, So Okay, It looks like the head is right at the top. The feet of the bottom now select the move tool and move the head up to this position between the ears of the cat, like so and now, like at Confirmed in Taipei Inbox Collider tooty and said this guy to a trigger And let's zoom in a bit more and click Edit Collider and I want you toe resize the collider so that it starts to look something like so already there Click edit clatter and now cleaned the tag Drop down attack And let's add in a new tie called player head and save the project. Go back to head. So leave the drop down and select Clear head saved the project and you can zoom out now and let me show you what we have got till No. Select a cat. Bring the cat up to this point and double click to zoom in there. You have a crate. Okay, which has a bottom edge. You have a cat which has a head. Okay, now, in the next video, I'll show you. When the cat jumps, the head of the cat hits the bottom edge off the crate. The creates going to explode. Okay, it's gonna break. So we're going to do all of that in the next video. So see you there 10. Breakable Crates Scripting: in the last video, you did some basic set up to make the creates breakable when the cat hits them with its head. By the end of this video, you would have completed the scripting to make the effect possible. Like so. All right, so let's get started. Let's begin by selecting the head of the cat and click our compound type in head controller . He returned two times and you have the head controller script attached. Now double click the head controller script to edit it. Let's begin by writing the summary chicks for collisions with a breakable game object and then informs the SFX controller to do the needful. Next. I want you to lead the start and update methods, and instead we're going to write an on trigger enter avoid on Trigger Enter Tooty Collider to re other, and in here we're going to write an if condition. Other game object compared tag and we're going to enter the tag in just a few moments. It's going to be breakable. Then let's write a comment, informed the SFX controller to do the needful, so we need a method in SFX controller toe handle the breaking crates so Let's go back. Teoh, SFX Controller, Copy the method here and paste it here. And let's update the summary shows the box braking effect. Let's name the method as handle box breaking and lead This have a vector three position like so And in here we're going to understand she ate something. OK, so for now, let's delete this line, and I'm gonna show you what we're going to do for now. Let's get back to the head controller and type in SFX controller dot instance dot handle box breaking and like so and where's this handled box? Breaking its in SFX controller right here. Okay. Now, when this method is called, I want to do some testing. So for testing or debugging, we have a debug class which has a method call log and log. Hell, she log messages to the Unity Consul. Our prince Messages to the unity Consul here. Let's passing a message. Cat's head Kiddy crate. Perfect. Now I want to show you what's going on behind the scenes. Let me highlight the cat game object. Here's the cat. When the cat jumps, you noticed the cat has this head right? And when you have a look at the head. The head has the head controller script and what's in the head controller script? You have this bond trigger Enter tooty method This if condition is checking if the head of the cat right, This head of the cat which has a box collider tuti and a trigger if this head of the cat comes in contact with a game object which has a tag breakable. In that case, as affects, Controller will call the handle box breaking method in the SFX controller, the handle box breaking will print a message in the unity Consul. Now, at this point, you may ask me in the head controller script this if condition is checking If the head of the cat is getting in touch with a game object with the tag breakable Only then this method is being called and the message will be printed. But where is the game? Object with the tag breakable. And that's a good question. Let's have a look in unity. You know what is this crate breakable? It has this bottom edge. Okay, notice that this has the tag breakable. So the head of the cat as the catch starts to move up. We'll get in touch with the bottom edge. Okay? And at that point of time, the message will be printed in the unity. Consul, let me show you this inaction. Here's the cat. Let me start to move the cat and watch what happens in the council. Here's the cat moving up and watch what happens there. Do you notice at this point of time, the head of the cat is in contact with the bottom edge off the box or the Crete. And we just had a message in the unity console. The cat's head hid the crate. All right, Cat falls down. Let's try this one more time and we have another message. So let's exit the play mode. And now, instead of simply printing a message, we're going to get the position off this crate and then make this crate invisible, or destroy it and then show four fragments off this crate, which are going to travel in all directions. Okay, so it looks like the crate just exploded. So how are you going to do that for this? We're going to go back to the SFX controller and we're going to pass in a Vector three parameter college P O s or position and delete this line from here and save the script. Okay, So handled box breaking is going to take in a vector three position. Which Vector three position. Are you referring toe? If you notice this, create has a position. Were interested in getting a reference to this position so we can kind of show some particle effects here. 90 had controller. If you open this, Parenti says it's asking you for a vector to position. So how do you get the position for the box? Well, it's a bit tricky, but let me show you. Ok, here's d create the crate has a child object called bottom. The bottom has the edge clatter, toady. So we are going to start from the edge Glider tooty Why? Because on trigger enter tooty returns a clatter tooty variable and we have called it other . This other is nothing but the edge collider To re now have a look using the doc notation one more time. Let's say other dot game object. Okay, so this will point to the game object here now, once you have the reference to the game object, we're going to say dot transform. Like So? So we're going to get to this part here. Transform now from transform, Weaken, jump one, step up to the crate, breakable. The parent game object we're going to say transformed. Odd parent. Okay, this is the way to access the parent game. Object. So now with this, you have access to the create breakable. We're going to say parent dot transform. Okay, because now in the crate breakable were interested in getting the transform. The transform will get you the position. So up till now, you have the transform. Now simply say position, and that's about it. This one chain off connections gets you the position off the crate. Okay? This is a very useful way off figuring out positions in child and parent game objects. So you may wanna practice the line of thinking that I've shown you. Okay. If this is the first time you're trying it, practice is the key. The more you practice, the easier it's going to get for you already. So you know that from here to here, you have the position for to create. Okay, so we can use this information. Let's copy this from here to here and follow along. We're going to destroy the create now. So let's say destroy and paste other dot game objects are transformed. Appeared from Parent. I want you to call game object because the destroy takes in a game object. So starting with the bottom edge, this is how you kept going. Up, up, up and reach the game. Object create. All right, see if descript. Now, let's head over the unity. No watch. Here's the cat and boom! The cats had hit d create and the create vanished Perfect exit the plane mode. So now you've seen that this piece of code destroys the create When the cat hits d create. Now in the SFS controller handle box breaking should show you the special effect. So let's go to the SFX controller and let me scroll up a bit. So you have some space and I'm going to explain something. Now we have a Vector three position, Okay? As a parameter in this method, What position is this? This is the position off the create you've seen the trade is vanishing or is being destroyed, right? So how can you use this position to show the particles are broken fragments. Let me paste in some comments, and we're going to create four fragments and we're going to position the fragments here. I'll show you the code next. And then Once you have these four fragments on the screen, these four fragments have their jump scripts attached to them. So the moment you show them, they're going to jump apart. And that's gonna give you the impression that D create just exploded. All right, so let's see how to do that. We're going to show these fragments using in san she ate. So for an example, let me copy this line from here because it's God being Stan. She ate call. And the reason we're getting these four positions is because let me paste in three. Maurin, Stan. She ate statements. These instead. She had statements taken a position. So we need the positions first, and then we're going to pass in references to the floor fragments. Okay, So you may ask me, where are the fragments? All right, don't worry. First, let's figure out the positions and then we will go ahead and create the fragments. Okay, So the positions, the position will be a vector three. Call the 1st 1 Pos one. Assign it P O s. This pos is the parameter you got from here. Another factor Tree. Okay, I want you to copy this from here. Peace did three times. And then I want you to change the numbers. Like so. So we have four positions and they have been assigned to the position of the crate. Okay, so all of these fragments are at the same position. Now we're going to shift D positions off each fragment left and right, and then we'll see. So the first fragment will be positioned in the X axis, and it's gonna be shifted by 0.3 units to the left, Copied from here. Patient here for this fragment. We're going to shift it to the right, copy it from here, and also wanted to change this position two. And then let's paste it one more time and say position, Tree, Let's say negative. And we're also going to shift it in the why access and copy it from here one last time. And instead, position three, it's gonna be a physician for and this will be a plus, like so. And that changed these to position one position to position Tree. Copy this one more time. We need four fragments and position for writing. So this is the first part. The last thing that remains is creating these fragments. So let's see how to do that in the assets. Fuller. Let's go to these sprites folder like so and select items. And we're going to drag this create Sprite here, like so. Okay, now reading this to box Fragment one. Select Lear earlier and typing fragment. See the project. Go back to box driving one added to the fragments layer like So Now I want you to click edit project settings and select physics duty, and I'll show you why I created another fragments layer. This Lear collision matrix allows you to specify which Lear can collide with another leer. So we don't want the players colliding with the fragments. Okay, so I want you to uncheck this here. Okay? So players and fragments are not going to collide with one another. Save the project. Select the fragment one click Add component type Inbox, Collider tooty. We also want a rigid body tooty because the fragments will fly so rigid body tooty like so and now we make these fragments a bit smaller. So 0.45 in the X 0.45 in the UAE. This looks about right. Next, I want you to click add component. We're going to add a fragment. Ctrl. It returned two times, and this creates the fragment controller script. DoubleClick, The fragment controller script to edit it. Let's get started by pasting the summary manages the behavior of the box fragments. Now we need a public Sector three jump force next public float Destroy DeLay, and we need a private, rigid body to T R B. Now in the start method, let's say R B equals get component rigid body to D, and this gets you the reference to the rigid body toady. Now let's a rigid body dot at force passing jump force, and that's about it. We don't need the update for the script. Now let's get back to unity. You'll notice the fragment controller has thes public fuels now because this is box fragment. When I wanted to travel in the left direction, so I'll say negative 203 100 for Dwight and I want this fragment again destroyed after two seconds. Save this. Let's make a prefab out. If it's so double could be prefab for And you can drag this inside the SFX voter like so And now Duplicate box Fragment one and it's box fragment to and change this to 200. Save the project and you can delete box wagon one from here. Now we have the fragments. And now in the SFX Nazi s make sure this is declasse, the simple class that you created I want you to create to public game object views called SFX. Underscore Fragment one and SFX underscore fragment to you Have the Commons, like so save the script. And now, finally, in this affects controller for these variables were going Teoh specify as effects dot SFX fragment one. And for the remaining three, how we look, this should be fragment to fragment to and fragment one. Save the script and before we test one last piece of code, change this to destroy. Okay, this should be destroy delay and copied from here. And then we're going to say destroy the game object after destroyed away, right? Save the script now because you made a correction here. Destroyed delay, Go back to unity and select bullet off the fragment like so and now destroyed. DeLay is Earl, so you want to change it to to save the project, and you also want to change the Y value to 200. And there's a material field here. So we're going to create a physics 20 material Click Materials folder, right click and select. Create physics duty material and let's call this box fragment and in here want you to change bounciness 20 points of lead. The friction B zero point for save the project. Then go back to the Pacific's and select Box Ragland one and for material type in fragment and select this guy. All right, I want you to do the same for fragment to and there this is going to give some bounciness to the fragments as they are destroyed. And now let's go to play mode and time to test here. Okay, so that's a very nice effect, right? You notice the particles just exploded and we have the coin sparkling effect. Let's exit d play mode. Now you can select this, create and apply changes back to the pre fab to make sure everything's applied correctly. And then you can delete this from here. Click level map and activated and notice You have two crates. Okay. One of them say's creates breakable, so I can show you something. Let me delete these two from here, and I'm going Teoh, paint these two breakable crates and now notice the's two crates Don't have box clatters because we're going to use collider mode and select these two and add the boss letters here . The cat can break these creates but notice As the cat hits the creates, the cat keeps moving up. Right? So we can do a small thing here in the head controller script before you destroy the crate . Want you to said the velocity of the cat deserve So here's the line of code that will help you do that. All right Now I showed you how you can use a particular game. Object to move to the parent game object and then from there, tried to access the transform and then get to the component. Right? So if you work through this chain off sequins, you'll be able to figure out how I got to the rigid body to tee off the cat and rigid body tooty has the velocity property, and I'm setting it to victor to 0.0. All right, this is zero velocity, and that's it. That's going to do the trick for you. And now noticed that as the cat hits the create, the cat bounces back. All right, because this is realistic, right? The cats had hits t create to create breaks, and the cat bounces back. All right. And now it's time for a mini challenge. Like I painted these two breakable creates here, try to paint, wriggle creates wherever you wish, and then experiment with the effect that you get. All right, this is your many challenge. So now we're moving ahead with good speed. And now is the time to introduce you to one of the most important elements off your mobile platformer. And what that is will be revealed to you in the next video. So see you there 11. Create the Game Controller: although this video will be very short, but you'll get introduced to a very important part of gameplay. Mechanics called the game controller. Think off the game controller as the main manager, which controls important parts of your game. Like keeping the score, showing the game menus as required. Saving and loading the game data updating the heads up display, managing player lives and so on and so forth. So here's how to get started. Expand the arrow next to controllers, then right click controllers and select, Create, Empty and rename This game object to gain Ctrl now click Add component type in game. Ctrl hit returned two times, and this adds the game controller script to the game controller. Now double quick the game controller script to edit it. Let's get started by writing the summary. Here's the summary manages the important game play features like keeping the score, restarting levels, saving and loading data, updating the Judy or heads up display that sector. Next, we want other scripts to be able to access public methods in the game controller without making an object off game controller. In my previous videos, I've shown you this technique, so let's write a public static game controller and named the variable instance. And let's write void a week and in here we're going to check if instance equals. No. In this case, we're going to assign instance to this this game controller instance. All right, this is the basic set up. Now in game controller. You're going to create public methods, which you can call from other scripts like Player Controller. It's extra now. In the next video, I'll show you an example off such a public method, which a garbage collector will call when the player falls and dies. What's a garbage collector who? Well, join me in the next video to know the secret. I'll see you there. 12. Create the garbage collector: up till now, when the player or any other game object falls below the camera, it's not destroyed and, well, technically, keep falling forever. How can you fix this? Well, let me first show you what I mean in the play mode. Watch what happens to the player and they're The player is falling and it's falling and it's falling. And there there's no end to it, right, so the player is still active. We need to do something about it in this video. I'll show you a useful technique to solve this problem. So let's get started first. Let's see, in this mobile platformer, the player is walking on the ground and the camera will always follow the player. The player will always be inside the camera, right? So if the player falls down below the camera, what if we have a box kind of a mechanism like so? And when the player and church this box, let me show you. If the player enters the spots like so the level will get restarted, or if any other game object falls, it's gonna be destroyed, right? That looks like a good solution, so let's see how to implement it select mean camera, right click and select. Create empty and reading this to garbage Ctrl. Next click. Add Component, Taipei Inbox, Glatter, Tory like so and then select garbage controller and then drag it to the bottom. Like so. Next, I want you to resize the X or increase the value of the X and notice you're getting a nice box kind of mechanism or platform. I want you to said. This, too, is trader right, so game objects falling on this box will be able to pass through it and on trigger enter to tea will be called. And where is it going to be cold in the garbage controller script? But I don't see the script. All right, let's add it at Component Taipei in garbage, Ctrl hit returned two times, and there the garbage controller script has been added DoubleClick the script to edit it. Let's begin by writing these summary, and it's simple destroys any game object that comes in contact with it. For this script, we don't need the start an update, and we simply need devoid on trigger Enter tooty, and this takes in a collider. Tooty. Let's call the parameter other and let's write, destroy other DOT came object, and that's it. This is the only piece of code that's required for the garbage controller to start functioning. All right, so let's have a look for this demonstration. Noticed that the garbage controller is a child off the main camera and the camera is going to follow the cat. Right? So when I go to the play mode, this garbage collector will be under the cat. And now, because of the scripting you did, the cat will get destroyed when it touches the garbage collector. So let's have a look there. You notice the cat is not there in the hierarchy, all right? And you're getting the air messages because thedc amorous trying to follow the cat. But the cat is not there anymore. All right, so don't worry about it. Let's exit the plane mode and you've just seen that the script is working, Okay, now that it's destroying the game object or any game object which comes in contact with it , how about making the player land on it and the level should restart? That would be cool, right? So, in the next video, join me and I'll show you how to do exactly this 13. Reload Level When Player Dies: By the end of this video, you would have learned how to make the level reload. When the player falls off the camera bounds legs so and they're the level reloaded and notice. There's a time delay when the level reloads. So let's get started. Select the main camera, expand it to highlight de garbage controller. Click the garbage controller and you have the garbage controller script. Double click this script to edit it. Let's get started by updating the summary and now in the on trigger Enter tooty, we're going to write an if block other game object dot compared tag is equal to the player . In this case, we're not going to destroy the player, and we will see what we do in the other case. If it's not the clear we're going to destroy it now for the player we're going to call a method in game controller, right? So save the script command dot and type in game controller, and this is the shortcut that I told you about in here. We're going to write a public void, clear died method, so this method will be called when the player dies. All right, so let's go back to the garbage controller and in here, let's say game controller dot instance stock player died and noticed. This method does not have a summary, so you don't know what it does, right? So let's get back to the game controller and add a summary. Saved the script back in garbage controller. Now, when you called him at that, you have a summary, and it tells you that it restarts the level when the player dies. So I encourage you to start using this practice if you've not been doing it and your cord will become professional all right, up till this point player died. Method is not doing anything, so go to game controller and where he won this method to do well to summary tells you, restarts the level when the player dies. So how can you restart the level? First? You need to add a name space using unity. Engine dot Seen management seen management name space gives you a scene manager class and you can called load seen method and load scene takes in the name of the scene. So here's a quick question. When you go back to unity, tell me the name of the scene that's open right now. You can use my scene. Pro Platt Pneumonic Perfect. It's game played Opportunity Seen Pro means project. Platt means platform seen pro Platt. So we're going to type in gameplay and save the script. Now. What's gonna happen is notice that the garbage controller is a trigger. When the player enters the trigger off the garbage controller void on trigger, enter to tee off garbage controller will be called this method. This method will store the reference off the box, glad to re off the player in the variable or parameter called other. And this if block will check if the game object is a player. In that case, game controller will be informed and player died. Method will be called other game. Objects will be simply destroyed in game controller were using the unity engine, not seen management name space. And this name space gives you access to the sea and manager class, and we're using the load seen method off this class and passing in the name off the scene load. All right, you can see game played opportunities the name of the scene. So it looks like everything is set to go when you go to the play mode. When the player lands on the garbage controller, the scene should reload. You also want to make sure if you click file bill settings, gameplay is listed here. All right, if it's not listed, you can go to scenes and you can drag and drop game played here, right? Canceled us and close this window. Now it's time to test. Okay, Looks like the scene is reloading, right, because you see the player bouncing up and down. But wait a minute. If you go to the console, we have a bunch of errors in here. So what's going on? Is there something we missed? Don't worry, Let me explain. First, let's exit the play mode. They're a couple of scripts which depend on the player like the main camera has the camera controller. The camera controller is looking for a reference to the players. Transform right. Also, if you look, the cat itself has the head controller and the food controller. This is defeat controller. This is the head controller with so many script. When the cat hits D clattered thinks mess up a bit and you start to see the console with the error messages. The simplest fixes in clear diet. Weaken. Deactivate the player has to player hits the collider off the garbage controller. So for that we need a reference to the game object player like so okay and in the garbage controller in here, we're going to pass in the reference of the game Object player, other dog game object and back in game with Children. Clear dot set active. False. All right, this is the way to deactivate game objects in the scene. Save the script. And now, when you test noticed that there are no error messages, it's been taken. Care off. Let's go to the play mode and boom. You notice the player is jumping up and down like a yo yo, but the console does not have the previous error messages. So far, so good. But you haven't asked me. Hey, this is looking kind of funding the players jumping up like a yo yo. Can we fix this? What if we could introduce a delay before the level restarts? How about a second? All right, Yes, that's possible. Let's go ahead in the game, Controller. We're going to declare a public float and call it Restart delay. Right. We're going to copy this from here. And now we're going to use it how we're going toe use a method called invoke and invoke. Takes in a string method name. So, for now, let's say a restart level, okay? And this takes in a flow time, so restart delay for the time. And this guy's and red means it's not been created yet. So let's say avoid restart delay and in restart delay. Oh, thes brackets are all right. They should be the round. Okay, so restart level is going to restart the things. So we're gonna copy this from here and pasting here. This is the only piece of code that you need to introduce a time delay. And now you go back to unity and you select the game controller, you load issue. Have the restart delay public field, simply enter one. Saved the project and time to test. Now, do you notice we have about a second of delay before the level restarts. Okay, you can see the player takes time before jumping back again. All right? That's essentially the level. Reload. Okay, so now we've taken care of the player falling off the camera bounds and the level restarting. But what should happen when the player falls into the water? You notice we have some water. Body? How about showing a nice splash and then restarting the level with some delay? This is exactly what we will do in the next video. So see you there. 14. Splash Effect When Player Fall in Water: when the player falls into the water, there should be a nice splash and the level should restart after some delay. By the end of this video, you would have learned how to do exactly that. Like this. There you saw this nice splash effect and the level reloads. All right, so let's get started in the player controller script. I want you to look at the on trigger enter Trudy Method. We have these cases, right? So want you to add another case. You can add it anywhere if you like, and the one with the lure and let's say water and ended with this Coolum and right break. Like so. And now we're going to enter two comments, show the splash effect and inform. Let's get the tool tip out. Informed the game controller. Right. Save it. Now we're going to show the splash effect. So if you have a class for that, right, So let's open the SFX class and in here, I want you to declare a variable called a suffix splash off type game object, Right. This is the particle effect that you're going to see. See the script when you go back to unity and you highlight the SFX controller. You'll notice you have the SFX splash public field. But there's nothing in here. So there's a link with this video. Go ahead and download de particle effect. And once you have downloaded it looking the pre fabs SFX folder, right click, important package custom package. And then let's download de Splash Unity package already click Import And there we have it. Now all that remains is I want you to drag this from here and drop it here already. The other thing I want you to do is click SFX Splash the particle effect and looking the Inspector, We have a script that's missing, so I want you to click this anti being destroy with delay And they're this error is taking care off. Second, I want you to look at these sorting Lear right now it says unknown there. Once you click this and select foreground and ah wanna make sure the order in Lear is our Now you can see if the project and now you see these water tiles. We also need to add the tag water to these tiles. So these guys are indie editor defile resource is l B tiles and you can increase. Do you preview size? Select the number 17 And first I want you to add a box glider 20. Okay. And I want you to drag it here like so you can have a better view and click Edit Collider. And I want you to drag this up to this point. Kind of like so resume into it. Click edit, clatter. Then I want you to a select tag attack. And that's at a tag. Water and save. Go back 12 b tiles. Select 17 in a sign the water tack to this tile. Okay, so make sure you have a tie Water. You have a boss glider. Tuti and I also want you to set it a trigger and then click apply so that the changes are applied back to the pre fab. Now you can delete the tile from here and notice all the water tiles have these box colliders, which are also triggers. Okay. When the player falls in one of these tiles, the entre entered hoodie will be called now in the SFX Controller script. Let's have a look somewhere below the show player landing. This seems right. I want you to copy this entire method and paste it here. And I wanted to change the name of this to show Splash. All right, it's going to take in a victor three position and in san she it is going to in Stan. She ate SFX Splash, save the script and let's change the summary shows the splash perfect when clear lands in water or falls in water. All right, this looks good. Now, in player controller, we're going to call in SFX controller dot instant start show Splash, and we're going to pass in other dot Game object transformed our position, and now we're going to inform the game shoulder to do the needful. So we need to create a message in game controller. Let's open game controller and in here, let's copy this entire message. And instead of player diet, we're going to say clear ground. And yes, we're going to pass in a parameter of type game object. And instead of saying player set active fools, we're simply going to invoke the restart level with a restart DeLay and, uh, let's update the summary and say the player falls in water, save the script and well, looks like we're all set to go. So it's time to test. Now here's the player and the player. Jones and there. Did you notice we have a nice water splash effect? They're so this looks kind of cool. And there you have it. So now you're making some really cool effects, right? Let's continue this trend and make another cool game feature called dropping platforms. All right, we're going to place a few platforms year, and, ah, when the player lands on any of the platform, the platform starts to fall down. If the player does not jump off in time, the player is gonna die. All right, So join me in the next video and let's get this new game feature implemented. 15. Dropping Platforms: In this video, you learn how to make another cool game feature called Dropping Platforms. When the clear lands on a platform, the platform starts to fall down like so. And if the clear does not jump off the platform in time, the level reloads and the player dies, Right? So let's see how to get this implemented and let's dive right in. In this video, I'll show you how to design the platform first using level builder. And then I'll test the design to see if everything's fine. The player can walk on it. Then I'll show you the logic behind dropping the platform, the manual process. And then we're going to use this logic through scripting all right to make the platform drop. So let's get started by designing the platform Select level map activated and then in Pain Boat. We're going to use these three brown tiles. You can increase the preview size to have a better view, and now also, let's zoom in a bit, so when you're designing, you can have a better view in the scene window. All right, this looks about right back to the paint mood. Select this first tile and now Let's painted hereby left looking. Okay, This looks good. Select the middle tile and painted here in the last right tile. Okay, this looks about right. This is how the platform is gonna look like. Now, if you've noticed in paint more, you had a parent name. Okay, so by default, all of these three tiles have been painted under the parent named Ground. Let me show you where the ground is. Here it is. Okay. All these three tiles are in the ground. However, this is not ground, right. This is above the ground. We have another period name, high ground. So go to selection mode. So, like these three tiles, select parent name high ground and simply click on change, parent. Okay, notice you have another parent called High Ground in these three tiles have been changed or have been shifted to the high ground. Good. Now we're going to group these items into one single unit, so make sure that these were selected click group selected items. And there we have an item group. Let's reading this guy to dropping black form, and there, So far, so good. Now we're going to give it some properties, so first, let's go to the layer. Select ground and yes, click. Yes, changed. Then click Add component type and walks. Glad or to re why we need it. We need to have collision detection, right? The cattle jump in without a clatter. The cat cannot stay on the platform. Next. This clatters is small so click edit clatter And while dragging you can hold down command on your keyboard and resize it like so and Ah, now for the platform to fall down, we need to add a rigid body to Lee. OK, this brings the platform under de forces of physics and gravity. But when you start the game, you don't want the proper form to fall down immediately. So you want to put a check for is schematic. This schematic will hold the platform as it is. Okay, when you un check it, it's gonna fall down. So here's a clue for you later on in this video, you will see how you can program the logic for dropping platform. For now, let's move on. You also want to check freeze rotation. Why the case when the player jumps on the platform and starts to move around the platform will start to rotate if this is not checked. So you don't want this to rotate. Right? So we need a check here, and you want to make sure that the tag is ground. If not, click tag and select ground fault Attack. Right. So this completes tea set up off your dropping platform. Now it's time to test if the player cat can move around and jump and everything is set up correctly. So let's have a look. We're going to test three things. First, if the cat can jump on the platform and walk around showing all the proper animations. Second, if the platform stays where it is when the cat jumps on it, Okay, we're gonna make a drop later, and then we want to check if the platform is not rotating. Okay, The platform should not rotate, right? So let's go to the play mode and watch what happens here. The cat is going to jump there. The cat lands. We have a nice landing animation. The cat is able to walk on the platform and it looks like the animations are in place. The platform is not falling, and it's not rotating as t cat moves perfect. So it looks like the test has been successful. Now let me show you the logic behind dropping this platform. Right. So here's the logic. Select cat and noticed the cat has the boxer lighter tooty. And when you select d dropping platform, it also has Boss Laboratory and the rigid body to re also the rigid body to d with the dropping far form has theist cinematic property, which has a check on it. So here's the secret. If I go to play mode and I uncheck, this guy is schematic, Watch what happens there. You notice that is the secret ingredient. So we're going to write a script, Attach it to the dropping platform. These script will keep checking if a collision has happened between the cat and the platform, and the moment the collision is detected is key. Thematic will be unchecked and the platform will begin to fall. Right. So let's dive into some code and see how to get this implemented. Click bad component typing, dropping platform. It returned two times and then double click to edit descript. Let's begin by writing this summary controls the platforms dropping behavior. Now where is schematic in rigid body to re component rate. So if we can get a reference to the rigid body to tea, then we can check or uncheck it from this work. So let's do that. Let's get a reference. Declare a private variable left high Bridget body to t like so and call it RV. Now in the start method, let's write in R B equals get component rigid body to be And there you have it, a reference to the region body to be conformed. Next, I want you to delete the update method and instead write void upon collision. Enter tooty and because it's a collision, enter tooty Gonna take a collision to re remembered, the pneumonic collision will take a collision duty trigger will take a collider Trigger Collider collision Collision. Right. Let's name this parameter other. And in here we're going to say, if other dog game object Ah, compared tag. This is equal to the player like so in this case, you want to go ahead and unchecked Theis schematic. So RV dot is schematic equals false. This will uncheck the schematic component. Now let's test and now watch When the player jumps on the platform, it should fall down. Okay, It looks like it's working right, but you may say, Hey, it's dropping immediately. Can we add some sort of delay before it's dropped? Yeah, that's possible. And let's do that also before we go ahead and add the delay. Wanted to show you some improvement. I just figured out what's happening is it's the cat or any part of the cat. All right, Maybe if it's this right part of the cat will collide with the right side of the platform. The platform was still fall down. We don't want that right. We won that. The cat's feet collide with the platform like so. Only then should the platform fall down. It's a very minor change. I want you to go to the player controller script and look in the on collision. Enter tooty method. All right, this piece of code, this one line, I want you to take it from here. I want you to open the feed controller script and in the entre enter to we want you to pay secure. You'll notice that this guy is in red because is jumping belongs to player controller here . So let's change its position from here and put it in a public block. Let's call it public. Boom is jumping. Then what? You delete this piece of quote? OK, it's not needed because we're shifting it to the feed controller script. And ah, we need a reference to the player controller script so that we can use the is jumping. So let's call the variable player controller. And to get a reference, we're going to put the code and start and we're going to say it clear told her equals. Now watch what I'm doing and then I'll explain Game object. Ah transformed parent dog game object. I get component Clear controller like so. And this sequence is getting you the reference to the flare controller script attached with the cat game object and storing it and clicked order. Now I want you toe Copy this from here, pacing here and put a dot and that's it. Let me also explain how I got to this. Ok, this game object is referring to the game object to which the feed controller script is attached to. Okay, where is this for you Controller attached to it's attached to the feet. Game object here. Okay, Here's the food controller script. So this game object is referring toa This feat. This transform is referring to the transform off the feet. Game optic this guy. All right, The transform has the ability to get access to its parent. Here apparent is this cat game object? That's what we're doing here were saying transform dot paren dot game objects. So this part of the sequence is getting you the reference to the cat game object, and this cat game object has access to the player controller script. Okay, this is a component. So we're calling get component and passing in the type of component there. Controller. So that's how I got a reference to the player controller script. Okay, save the script. And now let's go to dropping platform. And in here, we're going to make a small change here because Thekla's in will happen with the feet and let me show you the feet has the edge clatter, which is a trigger so on collision enter will change to on trigger enter tooty right, and the trigger takes in a collider. Tooty remember the pneumonic Trigger Collider collision collision. So this will be changed like so and we're going to change the tag. Teoh. Their feet and clear feed does not exist. So go back to unity. Select feet. Click this drop down tag attack and we're going to create a tag called Claire Feet and then a sign. Bare feet tag to the game. Object feet, right? Let's place the cat here and test Larry. The cat jumps and it hits the side of the platform, but the platform does not fall. That's good. Now the cat jumps right in the middle and there we go. So let's go ahead and add some delay now, right? So for that, let's go to dropping platform. And in here, let's make a public float dropping delay and we're going to write a method called start dropping and in this method would have bring this piece of coat here. And you may wonder, what are we going to do here? We're going to write in Wolf and this method start dropping with a dropping delay, right? That's about it. So the moment the trigger or defeat of the cat touches the my form this in world will call start dropping after this specified delay and ah, start dropping has quote for turning off the cinematic, and that's about it. So let's go out and test it. Select dropping platform and you'll notice you have the dropping delay. Public Field said this toe one Say the project and let's go to the play mode. And now notice that you should have about a second of delay before the platform falls in there. Yes, it looks like the delay isn't place. You can give it one more try, and that's perfect. So there you have it. The pop form is falling with some delay. Now the last thing that remains is you can drag this platform instance inside the pre fabs folder, and this creates you are dropping platform prefab. So if you want to use it, you can simply drag it from the free fat folder onto the scene view right, and you can work from there. Now, here's a small suggestion right at this point because the way level builder and nested pre fabs in unity work, you might not want to save this prefab in the L B tiles photo. All right, maybe in some future update I'll make some changes to level butter. So there you have it. Now you can go ahead, experiment with your shopping platform, join me in the next video. We'll show you another cool gameplay feature called Moving Platform. Right. So we're going to implement it in the next few. I'll see you there. 16. Moving Platforms: moving platforms is another cool game feature that you learn how to implement in this video , right? Like so, by the end of this video, you would have learned how to create these moving platforms, which can move in the horizontal direction as well as in the vertical direction. And not only that, I'll also show you how to use these positions to make these platforms move in any direction . Right. So let's get started. Designing the platform should be easy for you. Select level map activated and then in paint more. These are the tiles you can select, like so, and by left clicking and dragging in the scene view, you can draw these tiles. However, by default, all the tiles you draw are drawn in the category brown. Now moving platform is something that should be above the ground. Right. So we're going to select high ground for parent name and let me zoom in a bit so you can see better for moving platform. We're going to select days tile, right, the one with the ridges and let's left like one to entry. And when you do this notice you have another category high ground in these three tiles. Let me show you. They have been pleased or shifted to the high ground category. Right? For better organization. Now in level map, go to selection mode and notice the's three tiles are drawn individually. We're going to group them. So you want to select these three tiles? Change parent aimed the high ground in click groups like tonight in button. And there you have a night in group Really? In this toe platform. Okay, this creates our platform. The moving platform. However, you want to make this platform move between two points may be here, in here. Okay. Or probably here, in here. So how can you get that done? Select high ground. Create empty and name this guy moving platform. Now what you want to do is select platform and you notice it's got the position. We want to bring this moving platform to the same position as out of this platform. And where is this moving platform? DoubleClick. Select this mortal, and here it is. Okay, so one way is you can drag it like so and keep dragging. Keep dragon. Keep dragging until you reach this platform. Well, there's a better way. You select this platform. I copied this component from here, and it copies or gets you the position. Go to moving platform the transform like the gear icon and paste important values and bingo . Just like that, you have to be moving platform reposition to the same position as that of this platform. Now, I want you to drag this platform onto moving platform, like so right. This makes this platform a child off moving platform. So if this moving platform game object is moved, the platform moves along with it. You may still be wondering why we're doing this, and that is because we want to create two positions, right? And the positions are not going to move as this top four moves. So the way to do that is right. Big moving platform. Create Mt. And call this guy Pos one. Or Position one. Let's give it a nice icon, this purple red thingy. And then I want you to select this and drag it here, then duplicated this guy green in this top position to and drag it probably to the right side. Okay. You can position it anywhere you want. This is just a demonstration. Weaken. Place it here or here or here. Okay, you'll see. Let's keep this configuration right. So you have a moving platform, which has two points, right? And this is the platform which is going to move in these points. And when it reaches this edge, it's gonna bounce back and go to this point. And then when it reaches this point, it's gonna bounce back. All right, that's the objective. Or trying to secure. Now select this platform. Click at component type in boss ladder toady and click Edit clatter and resized the clatter like So this looks about right. Okay. And if you're having problems, you can go to me and camera. You can turn off the BG jungle right here so you can have a better view of what's going on like so there, this looks more right. Then you can turn on the jungle. Okay, so you have a platform which has a boss Ladder 20 now. Want you toe change the Lear off this to a ground and yesterday, Children, and also tag it brown. Save the project. So that's about it. This completes the design of your moving platform. Lets go to play mode and see if the player Cat can jump on it and move properly. Already, it looks like everything is good, according to the design, but this platform is not moving yet, right? So let's see how to write a script and make this platform move. Between these two points, click add component type in moving platform. It returned to the Times, and this adds the moving platform script. Double click this field to edit the script in the script. Let's get started by adding to summary Now to get the two positions, let's declare to public fuse off type, transform, public transform, position one and position to like so and now you'll notice. In the moving platform script, you have to public feels position one in position to drag this position one game object onto position one fuel and physician to game objects onto position to a few. Next to move the platform. The platform needs to have some speed, so let's declare a public float speed like so and now this moving platform script has this public speed variable. Let's enter a value of two for speed. Now the platform needs to know which of the two points it should move to when you go to play mode, right, the starting position or movement. So let's say we want this position to be the starting position. Let's declare another public transform and call it start position like so or start position . I want you to drag position one and drop it on to start position. Feel like so in here. I want you to write transform, uh, position equals victor treat on Move towards and this takes in three parameters. 1st 1 is the current position. The current position will be the position off the platform. Target will be starred. Position dot position because start position is a transform and transform has positioned property. So like so and Max distance, Delta is the speed, so let's say speed and we'll supply the speed. But time, dark Delta time. We are multiplying time. No delta time would speed to get the speed in number of seconds, and that's it. This one line will start to move the hot form. The platform starts to move towards the start position, and that's it. It's not moving back right, because we still don't have any logic Done, tells the platform. The next position to move toe right? In this case, this was going to be the next position. So what can we do about it? Let's go to the script and in here, let's declare a vector three variable and let's call it next position, like so. And in the start method, let's right. Next position equals start position, Dodd position, like so then copied this from here. And for this target, we're going to say next position. So the platform will always keep moving to the next position and the game begins. The kids of the start method and this line, the next position will be assigned the position off the starting position. So it's gonna move toe worthies starting position, then in the update lock, we're going to say, if transformed, our position is equal to position one dark position. In that case, what you want to do, its next position should be changed to position to dark position. This is the platforms position, right? And this is position one's position. The moment these to become equal. We're going to change next position to position to adopt position. All right, this is position one. This is positioned to on the platform. Starts to move and reaches this position. In that case, the platforms, position and position one's position will be equal. In that case, you wanted to change the next position of this guy right position to. That's what we're doing here. Next position is equal toe position to dock position. So you may be wondering when it re gist. Position number two. What's gonna happen? Well, we need another if Block, copy this from here. Pace it here. And we're going to change these numbers. That's all. So now when the platforms position becomes equal to the second position here All right, then next position will be assigned this position. Position one. That's what we're doing here. Next position is position one, not position, and that's it. This is going to start moving the platform in these two points. Let's verify they're the platform is moving and it should return. Yes, it is returning. And from here will it return? Alrighty. So this looks like it started moving in these two points. All right, now let's check what's going on with the player. Let's exit the plane mode. That's maximize on plate and check again. Here's the cat and the cat jumps on the platform, like so but hugged the cat is not moving with the platform. What just happened? Let's give it another try. Is it something off a bug? No hopes the cat is not moving with the platform. So what is the mystery? Buying it toward? It's a small thing and let me show you. Let's exit the play more to solve this. You simply need to make this cat a child off this platform. And how do you do that? It's simple. You select the cat drag and drop it onto this platform. End up. The cat is now a child of this platform. Also, let's drop this cat here and test and now notice the cat is moving along with the platform . The problem's been solved, right? Also, if you drag the cat out of the platform, it's no longer a child of this platform. All right, this is important. To do this. You need a public reference off cat in feed controller in your let's declare public game object player like. So when you go to feed controller like so you notice you have this public few there. Drag this cat game object onto this player. Few and this is the first part now in this entre enter duty. Copy this if block and paste it here and we're going to change this to, uh, yeah, and in here we're going to write just one line of code clear transform. Docked here equals other God game object Transform. This one line will do the trick for you, right? But before that, you need to create this tag platform. Copied this from here. Select this platform. Click time time. Let's add a tag. Plop one now select pop warm and change its tag to platform. Now before you contest. Let's move the cat back to starting position here and let's test Alrighty. So let's jump the cat like so and notice. The cat is a child of the pop warm, and it's moving along with the platform. Right? Or here's another thing. The cat jumps and there the platform is still able to pull the cat right? So what's going on? Well, up till now, we've written code to make the cat a child of this platform, but we don't have any code to detach the catch from the platform, so let's do that in here. You have a void on trigger entered hoodie. This time you're going to need void on trigger exit tooty like So this takes in a collider to re other, and now simply copy this portion from here. Pacer here and let's remove all of these lines and that's right. Cleared. Transform parent equals. No, that's all. So what we're saying is, any time the feet of the cat leave the platform. In that case, this one line will detach the cat from the platform. Right? So save the script. Let's verify the cat is here. Let's show the cat like so in there. Now when you try to jump the cat, the cat jumps fine, and it's not being pulled by the platform anymore. So there you have it. A moving platform. But wait a minute, we can improve on it further. Okay. You see these two points? What if he could draw a line between these two points? Only in the scene view so you can see the line along which the platform is gonna move. It's very easy. Let's go back to the moving platform script and in here let's write void on draw gizmos on drug gizmos is a part of model behavior, and it's got the gizmos class. So let's say, gives most odd draw line and jawline takes in a vector three from Victor three to the From is gonna be positioned one dot Position two is going to be a position to a position, and that's all. Saved the script. Go back to unity and Bingo. Now you see, you have a nice line that's been drawn between these two points. All right, this line is not visible in the game view. It's only for your debugging purposes, so you have your moving platform. Now let's make a prefab out. If it drag and drop it onto the pre fabs folder and you'll notice a prefab, it's created for you, right? And the other thing I wanted to show you was, let's drag this prefab from here and it creates an instance. If you select one of these positions and I changed the position like so can you guess what happens? All right, let's see, Let's go play mode and there you have it. Simply by changing the positions, you can change the direction, invest These platforms are gonna move. So there you have it a nice moving platform that you can move in any direction simply by changing the positions. So I hope you're having fun doing this mobile platformer In the next video, I'm going to show you another cool feature that you can implement and add to your mobile top far, So I'll see you there. 17. Scrolling Backgrounds aka Parallax Setup: scrolling backgrounds is a popular feature in many platformer games. The player moves and the background starts to move in the opposite direction. Like you can see here, the trees are moving and the background mountains are also moving. Right now, in this video, I'll show you the basic set up off how to set of the backgrounds and how to add the textures and so on and so forth. And in the second video, I'll show you how to write code and make the fire lacks effect. Also, in the second video, you'll have a challenge of adding the third background that's gonna look something like this. All right, there's a fence in the player moves and all the three backgrounds are moving, according to the Parallax. All right, so let's dive in and get started with the Parallax Select Mean camera, right click and select. Create anti reading this toe parallax PG like so now, right Click Parallax PG, Select three D Object and select Quad Really, in this squad to be G underscore one. Now select the or tangle tool, and you can drag the edges off the court like so, and I want you to resize this to just about a bit bigger. Nandi camera size like so this is fine now. Remove the mesh collider. Uncheck received shadows, unchecked motion vectors turn off cast shadows turned the light throws off and reflection probes off and have a look at the position. Z. I want you to set this to a six. Save the project. This quad will act as the new background, but you may say there's another background, right? So let's check beauty jungle and turn it off. Okay, now there's no background here, so we need to find a texture to show the beauty. So let's make one from here. Let's search for existing BG sprites and ah, let's begin with the BG Jungle, right? I want you to duplicate this. Rename this toe BG Jungle Underscore T E X and then once you're looking the inspector and change the texture type from spry tooty to texture, change the rap mode from clamp to repeat and filter mode toe Beilin here max size to 512 from compressed changes to true color and then click Apply now, drag this texture onto this quad like so, and you notice you have some sort of a background, but it's very dark, right? As if it's way past midnight. Don't worry. We're going change this select BG one. You notice you have this materials expand this and you see a bunch of properties, right? Once you select Shater and cheated from standard to mobile particles also blended. And there you notice your background comes back in full color, right? But if you notice in my previous videos, I told you that the background color should not be as bright as the foreground color. Okay, It's a good design principle. So we're going to have to change this background. How can you do that? First, I want you to right click and select the folder in Mac, you gin Select revealing fighter. And this is where it is. I'm gonna use a open source tool called gimp. You can use any tool off your preference, give his open source, and you can download it from get dark. And we're just going to change the brightness and contrast a bit. So select colors, brightness, contrast. We're going to reduce the brightness just a bit like so right. So it looks like it's a bit darker and we're going to reduce the contrast This Well, there, this kind of looks. Okay. All right. So for brightness, its negative 33. And for contrast, it's negative. 18. Once you receive this and click file, override the jungle like so and it's going to override the changes when you go back to unity Notice. Now, the background is a bit lighter. Okay. If you go to play mode there, it's looking a bit nicer. Exit the plane mode. Highlight BG one. And now notice these bunch of properties that you see here. One of the properties is offset. Okay, let me show you something. This is the X value for off said this is why. And offset is a vector to If I change the X value like so notice the background starts to move in the horizontal direction. And if I changed the y value like so the background starts to scroll in the vertical direction. All right, so this is the secret of Parallax. So it's time to right some coat. Join me in the next video and we're going to use all of this knowledge to make this background move as the player starts to move and we'll go ahead and add another background , right? The parallax effect. So see you there 18. Parallax Scripting: By the end of this video, you would have learned how to write code and made the parallax effect right? Like so in this case, we have three backgrounds in this video. I'll show you to backgrounds and you'll get a mini challenge to do the third background. All right, so let's dive right in and write some code. Select BG one click Add component Type in parallax Hit returned two times, and this adds the parallax script to your GI. Want double click this feud to edit the script. Let's begin the script by adding the summary provides the fire likes effect shortened. Simple. And to do this, we need a couple of variables. 1st 1 public flowed speed. This is the speed at which the Parallax PG moves. We're going to set this toe. Jura points to reserve one and I'd explain why such a small number? Next, let's have another variable of type flowed. Call it off Set X This is the secret to horizontal Parallax. If you're wondering where is this offset ex? Let me show you selecting the BG one notice you have to use a bunch of properties under material. This is the officer it's a vector to, and it takes in X and y values well. I call this X value as offset X. And if I change the value here, the background starts to move horizontally. And if I change the value via the background starts to move vertically. This is the secret ingredient in Parallax. All right, now let's see how to change the offset through code. Let's declare a variable of type material and let's call it Matt. This is the material attached to the renderers off the quad. Okay, what is the one acting? Ask the background, right? It's got a texture. And in the start method, let's type in. Matt equals get component. We're going to get in a render, and the rhetoric is gonna give you access to the material property. So this one line of code is getting you the axis off the material and storing the reference in this variable Matt. Now in the update, let's write Offset X plus equals 0.5 f like so and matt dot set texture offset notice it takes into of values. The first is a property. Names were going to name the property. Now watch this carefully. It's an underscore. M a i n t E x right. The T and the M are in Campbell and the M has this prefix underscore, right? So it's under squirt main text. I'll tell you, this is how the texture is represented internally within the unity engine. And the second parameter it takes in is a new vector to and in here for X Ramona passing offset X And why is gonna be sure? All right, this one line is going to do the movement for you because we have pleased this line in the update method. If you're wondering why we're using a very small value for offset X, here's an important thing. If you use a value off one and above, it's going to start moving the background really fast, right? You don't want to do that because it's not good for user experience. You want to keep this value very small. All right, this is just an example. Now, let's go to play more and see what this line does for you. And then I'll come back and explain notice the background has a nice crawling effect. All right, so it's working now. Let's exit the play mode and let me explain what's going on behind the scenes. This one line of code is doing the trick for you. And this variable can invoke the set texture, offset method. This method takes into parameters the name of the texture you wanna work with and a new elector to Why a vector to? Because the offset let me show you. This guy is a vector two takes in an X and A y were interested in changing this X We write new elector to and passing Offset X asked the X value and we're not touching the y. So this does the trick for you. However, you may ask, this is all good, but we're using a predetermined value. How can you move the background, ask the player moves and that's exactly what I'm gonna show you. Now you notice this public float speed variable, we will use it now. First, I want you to delete or maybe comment this line out for your future reference and instead we're one right offset. X plus equals input. Dog, get access, raw horizontal and we're going to multiply. This would speed and that's it. This one line will do the trick for you. But before this we need to assign the value of speed because it's a public variable. Right? So let's go to unity and set the value 0.0 hours or one, right? Remember I told you higher values will move the background very fast. Let's test now. You can use this mountain as the reference right. Notice that as the player moves, this mountain will move in the opposite direction. Here. Notice. So you have some scrawling going on in the background there. Okay. If you disable the parallax script and notice what happens with the mountain notice the mountain is not moving inch. The background is not moving. So you can say the Parallax script is working Now. Now, let me explain what was going on behind the scenes. This input dot get access raw is being called when you're pressing the left and right arrow piece on the keyboard, right? Or even if you're using a joystick, it's gonna work. And this method returns either one or negative one or zero. You know, from previous videos that have players ex velocities, one player is moving right and negative. One moves player left and zero means no movement. So we're changing the value off offset. Based on what? Input dot Get access raw that that is returning to you. This starts to move the BG. However, if you notice there's something that we need to fix All right, let me show you. Let's go play mode and now notice. There are two things. First, when the cat gets stuck, the background is still moving right. That shouldn't happen. And the second thing is, if I used the mobile left and right buttons. The parallax is not happening here. Notice the mountains are not moving. So what's going on? We can solve those problems using a simple approach. All right, we're going to create to colliders one of the left, one to the right of the cat. And then we're going to check if any time the colliders collide with any obstacle like these stairs or these boxes, the Parallax script will stop scrawling. All right, so for this, let's exit the play mode, Select the cat break leg, create empty, and let's really in this toe left check. You can double click on the cat to zoom in a bit. Select left check. Click add component Typing edge collider Tory. And we're Goto added d clatter. So you select the move tool, Move the clatter to the left of the cat. And now notice the cat has this left side of the boss the latter we're going to adjust this edge clatter so that it kind of aligns with this left side. All right, let me show you. Let's check. Yes, it's very much on the lost bladder. You can kind of move this a bit to the left, and this kind of looks okay. Now, I want you to select tag ad tag and adding attack called Player stuck, then assigned this left. Check this player stuck. Tack. Next click. Add component. Anti vein clear, stuck. And it returned two times. This adds D player stuck script duplicate left check. Rename this to write, check and drag it to the right. Like so Right. Very try and position it. Ask Close as possible to the right side. Off the catch. Bosco. Letter. Right. This looks about right. Okay. She had this kind of a configuration. Now, now select right check. And in here. Let's add in the rigid body to re component. And let's put a check for this. Property is schematic. Also, you want to check his trigger for the escalator? 20. This is an important step. And what did he done this for? Left check. Now, let me explain why we're doing this. Because we wanted the collisions for dese two child game objects left check, right. Check and the cat parent game object to be separate from one another. Right. If we're not using this set up here, left check is gonna interfere with the collisions off the player cat. In fact, you'll have multiple collisions. For example, of the cat picks up a coin, three coins will be registered instead of one. Right? Or in other words, three collisions will be detected. All right, 14 Right. Check one for left. Check one for cat. So to resolve this problem, we have this configuration notice, right? Check left, check. And the cat they belong to this layer flares. So what's special about it? Who did it? Project settings and physics story. And this is the Lear collision matrix. All right, This is very useful in determining which layers will collide with which Lear. So if you notice players versus players is unchecked. Any game object belonging to layer players will not collide with another game object belonging to layer players, right? This is important, right? Check left, check and cat belonged to the layer players. So between these guys, there's not gonna be any collision or trigger introduced. The method being called now open the player controller script and let's declare a public bull is stuck. Now you can select any of the right check or left check and let's double click the player stock script to edit it. Let's begin by adding the summary and let's declare a couple of variables. The 1st 1 is going to be a public game object clear. To get access to the play controller script. Another variable of type thinking toner. Call controller to reference Steve There controller script attached to the cat In the start , let's say flare controller equals clear dot get component Clear controller like so next you can delete the update method and instead, let's right to meth. It's void on trigger. Enter Tory, and in here we're setting the 1,000,000 variable is stock to Truth and then on Trigger Exit Tuti and in here this is stuck will be set to false. All right, these two entities are going to help you check when the cat gets stuck somewhere. Now let's open the Parallax script. This script is attached with the Parallax PG one and in here. Let's declare a public game object clear and clear water. And now let's copy this piece of code for a while and put it in. And if lock and let's say if player controller dot is stuck, if this is not true in that case, go ahead and show the Parallax right? Let's put a comment and was the keyboard Enjoy state, right? What about the mobile for Mobile? You want to go to play a controller and notice the player controller has these two billion variables left pressed and right? Pressed right? I want you to change them to public. And that said, when she do this, you can go back to Parallax And in here you can write if clear controller dot left breast, then offset. X plus equals negative speed else. If controller dot right pressed offset X plus equals speed, right? Let's put in a comment handles the mobile parallax. Now, before you test you need to specify a couple of public variables in Unity Select right chick and then dragged the cat game object under the player Few list of the same with left Check Drag cat Onto this player came object Few then select BG one Drag and drop the cat here like so And when last thing go to the Parallax script here and in here, let's write in. Clear Controller equals there. Get component Clear controller like so and now you contest. Here's the player I pressed Eat right mobile button and notice The mountains are moving so parallax is happening and bingo the players stuck and the parallax is not happening The left button is also working So there you have it. Just solve this problem So now you know how to make one parallax BG. You may be wondering how to add two or three of Parallax View Jeez right Well, it's simple duplicate BG one and let's call this BG to and for a moment let me turn off to t and show you things in three dimensions. What exactly is going on? Buying the scenes? You notice this is the background. This is the ground and the rest of the stuff. There is some distance, right? This distance is determined by the blue arrow. The blue arrow you can see here is the Z axis. This guy so or B two and B G one Z axis is six, right The value of same. Let's create some distance between the two so far BG to I want you to change this to four and notice you have some distance now, right? These two backgrounds have this distance Now when you go to the Tuti, let's turn on Toby, you'll be able to notice when I change the texture for BG to there will be some sort of three D effect or parallax effect. Now we need a new texture. So I want you to type in BG and ah, notice you have a trees BG This is the second background texture right now this would be spry tooty change this to texture from clamp change. The rap mode to repeat filter mode is gonna be by linear max. Size will be 5 12 and format is gonna be true color. Then click. Apply now, select BG to and drag and drop this guy on to be to and there you have it. You have a nice background. Next looking the parallax script and cheese this value to syrup When jurors or 18 I got these values through trial inner during my different projects. And before we test small change here, type in fragment. All right, you've got these two blocks rag wins, select both of these guys and in the sorting Lear wanted to change this from default to foreground. Right? See of the project and now you contest. So here's the player cat and notice those nice trees in the background. When the player moves, the trees are moving, so that's great. And you contest these mobile buttons and the trees are moving so everything is set up correctly. It looks like the player can jump and rigged These creates and it looks cool so you can see your mobile platformer is coming alive. Right? At this point, you might also want to go ahead and deep. Lie this to your android device to get a feel of how this is shaping up and how it feels like on an actual android device. And now it's time for a mini challenge. Your mission is to add 1/3 BG. I showed you how to add to beegees. All right, you can use any BG off your choice or there's a BG in this price folder. Let me show you. If you look in these sprites environment, there is a fence BG All right. It's off type sprite duty to go ahead and try to create 1/3 BG with a nice parallax effect . All right, there's a small clue that I would like to give you When you create this fence, you'll notice that this fence is not so big as this background. If you select this rectangle tool and you select one of the quads, you can actually resize the quartz, right, so that you have some space for the backgrounds behind it. Right? So that's the kind of foot clue for you. And once you've completed this, let me show you how it's gonna look like here, Steve. Third background. All right, so this is how it's supposed to look like. Also noticed that the creates and the ground these guys are moving way faster down the fence and the fence is moving faster than the trees and the trees are moving faster than the background. The mountains, right? This is the secret of Parallax. All right, I want you to go out and practice different values. Also, if you face any problems adding the third background, I'll probably add an update lecture in the update section or in the bonus section. Right? Feel free to send me comments and suggestions through the discussion area, and I'll be more than happy to help you. Right? Go ahead. Give this a try. And this is how it's gonna look like. So there you have it. Parallax effect in a simple and easy to implement matter, Right? Hope you're having fun. Join me in the next video where we will add some more cool game features. All right, see you there.