The ONLY Raycasting Tutorial You'll EVER NEED

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
imagine that you gave your six-year-old baby brother a laser now why on Earth did you give a kid a dangerous weapon we'll get to that this laser is just like any other laser when you fire it a beam comes out okay hold on we need a good name for this laser okay so the difference between any old laser and the Ray Caster is that when the kid fires it and the laser beam comes out it tells us the name of the object that it hit it actually tells us five different things but we'll get to that later now if we gave our baby brother a laser that's not the rayter well things could badly but this raycaster is safe as it doesn't incinerate objects but later on I'm going to teach you guys how to make the ray cter incinerate monkeys on a trampoline for scientific purposes okay okay back to the ray cter your baby brother can shoot the ray cter wherever he wants he could even shoot it at you and one of the things that it tells you is what the object is so if he fires at a tree it will tell us whichever part of the tree that it hit this could be like the leaves on the tree or the stump if it's fired at a wall it will tell us that it hit a wall now important note that our Ray cter does not cause harm to anyone it just tells us information about what it hit the second thing that our raycaster tells us is the position that it hit so if we fire at the noob it will tell us what part of the noob that it hit and it will also tell us the exact position that it touched the noob at for example if the laser from the ray cter the noob's forehead then it will tell us the position of the forehead okay okay now let's say that your baby brother tried to shoot the laser at the strong Noob here but his friend Bob got in the way because he thought that the raycaster was deadly in this case it's not going to give us much information about the noob it's only going to give us information about Bob because our laser didn't touch the noob it touched Bob so this brings us to an important fact that you must remember the laser only cares about the first thing that it hit it does not care that we tried to hit a noob it only cares about the thing that it actually hit it's like how all of us want happiness but life doesn't care that we want happiness our actions however can bring us happiness but we have to do stuff that makes us happy in order to get happiness it's like happiness is a noob our actions are Bob and life is a laser okay enough philosophy there's three more things a ray casting tells us but we'll get to that later because this is where Ray casting gets interesting so why did we give our baby brother the Ray Caster anyway it's because it wasn't a dangerous laser however as Roblox scriptors we can turn it into a dangerous laser and obliterate stuff in our game and I'm going to teach you how to do that but first let's replace the Ray Caster with a point this is where the laser starts and it's called the Ray origin in our case the ray Caster's position was the ray origin that's where the laser started and let's also pretend that the laser is invisible this is what Ray casting really is now let's bring this into Roblox and begin our world Annihilation okay so the first thing that we need is a ray origin remember that this is where the laser starts I'm going to find a laser in the toolbox and we'll work with [Music] it all right so I've also removed everything that's a script because I'm going to be the one scripting it and now we can start scripting so add a script into your laser the first thing that we need is the variable that represents our lasers so I've said that local dangerous laser is equal to script. par. parar which is this thing right here and the next thing that we need is a tool. activated event which basically says say whenever someone tries to use this laser do whatever is between this function and this end and now we can actually rcast so the first thing that we need is the ray origin so what is the ray origin think about where does the ray casting begin I think it's going to begin at this point right here so I've taken an attachment and I'm going to place it right here so this is the ray origin this is where the ray casting will begin and I'm also going to name this Ray origin so I don't forget so I've defined a variable called Ray origin and this is is basically the position of the attachment that we made so this is the position of the ray origin which is also this thing so that's the origin it's the position of the ray cter now we need a ray Direction so ask yourself which direction will we be shooting the laser well the laser should be shot to the place where we click so let's think about this for a second imagine that the player clicks over here and the position is 555 well that's the position but that's not the direction the laser is going to go that 555 is a vector that goes from the origin to 555 the direction however is a vector that goes from the laser to 555 and to find the direction we would take the position and subtract it from the Laser's position that will give us a vector that points from the laser to the destination now we want this laser to be really large I mean do you want a small puny laser in your game no we want a really good laser that has the accuracy of a uh a uh 7.62 time 51 mm NATO 308 Winchester so we'll get the the difference between the mouse position and the rate origin then will make it really really small then will make it really really big and in turn that will make it really really accurate so that's basically saying make the laser go 500 studs away from the rate origin to the place we clicked now I've also set up a remote function to get the mouse position which is this thing right here and I set it up using a local script and it's basically just saying whenever a script tries to do an invoke client then tell that script the position of the mouse so this invoke client line right here it's basically just giving us the mouse position and now all we have to do to fire the laser is call workspace raycast and all this is doing is sending an invisible laser from the ray origin to the direction that we want to fire and remember that Ray casting tells us the object that the laser hit and the position so we can just print those things it's the raycast result. instance and raycast result. position and after you play the game you'll see that once you use the laser it prints the object that was hit as well as the position and I can do it to the haa right here so if I want this wheel I'm going to be really careful I'm going to click it boom I got the wheel I can click on this wheel boom wheel I can click on anything in the haa boom it found an arrow now I can't even see that the arrow is there but because my Ray casting is so good it'll tell me that there's an arrow there and now we know two things about the object that are laser hit now for world domination and monkey incineration we have to get rid of the stuff that a laser hit so if my hit a monkey it's got to get rid of it and thank God for us it's just one line of code raycast result. instance destroy so now if I use my laser I can click on this part it's gone I can click on the wheel it's gone I can click on anything it's gone and there you have it it's a basic laser that uses raycasting we can add the fancy particles later but wait a second wait rayc casting tells us three more things remember it's not just the position and the instance so let's learn about three other things and then we can begin monkey incineration rayc casting also tells us the material of the object that it hit so for example if it hits something that has a grass material then it will tell us well that it hit something with a grass material if it hits something that has a snow material then it'll tell us the thing that it hit had a material made of snow now fun fact if you've ever played a game that makes different footstep sounds depending on what the character is stepping on guess what it uses Ray casting now the way that it works is we would set the ray origin to the foot of the player and we shoot a ray downward since it tells us the material of the object that it hit we know the player stepped on grass sand or snow then we can just play a sound depending on what they've stepped on pretty cool right oh by the way about that invisible laser that we shoot it's actually called a ray hence the name Ray casting okay so the fourth thing that Ray casting tells us is the distance between the ray origin and the position of the thing that it hit you can think about this like how far the invisible laser traveled before it told us something remember when we used a laser and killed a haa if if the haa was really close to us it would give us a small number like 20 that means that the laser only had to travel 20 studs to reach the haa if it's really far away from us however the distance would be a big number like 500 imagine that you're scripting a sniper for example and you want to display head shot from a distance well you'd use the distance to show how far away the head shot was if a player shoots a noob from 400 studs away the recast result will tell us 400 and that's the number that we display on the screen so let's recap we've covered four things that Ray casting tells us they are the object the position of the object the material of the object and the distance between the ray origin and the object the last thing that it tells us is a little bit complicated it's called the normal but you guys know I'll make it real simple so let's look at this Cube it has six different sides right let's look at the front side let's say that the laser hit this front side the normal is a vector that points out of this front side think about the normal as a vector that starts at the face and points away from it so the rast result will give you the normal normal which looks like this if the laser hit the bottom of the cube the normal would look like this remember that's just a vector that starts at the face and points away the normal is 0 1 0 in other words it's a vector that points straight down if it hit it at the top the normal will point to the top which is 0 1 0 the same can be said for all the other sides of this Cube now with that being said we can Define the normal as a vector that points on the surface of the object away from it and the rast result will give you this normal now let's think about a complicated shape like a curve just because lasers sometimes will hit complicated looking things the normal depends on where the laser hits the object if the laser hit this curve over here then the normal will look like this or if it hit this curve here it will look like this now you might be wondering when would I ever use this normal stuff anyway it doesn't seem normal to learn about normals well you might find it interesting that car scripts hoverboard scripts scripts where your character is writing something all of these scripts use these normals when you're riding a car for example and there's a mountain we have to tell the car to rotate so that that it climbs up the mountain to do that we could use a raycast from the bottom of the car to the ground and that will give us the normal of the ground and with that information we can tell that the car is on a hill and has to rotate whenever the terrain changes the car will rotate with it and now we've covered everything that recasting tells us time for monkey Madness now if you've been learning pretty well so far and you want to learn more about scripting in a clear way where each line of code is explain to you then this book is literally made for you the beginner's guide to Roblox scripting this book will teach you BL scripting from the very beginning and by the end you'll know so much you'll be able to create your own admin commands and understand all of the code in it if you're interested there's a link in the description of this video or you can go on Twitter and look for my pinned post if you buy the physical copy and send me a picture on Twitter within 6 months of this video being uploaded I will give you a surprise let's add a highlight effect so that instead of the object disappearing it'll have a cool effect to [Music] it okay so what we're doing here is we are taking the Highlight we're putting inside the script and then we're cloning it and putting it inside the instance remember it's the rcast result. instance and then after 1 second we're destroying the instance so let's see what this looks like in Roo okay so I have my laser and now if I click on something it gets highlighted boom boom and destroyed it's being incinerated one by one I can incinerate anything that I want like this part right here bam it's gone bam it's gone bam oh bam okay that's gone bam come on there we go it's gone and now what I've done is I've given it a cool laser like effect so what this is going to do is it's going to animate the incineration so now let's play and let's see what happens so I've got my laser and now if I click on the spawn location it's gone I can click on anything I click on this part with our Superior accuracy we can get it boom it's it's gone boom it's gone boom it's gone and okay whatever that thing was is gone okay great now for monkeys on a trampoline here is a trampoline and here is a monkey now we want these monkeys to jump and since this is a tutorial on recasting let's use it so what does a trampoline do when you jump on it well it makes you go up so we're going to recast from the monkey down to the trampoline and then we'll get the normal and then what we're going to do is we're going to use that normal to push the monkey so once again what is the ray origin where does our Ray begin so the ray origin is going to be the position of the monkey because we're going to be shooting a ray downward and the ray direction is going to be Vector 3. new 0 -30 which is just a ray that points downward three studs and now every 200 milliseconds we're firing a ray cast from The Ray origin which is the monkey's position and we're firing downward now remember that the normal is a vector that goes from the surface of an object and points away from it so it should be clear the normal is going to be upward because the ray is going to going to go from the monkey it's going to go downward it's going to hit the trampoline surface and then that's going to be pointing away so then all we have to do is just apply a big impulse in the direction of the normal and you can do this using apply impulse all we're doing is giving the monkey a quick push upward so all we're doing here is we're checking if the rcast actually hit something and then we're taking the normal and applying an Impulse in that direction to the monkey now the normal Vector is going to be pointing upward which is 0a 1 comma 0 now what happens if an ant tries to shove you not much right so this impulse isn't going to do much to the monkey because it's so small so what I'm going to do is I'm going to take this normal I'm going to multiply it by 350 and now if we go to our game and we run it we can see that the monkey is now successfully B bouncing on the oh okay it's gone okay it's still bouncing okay buddy okay so obviously every object made the monkey bounce and that's not how trampolines work so what I'm going to do is I'm going to rename this to the trampoline floor and the monkeyy is only going to bounce if it touches the trampoline floor so if that laser that raycast if that Ray touches this trampoline floor then the monkey will get sent upward if it doesn't then nothing will happen and that's basically what I've said here if the instance that the raycast hits is actually a trampoline floor then apply the impulse okay so now there we go that is how a trampoline works now if I want to make this monkey bounce higher I'm just going to make this number a little bit bigger let's say 950 and now the monkey should be sent flying bam wo that's awesome okay so now you have an in-game monkey that hacked its life with infinite stamina and will jump on this trampoline forever its legs will never get tired unless it does not touch this part so if it doesn't touch this part then it'll stop but if it's as long as it's touching this part it's going to keep on jumping now take your monkey and multiply it by five so just duplicate it five times so contrl D contrl D contrl D contrl D contrl D and now you have five monkeys jumping on a trampoline they're flying out oh my goodness okay so I've taken the trampoline and I've made it really really big so now if we play our game monkeys on a trampoline here we are monkeys are jumping on a trampoline literally now I have my laser and now I'm going to try to shoot them down come on I got this oh no oh no now they're no longer jumping I got them I got them got cheated fine all right guys so I've made a couple of changes the first change is I've changed position to World position so this does is it actually gets the actual position of the attachment and the second thing that I've done is I've added a bunch of and statements to this if statement right here so what we're doing here is we're checking if the rcast result exists and if the instance's name is not equal to the trampoline floor so the reason we do this is so that we don't accidentally incinerate the trampoline floor and this line makes it so that our laser does not actually hit the laser itself so we're making sure that the raycast doesn't actually hit this laser and accidentally incinerate the weapon that we're using and this line right here is we're making sure that the laser does not hit our character and then finally we're making sure that we can't incinerate the base plate which is this thing right here now this may seem a little bit complicated but later on in the video I'm going to teach you how to hack this raycaster and give it some crazy Godlike Powers so that we don't have to use these if statements and now that I didn't cheat now let's see if I can get a monkey or two so let me you you you yes I got him woo and this requires extreme concentration oh I just inated the rim of the trampoline this should be like an Olympic sport come on why is this so difficult okay now the trampoline rings are gone so this thing's going to okay yep got you now and we got a couple more Bad Monkey there we go and let me get that one okay this is really really difficult come on come on come on okay well he's basically come on come on laser okay yeah so so in this scenario remember what we talked about in the beginning of the video so the laser here it's trying to hit this monkey but this thing is in the way and remember what we learned it's going to touch this and it's not even going to look at this monkey so in order to fix that we have to actually get to a position where the raycaster boom can touch the monkey now I'm going to teach you something really crazy it's almost like hacking the laws of life but we're game developers so it's like hacking the laws of the game but it's our game so we can do whatever we want so haha what if I told you that we can make our laser really smart for example if our friend Bob got in the way of the laser currently our laser will incinerate Bob but we don't want to incinerate Bob he didn't do anything to us well we can literally tell our laser do not hit bob and since we coded it it's going to be forced to comply with our crazy demands to do this we use something called raycast pams which is short for raycast parameters and this is just how we want the raycast to behave like for example do we want it to hit water do we want it to hit only specific objects do we want it to not hit specific objects to create some of these raycast prams you can type raycast pr. new and now we need to tell the raycast that we want to exclude stuff so this is stuff the laser won't even know exists in the world it's just going to go right through it and to do that all you have to do is type this line rcast pam. filtertype equals en num. rcast filter type. exclude and this is going to exclude things from the laser all right so now if you remember from before we have this gigantic if statement which is basically saying if the raycast does not hit a trampoline floor and the raycast doesn't hit something that's a part of the laser and the raycast doesn't hit something that's a part of the player and the raycast does not hit a base plate then actually do the laser stuff so this works and all but what we're doing is we still have to shoot the ray cast using this line and then we check everything but instead of doing that what this raycast perams thing allows us to do is it allows us to check these things while the laser is being shot so it can actually go through things it can actually ignore that things exist and go right through them and continue with the raycast so if there was a new behind Bob and we put Bob inside of this raycast prams thing then the laser will go right through Bob and it will just get the noob so that's why this is really powerful so now we've told the laser that we want to exclude stuff but now we have to tell it exactly what to exclude so we're going to do is we're going to say raycast params filter descendants instances equals and then we're going to set that to an empty array and then inside of that array we're just going to put all the objects that we don't want the raycast to include so remember that we checked to make sure it's not the trampoline floor so I'm going to put that trampoline floor which is workspace SCP trampoline. trampoline floor whoops trampoline floor so now it's going to exclude that so it's not even going to know that this thing exists then we're going to say workspace player. name and this right here is our character which is this thing right right here and then we need to say the laser and the last thing we need is the base plate so we're going to say worksspace dobas plate and now we don't even need this line we don't even need this end and we can fix up our indentation we can get rid of these spaces so once again all we're doing is we're saying we want to exclude these things and if the ray cast stumbles across any of these things then it's just going to go right through them and now we have to actually take this raycast params and bind it to the raycast result so to do that you just take this raycast result line put it right there and then we're just going to make a comma raycast params so now we've told the ray cast three things we've told it the ray origin we've told it which direction we want it to Fire and we've also told it the raycast params which happens to have the exclusion details so these are the things that we are excluding from the raycast and if we play our game you can see that it's just going to function as normal if I click on this nothing's going to happen because what happened was the raycast went right through it and it's not going to touch it but if I use it on like a monkey still works see if I can get this yes I got it woo that was nice so this is a water balloon catapult and this is not in the list of things that we're ignoring so if I click on it it'll get incinerated one by one and wh would you look at that the physics just made it go down get rid of this wheel that's cool get rid of the balloon so because this object right here is not in this list of things that we're excluding it's just going to allow the ray cast to go through but I cannot see I cannot destroy the base plate no matter what because that thing is inside of the things that we are excluding all right so we've learned how to exclude stuff now we're going to flip the script a little bit and instead of excluding these things what if I want to include these things so we're going to say include and what this is going to do is it's basically going to flip what we just did so instead of excluding these things it's going to only look at these things so anything that's not on this list will not be a part of the laser so that's not going to be a part of what the laser can see and so now if I try to use the laser it's probably going to break because now we have the laser we have the player included and we have the trampoline floor and the Bas plate so the laser is probably going to fry itself R click on the laser okay as you can see the yeah okay so once again changing exclude to include makes it so the laser only looks at these things and it only takes care of these things so if the laser hits any of these things then it will get fried and if it's none of these things then the laser won't do anything and here's another example it's not going to touch that because that's not a part of our list of things that we're including but if I use it on like the laser there it's gone okay and now here is some extra bonus material it is called shape casting and it's relatively new so imagine that instead of a thin laser you have a really wide missile oh don't worry we'll spare the monkeys for this one if we try to use Ray casting on this thing it's going to shoot a ray which is really thin but for a thick missile it's not going to be too accurate if we use a thin Ray and that's where shape casting comes into play instead of a thin rate with a shape cast we can push a 3D shape like a block or a sphere along a line and see if that object touches something so for a missile we could use a sphere and push it in a Direction so instead of using workspace raycast we can use workspace Fear cast and workspace blockcast it gives us the same thing that a raycast result will tell us the object position material distance and normal the setup on sphar cast is a little bit different and I might have a video on this in the future but for now if you want to read more about them I'll link an article in the description and there you have it if you enjoy this tutorial make sure you subscribe and hit the Bell icon so that you don't end up like the monkey boom
Info
Channel: script_ing
Views: 9,195
Rating: undefined out of 5
Keywords: roblox raycasting tutorial
Id: YipkISTwGlc
Channel Id: undefined
Length: 23min 54sec (1434 seconds)
Published: Wed Jan 03 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.