Collisions and Triggers ("OnTriggerEnter not working!")

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to cover how to handle simple collisions in unity we're going to look at colliders rigid bodies and on the various events let's begin [Music] hello and welcome I'm your code monkey and this channel is all about helping you learn how to make your own games with enough tutorials made by a professional indie game developer so if you find the video helpful consider subscribing okay so this is a very simple beginner video I'm making this because I've seen a lot of comments by people confused with how to get collisions working one of the issues I see is a lot of people cannot get their triggers to register it can be a bit tricky so let's check it out so for starters let's make a new game object so in here create an empty one and let's call this our health potion and now let's add a component of type box Collider 2d so here you can already see the widget displaying the size of our Collider now let's also add a sprite renderer and now for our visual over here I have a health potion texture and just drag it on there okay and now we can click on this button in order to easily add a deco editor and make it just big enough to capture the whole texture okay alright so here is our simple object now let's make another object so we're going to create an empty game object this will be our player and inside we have a component of type capsule Collider TD okay and then also a visual so a sprite renderer alright so here is the sprite with the capsule Collider on top so now that we have these two simple objects let's see how we can handle collisions so first of all on the health potion over here on the collider properties you can see we have a bunch of things we can change for example over here we have a box called is trigger this makes it so that this object is not physically solid and it only triggers an event this is right for example for making the player pick up an item so we do want the player to be able to get inside of this box so this meant to be just the trigger okay and for the player will leave it as not a trigger since we want it to be a solid object okay and now in here I have a simple script with some basic movements so just add it to the player and here you can see these script is extremely small very simple here we just have a function that being called on update can't handle our movement and we're testing for inputs and moving our transform that's it very simple and now in order to capture our trigger we need to they function so we're going to add the function on trigger answer to D so this is the function that is called whenever this object is involved in a collision with a trigger so here in let's just add a debug log and say something all right so let's test okay here we are and I can move the play around all right make sense and now let's check out the world whenever I approach each item and I go and nope we have nothing happening so this is the part that really confuses some people they both have their colliders correctly set up so why is nothing happening well the reason is because the physics system is meant to work with rigid bodies and not just empty colliders so here in our player we need to add a rigidbody in this case rigidbody 2d and here since we're working on top down in order to make it not fall down just set gravity to zero however the item itself does not need a rigidbody in order to get the collisions working we just need one of the objects in the interaction to have a rigidbody so the item is fine just like this just a box Collider with its trigger set to true and the player has a capsule Collider and a rigidbody okay so now let's test this okay here we are making some move and if I go towards the atom there you go now we have our triggers correctly working awesome so as you can see it's very simple to fix this issue with collisions not triggering all you need to do is make sure that you have at least one rigid body in that physics interaction so now let's look at some more collisions alright so over here I made a static wall and a bunch of debris so the wall is meant to be static and we should be able to push the rocks around so here on the wall all we have is a simple box Collider and you can see it is not a trigger and on the rocks they also have a box Collider and in addition to that they have a rigidbody set the dynamic we have the rigidbody since we want the rocks in order to be influenced by the physic system and over here on the player here on let's set the rigid body from dynamic and put it as kinematic so this is important and we won't see what this does and I'll back in our player code we should rework our movement code and move it into the fix update due to physics okay so here we're using the rigidbody2d move position function in order to move our player okay let's see okay here we are and I just replace the player with a nice animated character instead of a simple sprite but it works exactly the same so I can still move just like normal and now over here we have our rocks and if I move towards them yep there you go as you can see I can correctly push around the rocks alright so that looks good however let's go into the wall and there you go the wall does not work and I can go straight through it so here we have some weird issues now the problem in this case is because we set our rigidbody to be kinematic so kinematic means that it will not be affected by outside forces so as I move the rigidbody towards the wall the wall will not be able to block it but being kinematic it still effects other objects so that's why we can still push these rocks around now here if we modify the original body type from kinematic and place it as dynamic and now we test okay so here we are and yep I can still push the rocks okay great and now on the wall any of there you go now the wall does indeed work as a solid object so if you have issues with your objects not colliding pay attention to whether or not they are supposed to be kinematic if you want to learn more about physics interactions including how layers and bit masks work then check out the video linked in the description now let's look at a bunch more collision events so here we played around with the ontriggerenter 2d which gets called whenever a Collider enters another one that is set to being a trigger then we also have the proud void on collision enter 2d this one works the same when it enters accept this works with a Collider that is not set to being a trigger and another difference is the parameter in this function is a collision instead of a Collider so the collision contains a bunch of information on the collision that happened like all the various contact points collision velocity and so on then we also have the on collision exit and on trigger exit so these get called whenever the collision between the two objects ends and then finally we have the on collision say and on trigger stay these are called on every physics update while the collisions are active so let's have logs on all of them okay let's test okay so here we are moving around now let's test so first let's collide with the wall and there you go we have a collision enter a bunch of collision says-- and finally a collision exit then over here we have our health potion which is set to a trigger so that when you go in if there you go we have ontriggerenter a bunch of trigger stays and a trigger exit at the end and finally over here on the rocks we can push them around and there you go we have a interstate exit just like that right so here are all of our nice collision events okay now let's apply this to some interesting behavior okay here's my player again now let's move towards the wall and yep some works as a simple solid object is it supposed okay now over here the rocks let's push them and there you go with the rocks are now somehow super heavy and they caused me some nice damage so the players nowadays over here we have the health potion and let's use the trigger in order to go in there capture and consume the health potion and then all the way in here we have this very nice special circle and as I go inside there you go we activate a very cool awesome shader effect and if I go out it gets is able so there you go enabled on enter and disabled on exit if you want to see how this cool walking shader works then check the link in the description so over here we have a very simple scene working with all kinds of physics interactions and collisions again if you have issues with collisions make sure that at least one of the objects in your collision has a rigidbody and check if it should be kinematic or dynamic as long as you can download the project file signatories from unity code monkey comm subscribe to the channel for more et tu Turtles post any questions at have the comments and I'll see you next time [Music]
Info
Channel: Code Monkey
Views: 122,234
Rating: undefined out of 5
Keywords: unity collision 2d, unity colliders not colliding, unity collider, unity ontriggerenter2d, unity ontrigger, unity ontriggerenter, unity trigger, unity collision, code monkey, brackeys, unity tutorial, unity game tutorial, unity tutorial for beginners, unity 2d tutorial, unity 3d, unity, game design, game development, game dev, game development unity, unity 2d, unity 3d tutorial, programming, coding, c#, code, software development, learn to code, learn programming
Id: Bc9lmHjqLZc
Channel Id: undefined
Length: 9min 4sec (544 seconds)
Published: Wed Dec 18 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.