2D EFFECTORS in Unity - Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in this video we're going to have some fun with some interesting 2d physics components they called effectors and they can actually be really useful we'll have a look at the area effector the surface effector the point effector the buoyancy effector and the platform effector also I'm sorry I missed the video last Wednesday I caught a cold and yeah it was bad but I'm over it so let's get in turn so in this scene I have a background sky they ground with the 2d box Collider and an asteroid the sprites that I'm using are made by unity and are available for free on the asset store that will be a link in the description so first let's go ahead and add a new circle Collider to our asteroid let's focus on it and let's decrease the radius so it matches let's also go ahead and add a rigidbody 2d in order to apply physics to our cake so now when we hit play we should see it fall to the ground cool the first component I want to have a look at is this surface effector if we go ahead and select our ground hit add component and add the surface effector 2d in order to use this we'll have to mark our Collider as used by a vector so what this component will now do is apply a force along the surface of the collider in order to match a specified speed think of the way a conveyor belt works at the airport so if we hit play now we should see that as soon as our ball hits the surface Air Force does indeed get applied and it slowly rolls along the surface we can decrease the speed here and actually make it go the other way we can also change the force scale this is the amount of force that we're going to add to our object in order to try and achieve the desired speed so if we set our speed to say 5 and bump up a force scale to 1 we should see that our ball immediately accelerates however if we set ours for scale to something smaller say point 0 1 we can see that it slowly increases in speed the speed variation allows us to add a random increase in speed we can also make this negative remember you can always control the collider in this component effects you do that by simply disabling a layer from the collider mask so if we didn't want this to affect water we would disable water and this is true to pretty much all of the effectors finally under options we have the possibility of using contact force this means that the force will be applied at the point of contact instead of at the center of the collider so if we enable this we should see that it very quickly rotates that's because the point of contact is always at the bottom of our sphere and so it's going to add torque because we are pushing at the bar my first fear and so we'll start to rotate around its center I'm just going to disable this for now and at the very bottom here you have two toggles design multiple of the effectors and they simply allow you to enable or disable friction and bounce from the physics material sitting on this object so next up is the area effector if you go ahead and right-click hit create empty let's reset the transform on this object and call it something like force field we then add a new component the component we need is a box Collider 2d this is required by the area vector well then add the area effector itself again we'll have to check use by a vector and for this component we also want to mark the collider as a trigger so the area effector applies forces within a certain area and our area is defined by the size of our Collider so if we're going here and say scale up this component a bit and we can maybe also move it over to the right here we should now see that whenever our asteroid enters this field it's going to apply a force in the right direction and it's going to do so with a magnitude of 10 so if we hit play and see our asteroid fall to the ground you should see it speed up as soon as it gets to our area effector it did but it wasn't too visible we can go ahead and up the magnitude let's set it to 30 and we can also add an angle say if we wanted this area effector to have the ball shoot upwards well then we would go ahead and add the force angle of 90 degrees because if we move 90 degrees around the z axis it's going to shoot up so if we now hit play and wait for a meteor to enter the force field we can see that it gets shot up into the air this angle is currently relative to the orientation of our object so if we go ahead and rotate our force field along with the axis we should see that the direction in which our asteroid gets shot does also change if this is not what we want we check use global angle and so it will always get shot upwards there a vector also allows us to add some variation to our force and choose where we want to apply our force if it's set to rigidbody it's going to apply it at the center of mass of our rigidbody 2d and if we set it to Collider it's going to apply force to the current position of our Collider in our case that should mean the same because we're dealing with a totally circular object but for more complex objects that might make a difference finally we can also apply damping to a rigid body or in other words we can apply linear air resistance or rotational air resistance I very rarely find a use case for this next up is my personal favorite it's the point effector the point effect that apply as forces to either attract or repulse against a certain point note that I've set the gravity of an asteroid to zero so that it's just going to hang around in empty space in order to add a point effector we're actually going to have to add yet another Collider to this object yes you can have more than one circle Collider on an object so we'll add a circle Collider 2d and this one we're going to bump up the radius quite a bit that's because the radius here determines whether or not a nearby object is going to be affected by a point effector so let's make the reach of this fairly big we can then mark this as trigger and as used by effective let's then add a component and this is of course going to be the point effector now the force magnitude is of course the amount of force that we want to apply and this is negative if you want it to attract and positive if you want it to repulse if we go ahead and duplicate this asteroid now and move it over here and hit play we should see that that you very quickly clash together however the force that is being applied is currently constant meaning that as long as these two objects are within reach of each other we'll apply a constant force with a magnitude ten to each asteroid in the direction of the other asteroid this is fine for in many cases but when it comes to gravitational attraction as is the case between these two asteroids a good friend Newton actually stated that the force being applied to these is proportional to the product of the two masses and inversely proportional to the square of the distance between them we call this law the law of universal gravitation and the relation between force and distance we call that inverse squared so if we want fairly realistic gravitational simulation that is what we would use though if I go ahead and apply that both our objects here we should see that the closer they get to each other the quicker they will accelerate we can then grab any of these asteroids here and you can see it's really quick and fun to play around with we can also duplicate one of them to introduce even more into the scene here and you can see how quickly you can get fairly realistic gravitational movement and it's really easy to go in here and tweak different parameters you could go in here and thumb up the mass on one of these objects so that is actually not going to have any effect on the force being applied so what you could do is increase the force magnitude together with the mass of the different objects and that would mean that the more heavier objects applied a great attraction or force to the audio objects as stated by Newton's law but if you want to learn more about this sort of stuff I suggest to check out my video on forces where we go a lot more in depth so just click the covers around the asteroid it's a bunch of times and I also made this ball here which is the exact same thing except it has a positive force magnitude now when we hit play we should see some of the objects here stick together and some of them are repelled by the pole then we have the buoyancy affected to D and probably the most common use case for this is water or other kinds of fluids and it basically gives you a bunch of options to determine how an object looks load on top of a surface so in my case here I went ahead and created this water sprite in Photoshop and I added a box Collider 2d to it again we also have the asteroid here with a circle Collider in a rigidbody right now when we hit play our asteroid simply lands on the water and it doesn't look super good to change that let's go to our water and let's go ahead and add a new component we want to add the buoyancy effector 2d we also want to mark our Collider as trigger and as used by effector and now right off the bat when we hit play it's actually going to look a whole lot better the two primary things that you want to adjust here is the surface level this is just the surface location of the buoyancy fluid so if we go ahead and subtract a bit from this you can see that our object will find rest at a lower point for example in this case I think it works a lot better if our surface level is actually a bit deeper and then we have the density of our fluid and without getting into a deep explanation of Newtonian fluids and fluid mechanics I will say that the more dense a fluid the easier it's going to be for objects to float on top you can see this as I raise the density of a fluid our asteroid will be pushed to the top and as I decrease it it will actually start sinking you can also see what happens if we have a very sudden shift in density the forces that are being applied are going to be so great that our asteroid will be shut up words finally a buoyancy effect it also allows us to adjust damping as we've seen before and even add flow along a surface this works in pretty much the exact same way as our area effector we have a magnitude which we can also use the control Direction an angle and a flow variation the final thing that I want to cover in this video is the platform effector this component applies various platform like behavior the most important one is probably one-way collisions I've set up this example here we have our asteroid sitting at ground level right on top of that we have a force field it's going to apply an upwards force making it jump upwards and we have a platform with the Box Collider 2d so right now we hit play and our asteroid gets shut up and it's going to hit the platform and it's not really going to get anywhere but a very common thing that you want to do in platformer games is give characters the possibility of jumping on top of platforms by first going through them and then landing on them and this is what we referred to as one-way collision to add this we're simply going here at the platform effector 2d we then want to check use by vector not as trigger this time and make sure that under the settings under one-way it says use one-way now when we hit play our asteroid should shoot right through the platform and land on top of it if we go into the scene view we can also see that we now have this arc on top of our platform this is adjusted using the surface arc and this arc basically just defines the surface that doesn't allow colliders to pass meaning that anything outside of the arc is considered for one-way collision in most cases this is just going to be 180 you can also use the rotational offset in case you want to flip things around if we set this to 180 now take our asteroid here and drag them up and have them fall down you can see that it now works in the opposite way and finally under sides here we can adjust whether or not we want to use side friction and side bounds this means that if we have some kind of physics material applied to our platform say we wanted to make this platform bouncy we can choose whether or not we want that to take effect when we hit the sides of the platform so let's go in here and create a physics material and let's call this one bouncy we can set the friction to point 4 that's fine and the bouncing is to 1 then under our platform that's going to box Collider 2d and drag in our bouncy material we should now see that when we hit play our platform is indeed bouncy but since use side bounces currently turned off if we just disable use one way here let's rotate the platform 90 degrees around the Z let's also move up our asteroid we shouldn't actually see bouncing and indeed it does not so use this component wisely it can definitely help you get around a lot of the annoying control stuff when making a quick 2d platformer let's pretty much it for this video I hope you enjoyed it if you did make sure to subscribe so don't miss thanks for watching now let's see you in the next video thanks to all the awesome patreon supporters who donated in April and a special thanks to Derek huge jerk face llanera phi james callaghan Sybok Nami Cole cabral and Jason de Tito if you want to become a picture in yourself you can do so a pinch ENCOM flash bracket
Info
Channel: Brackeys
Views: 249,932
Rating: undefined out of 5
Keywords: brackeys, unity, unity3d, asset, assets, beginner, easy, how, to, howto, learn, course, tutorial, tutorials, fix, tip, game, development, develop, games, programming, coding, basic, basics, C#, physics, gravity, effectors, effector, bouyancy, water, forces, force, 2dphysics, 2d
Id: p0n6EFR1M8c
Channel Id: undefined
Length: 11min 26sec (686 seconds)
Published: Sun May 14 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.