Raycast Unity 3d - unity raycast tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you ever wondered how the bullets from your gun precisely hits the target or how this giant spider walks right above these rocks just like he can see his environment the magical spell developers put in this kind of situation is raycast so what is a raycast it's a ray that gets sent out from a point in 3d or 2d space and moves in a specific direction in unity we use physics.raycast which returns a boolean value it's true if it hits something and falls if it doesn't with that said let's dive into some implementation stuff all right here in the project i have a capsule character holding a gun and trying to save his friends from this giant cube monster what we're gonna do is we're going to implement a system so that capsule character only shoots to the cube monster and never puts a bullet inside one of his friend's head of course we're doing this using raycast so to do that make a c subscript and open it in visual studio delete the start function we don't need that right now while looking in the unity documentation you can see that physics.raycast takes six different parameters each one is perfectly described in here so let's see them in action for that type physics dot raycast and inside bracket we're giving our first parameter which is the position where our ray should start i'm typing transform.position here because i want the restart from the same game object i'm gonna attach this script to next parameter is direction i'm giving vector 3 dot forward here which will go in the direction where the set point is pointed okay we have given the start point and the direction next we need a variable to store data if the ray hits something so type out raycast hit i'm calling it hit info next variable is how much distance the ray should travel it's a float variable i'm typing 20 here there are two more parameters which we will be discussing later in this video so now this will return a bowl value so i am putting this in an if statement and typing hit something in debug and in else i am typing hit nothing save that get back to unity and attach this script to the game object where our gun's barrel is pointed at and hit play i have a small character movement script in the capsule by the way and of course i have added colitis to everyone because raycast need colliders to detect collisions as you can see when i hit play the console says hit something when there is someone in front of the capsule and says hit nothing when there is no one in front cool now let's draw a ray so that we can see what's happening in the scene for that type debug.drawray in the bracket initial position direction and length of the ray i am calculating the distance traveled by ray by typing hidden 4 dot distance and multiplying it with direction i am giving it red if the ray hits something and green if it doesn't save that get back to unity and click play there you can see when the player is in front of any collider then the red color turns to red and when it doesn't hit any collider then red color is green we can customize which objects should the ray hit in the scene by putting them in a layer and applying a layer mask for that make a serializable field of type layer mask i'm calling it layer mask and add this in the ray cast save that make a new layer i'm calling it cube monster and put this cube monster in that layer assign the layer in the script and hit play now the day only turns red when it hit the monster we have one more parameter left in raycast that is corey trigger interaction it's for deciding whether the ray should hit the is trigger enabled objects it has three settings we usually use it as use global which means it uses the default settings to find the default settings go to edit project settings under physics section you can see query hit triggers two other settings are ignore and collide ignore will ignore every trigger enable game objects and collide will include all trigger game objects in its radar for example i am typing ignore here and in the project when i enable east to go the ray is not turning red and the moment i disable each trigger it tends to red alright in the script you can directly give initial position and direction to array object and pass this ray object to the right cast and also you can initialize hidden four before the ray cast make sure you are making ray inside the update method i made some particles to add an extra effect to the shooting of capsule character now the cube monster can spit green blood and die that's it there's your right ass implementation in unity you can always download this project by supporting me on patreon and if you want to join the pixelbag community then link to that will be in the description below as always like comment share and subscribe if you haven't already and i will see you in my next video [Music]
Info
Channel: Pixelbug Studio
Views: 42,577
Rating: undefined out of 5
Keywords: raycastunity, raycastunitytutorial, raycastunity3d, raycast, unity, tutorial, gamedevelopment, unity3d
Id: cUf7FnNqv7U
Channel Id: undefined
Length: 5min 39sec (339 seconds)
Published: Thu Sep 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.