HOW TO CREATE PHYSICS IN UNITY 🎮 | Rigidbody And Colliders In Unity | | Unity Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in unity we sometimes want our characters to interact with the rest of the game and to do this we do have a couple of different components built into unity that can add both physics as well as colliders into our game objects now i will be working in 2d but this is something that will also work for 3d games in case you're working in 3d instead so let's go ahead and start by just simply creating a player character so what i'll do is i'll go up into my hierarchy window i'll right click go down to 2d object and i'll say sprites square now we'll just go ahead and rename this one for player just so we know exactly what it is and of course if you're working in 3d you just simply go up and create a cube instead under 3d objects so now that we have a player we can actually go ahead and just click it and inside our inspector window we can see that we have a couple of different components we have the transform and we also have the sprite renderer now the renderer is just simply going to be showing our character inside the scene but it's not actually going to have any sort of physics or collision or anything detected on the player so that's something we're going to have to add separately so to do this we just simply need to go down to add components go down to where it says physics 2d if you're not working in 2d but in 3d you just go down to physics instead and we're going to be adding a rigid rigidbody2d which is going to be down here at the bottom it is important to note that some of the components inside the component menu are going to have a 2d option so some of these you do need to sort of remember oh wait i'm working in 2d so we need to add the 2d component instead but once you have the rigid bonded 2d we actually now already do actually have physics applied to our character now before we hit play i'm just going to go ahead and reset my character so i'm going to right click in the transform and just say reset and then we can actually go and just click play up here in the top and then you can see that we do have a character that sort of has physics applied to it so it drops down to the bottom there so it's working great now of course we don't always have the same physics settings for all the different game objects inside our game so i just sort of go through the rigid body 2d over here and just sort of explain what exactly you can tweak and adjust inside of it the first one is going to be called body type and this one can be set to dynamic kinematic and static depending on what kind of behavior you want this object to have when it comes to physics you might be choosing one or the other you're most likely not going to be using kinematic aesthetic until just a little bit further ahead when you want a very specific type of physics going on on your object so just go ahead and ignore it for now just choose dynamic on the material we can add physics materials which is not the same thing as the material inside our sprites up here inside the sprite renderer the one inside the sprite renderer just basically tells it how to render into the scene you know with lighting or reflection so it's more visual sort of thing but the one we have inside rigid body is how the physics should have some sort of interaction once it starts you know connecting with other objects inside the game like should it drag along the floor or should they have some sort of bounciness going on to it so this is not something we're going to be touching for now but this is something that you probably will be touching at some point simulated is the one that actually makes sure that colliders and something called joints can actually affect our objects so for now well most of the time you just want this to have ticked on use auto mass is something that is quite cool it actually goes in and checks how big the object is based on the colliders on it but now we don't always want to have this setting on because let's say you have two exactly the same size circles inside the game but one of them is a balloon and the other one is a bowling ball then when they hit each other it shouldn't be you know a similar force that is being hit between them so we don't always want to have this auto mass ticked on and then we have something called mass which is where we can add the mass manually so if we want to have a balloon we can actually decrease this one and say well this is a balloon so it shouldn't have a lot of mass or if we're doing something like a bowling ball then we can increase the mass to make it a lot heavier and have a lot more impact once it hits something else now the drag options that we have down here linear drag and angular drag drag is basically air resistance so let's say i have an object inside the game and i throw it then the air resistance is what is going to stop the object from from continuing so if the air resistance is set to zero then the optic is just going to keep going and going and going until something else stops it because there's no air resistance now i called it air resistance some people do also refer to it as damping the linear drag is going to be the one that decides how much air resistance or damping that should be in a direction whereas the angular drag is the one that stops the object from rotating and then we have the gravity scale which is just basically when we want to add gravity to our optics so zero would mean that it's floating around in space in zero gravity and there's nothing pulling it down to the ground so just to actually show you this i can't set this one to zero and once i play the game it's not going to fall down anymore it's still going to have physics applied to it but not until something actually acts a force upon it so it's not gonna fall down and basically just float out of the screen anymore if it were to sit this one to something like 20 which is quite a lot then you'll see that actually really fast just sort of slides off screen here basically we didn't actually see it fly off screen it just disappeared now we do also have something called collision detection and this is something where it is going to say discrete as a default but if you have an object that moves really fast and crashes into another object then if it's set to discrete your objects might go through each other even though we do have a collision detection added to our object so it will still just sort of like hit and register the hit but it'll blast through so the collision detection should be set to continuous if you want to have an object that shouldn't be stopped if it hits something you know it shouldn't go through the object now interpolate is something that we use in order to sort of prevent any sort of jerky or jittery movement from our game objects so let's say we have a car game and we want the car to crash into another car and for some reason the physics is very jittery and it shakes and vibrates then this option here can actually help us sort of prevent some of that jitterness that might be happening and then we have something called constraints now constraint is a way for us to freeze the object in either a certain axis or by not letting it rotate so if i have a 2d game like let's say a platformer i might not want my optic to randomly rotate so my character's jumping around on his back so what i can do is i can lock the rotation and just you know sort of prevent any sort of weird directional movement that i don't want to have inside my game so now we know how to add physics to our game let's go ahead and talk about something called colliders which actually goes hand in hand with our rigid body let's go ahead and add in another sprite so i'll just go ahead and right click i'll create a 2d object and i'll create another square i'll go ahead and call this square floor just to give it some kind of name and i'll make sure i want to reset it so right click reset and i want to scale this one out so we have it going out far out on the screen i see for some reason i have my main camera disabled up here if you want to disable any of the game objects inside the scene when you're looking at it inside your scene view you can just sort of click the eye icon and then you can actually see it goes away so just a quick tip for you um let's go and make the floor reach the edge of the camera so right now it's reaching the edge and what i'll also do is i'll move it down the y axis so we have it going down just a little bit that's also going to make the player red just so we know exactly what is going on inside the screen and what i'll do now is i'll click play and the expectation is that the player is going to be falling down until he hits the ground and then he's just going to sort of land on the ground right so we'll click play you can see that instead of hitting the ground he's just gonna fall right through it which is not really what we want let's go ahead and fix the background color for some reason i don't like having this color here it's a lot nicer looking if you ask me and this is where colliders come in colliders are basically components that we can add inside our game objects so in this case here we do have physics applied to the game object but it has no edges that can actually detect any sort of collision so if two objects right now are hitting each other they're not going to detect any sort of collision going so they'll just sort of keep going through each other what we'll do is we'll go ahead and make sure we have our player selected go down to add components inside our inspector and i'll go down to physics 2d again this is going to be physics if you're working in 3d and i'm going to be selecting a collider that is going to be matching the shape that i have up here the best way possible so right now i'm working with a square so the box collider 2d is the one that seems to be the one that works with it so as you can see once i add it we get this little weird green border going around our character the green border is basically the collision detection that is around our player so if i were to also go down to the floor and add a collider to that one so in this case i'll also go ahead and select a box collider since this is also essentially a box and if i were to click play you can actually see that now since we do have some collision going they actually hit each other and just like you saw me do make sure you have a collision detector on both the player as well as the objects the player needs to be hitting and now you might have a question for me because what if we have a very complex object inside the game like let's say a tree we don't have something called a tree collider inside our components down here so how are we going to have it register the edges of our tree so that it has proper collision going on it well let's say you have a very complicated looking game object inside your game what you can do is if i were to click back to my player is instead of choosing the box collider we can actually go ahead and choose something called a polygon collider so if i go inside my physics 2d and we go down to the polygon collider 2d this particular collider is just sort of going to look at the object and say okay what kind of object are we dealing with and just sort of apply a collision detection all the way around the object so this is a really neat feature i could imagine some people just say well you know what the polygon collider is just gonna do everything for us so let's just use a polygon collide of everything you want to make sure there's as little complexity in your colitis as possible so if you were to have a square make sure you choose the box collider for the square so i'm just going to go ahead and remove my polygon collider again and the last thing i just want to mention here is that depending on what kind of collision you've added to your object so for example a box collider is not going to be the same thing as a circle collider there will be some different properties and settings inside the collider but i'll try to go through the more important ones that i know you will be seeing on most colliders inside unity so let's go ahead and go through some of the different settings we have inside our collider now edit colliders is basically going to let you edit your collider so it's doing exactly what it says it's going to do so if you were to click this one you can see that we can now move around the edges of the collider meaning that right now if we were to do something like this what it'll do if we were to play the game is that you can see that only that small corner of the box is going to detect any sort of collision and now we do have something called is trigger and this is actually a very important one because you will be using this one quite often when you're making any sort of game inside unity if it were to take on is trigger and actually go ahead and play the game you'll see something a little bit weird going on because the object is no longer colliding with the floor this is happening because the collider that i added to my object is no longer going to detect a hit and then actually see it as you know two objects hitting each other instead it's just going to see it as a trigger and it's basically used for for example if let's say we have a character that is walking through the level and we want to detect when the player is reaching a certain point in the level and once it reaches that point we want to spawn in an enemy or something then we want to have something called a trigger instead another example could be for example a light switch if i were to have a player character that walks around a house and then he goes over to a light switch and he wants to interact with it then we can actually detect when we actually try to go for the light switch but we don't want to have the lights which block our way or something so let's just go ahead and untick this one again because we do actually want our box here to collide and actually hit something once it hits something right we also have something called use by effector which is basically a setting we can tick on if you want this game object to be affected by something called an effector effectors is something we can add if you go down to component you can actually see that we do actually have them down here and as you can see we have a bunch of different effectors and we're not going to talk about what they do just quite yet and then we have something called offset and size now offset is just basically going to take our collider that is around the object and we can adjust where we want it to be the size is used to increase the size of our collider so let's just go ahead and reset the offset here so i can increase the width or i can increase the height if you're working with a circle this is going to be called radius instead and with this you know the basics of how to add physics to our objects and how to add colliders to our objects i hope this kind of gave you an idea about how we use physics and colliders inside unity i know we didn't talk much about scripts for example you want to push the player and actually move him around you know like an actual game but this is something i decided to split into a separate video so that is something i'll be working on next so i hope you enjoyed this video and i will see you in the next video [Music] you
Info
Channel: Dani Krossing
Views: 21,554
Rating: undefined out of 5
Keywords: Add Physics To Your Player, HOW TO CREATE PHYSICS IN UNITY, unity, learn unity, unity game development tutorial, unity game development course, how to make a game in unity, game development, learn to make games, how to install unity on windows 10, how to install unity game engine, how to install unity, introduction to unity for absolute beginners, Rigidbody And Colliders In Unity, rigidbody 2d movement unity, unity physics tutorial, unity collision tutorial
Id: 7LVOeNalTFA
Channel Id: undefined
Length: 13min 26sec (806 seconds)
Published: Fri Jul 30 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.