Unity DOTS & PHYSICS - Raycast, Rigidbody, Add force

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
physics are used in almost all games we'll take a look at how we can make multiple objects interact with each other using physics in unity dots how we can apply Force to the player to make him move and lastly how we can check for collisions on a specific layer to be able to use ECS with physics we'll need few packages first one is entities package second one is collections package third one is the entity's graphics package fourth one is is mathematics package and fifth package which is probably the new one for you is the unity physics package first we'll take a look at how we can add rigid body to some objects and make them work with ECS it is actually really simple in our hierarchy we'll again need to create new subscene where all of the entities are and under this scene we can create any object we want so just right click game object 3D object and I will create just some Cube and spheres so I have just added a cube with a box collider which will be our ground and I have also added few spheres where one will be our player and the other ones will be just some collectible items and to all of these fears I have added a rigid body component now even though this looks like we are not doing anything with the entity component system when we play the game select one of these spheres and go to the inspector set the mode to run time we can see that Unity has automatically added few components for us for example the physics damping physics glider physics Mass velocity and all of these components that we would need to add manually if we would want to create the object through script or we could just instantiate a prefab with the rigid body component already on it I can also try dragging one of the balls up and then just letting them go and you can see they are falling and physics are working as they should be so adding physics to our object in dots is really simple next we'll take a look at how we can actually make the player move so we'll be applying some Force to it and for this I will need to create a ball component ball altering and ball system the ball component is just holding the movement speed of the player the ball altering script is really simple I have already done this many times in my previous dots tutorials so feel free to take a look at them so this will just allow us to set some beginning move speed of the player before we run the game and then it will just create the entity and add the ball components to it in the ball system I'm first getting the entity manager because we'll probably need to use it at some point and I'm also getting the reference to the input component which we have been using in our last tutorial about getting input inity dots this component is just holding some data for the movement Mouse POS position and some booing if you are pressing left most button which we won't really need we are interested just in the movement but if you have your own way in getting input in dots you can do it however you want and don't forget to add using unity. mathematics Collections and physics because we'll be working with some native arrays and also we'll be adding Force to the player then I'm creating native array which contains all of the entities which we can get from The Entity manager and then I'm just going through all of the entities and checking if they have the ball component which is right now just on the player object and if they do have the component we'll need to add some Force to it when we take a look at the player object in unity we can see there are many components but the one that is handling the velocity is just called physics velocity and this is the whole system that will make the player move based on the direction as we are pressing the keys one thing you may notice that I did differently here is that I'm storing the component for the physics velocity in a reference r w so this means that it is a reference and we are not just copying the data into another variable so because it is a reference we don't have to be assigning the component then back to the object and it is ref RW because we want to read and also write to the component so the way to get the rrw is a bit different we are not using entity manager instead we are using the system API and then from the physics velocity component we can get the read and right value from it we can access the linear velocity and just increase it based on the direction that we are pressing the keys so movement. X multiply this by the speed also multiply by Delta time on y we don't want to be changing the velocity so I'm keeping it just on zero and on Z it is the same back in unity just don't forget to put the ball altering script on your player and now as I'm pressing W ASD it is adding the force so even when I let my hands over the keyboard the player is still moving and when the player pushes some of these spheres you can see they are also moving and they will fall down next thing that we will be working on are the rasts so I will make a trigger that will be just detecting if the balls fell from the ground so again we'll need to create three new scripts one will be the trigger component second one the trigger altering and third one the trigger system the trigger component is just holding a size for now we'll be working just with sphere casts but you can use it with any other cast you want and the trigger authoring is again the same thing it is just loading the size and setting it to the entity component in the trigger system I'm again going through all of the the entities and checking if they have the trigger component if they do I will first set their size based on the size that we set in the trigger component so that we can visually see how big the trigger should be and then we will do the ray cast so into this scene I have just added one more sphere which has the trigger altering component and a transparent material on it and how do we change the scale of the trigger based on the value that we set for the size well when we take a look at into the runtime mode we can see there is the local to V component which is holding many values and the way to set this scale is a bit weird for me but the way that you do it is by changing the X in the first line which is making it wider changing the y in the second line which is making it taller and the Z in the third line because if you try changing other components of these float fors such as the Y on the first one it will kind of stretch it which is not what we want so back in the trigger system if you find an entity that has the trigger component on it I'm first getting the trigger transform so getting the local to world and again I'm using the ref RW so that we don't have to be setting the component back to the entity then we can just get the size from the trigger component and just apply it to the transform so trigger transform. value RV because we wants to write through it and then I'm accessing all of these values the c0 is the first line that we had on the component C1 is the F second line and C2 is the third line so I'm setting the values for the scale as I shown you in unity so on the first line it is in the first component second line it is the second component and third line is on the third component and now when I select the trigger and go to the components I can change just its size and you can see that it is scaling it correctly so this will just give us better visual representation of how the trigger should look like to use any kind of recast we first need to get a reference for the physics World Singleton so I'm using system API get Singleton and the type is the physics World Singleton now we can just access it so physics World Dot and here we have all kinds of Ray cast so we can cast some Ray or we can do box cast or we can do sphere cast C we can do all kinds of casts and the way to work with these casts is pretty much the same as in classic Unity so on the physics World Singleton I'm calling the sphere cast all function for which we also need to have a list of all of the hit info so I'm creating native list the type is collider cast hit which is the same as R cast hit in just classic unit C coding I'm calling this hits initializing the list then into this F cast all function we first need to pass in the origin which for now is just the transform position of the trigger so I'm getting the value and we can do just the read only then we need the size so this is just the size that we have on the trigger component divided by two then we have the direction which you can keep just on float. zero we have the maximum distance which I'm having just on one and then we can output all of the hitss so I'm outputting them into the native list and lastly we have some Collision filter on which we'll take a look in a while and then as we have all of the hit info stored in the native list hits we can just go through all of the hits and let's say check if they are holding some component so in this for each Loop we could do it the same way as we are doing in here just check if the hit do entity is containing some component if it is let's say some pickable entity we can just destroy the object and so on but right now it will just destroy all entities that it gets close enough to and as we play the game nothing is really happening until I take the trigger and just move it up into the cube which you can see that then it destroys and it also destroyed some of these spheres that went through it so you can see it working with triggers in unity dots is also really easy and last thing that I will show you how to do is work with flayers so that we can check for the collisions just on some specific layer player so I will select the player and I will set it to the player layer that I have created and I will also select all of these spheres and set them to the collectible layer back in the trigger system when we are working with the sphere cast all function you can see the last parameter is the Collision filter so we can just create new one and we can specify three parameters but we only care about the belongs to and the collides with so the belongs to in this case can be for example some trigger layer the player layer and so on and the collides with layer will be the Collectibles layer because we want to be able to pick up the Collectibles but right now you may not be sure to which value you should set all of these variables so I will just create enam that will be holding them so in the Collision layer enam I'm just specifying all of the layers and their values the way that I'm setting these values may seem a bit weird to you so you may want to check my tutorial about bitwise operators but what this is essentially doing is it is working on the bit level and it is taking a one and sticking it six places to the left so the final bite looks like that now why did I set these Slayers to these values well when we take a look back into Unity we can see that the player layer is on the index of six and the collectible layer is on the index of seven which is the same number that I set it to in this enam and now we can apply these values to the Collision filter so I will set the belongs to to the player layer and the collides with to the collectible layer so it should look just like that and all of the layer indexes have to be unsigned integers and if you don't want to be creating the enam you can just specify the value right when creating the Collision filter back in unity I made the trigger really big and because all of these collectible objects are set on the collectible Slayer we should be able to just throw them down and they should get deleted once they reach the trigger so let's see how it works yep we can see that the ball disappeared just when it hit this trigger and when we try moving the trigger up you can see that it is not deleting the ground it is just deleting the objects that are on the collectible Slayer and you can see that creating a simple physics based game in unity using dots is really simple first we have added rigid bodies to these objects so that they can interact with other objects then we have applied some Force to the player so that he can move and lastly we made a trigger so that when some of these collectible objects fall through it we can let's say at some score to the player I hope that this video was useful if you have any questions or suggestions drop them down to the comments don't forget to like subscribe and I will see you in next videos bye thanks for watching this video till the end if you are looking for a Unity C or bold tutor then I am here for you so feel free to send me a message to my Gmail and take a look at my website for more info I can help you with your personal projects or teach you anything about game development you would want to know you are welcome
Info
Channel: Freedom Coding
Views: 4,452
Rating: undefined out of 5
Keywords: Unity, Unity2D, UnityEngine, GameDevelopement, IndieGameDev, PCGames, Coding, C#, Easy, Tutorial, Education, Unity3D, Programming, Bolt, UnityTurorials
Id: HHeuXgu7It8
Channel Id: undefined
Length: 13min 49sec (829 seconds)
Published: Fri Dec 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.