Advanced Roblox Scripting Tutorial #24 - Raycasting (Beginner to Pro 2019)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to a brand-new roblox triplet Oriole and in today's video I'm gonna be teaching you about Ray casting now before I explain anymore let me go ahead and show you a demonstration of what ray casting can do so let me go and hit run and you guys can watch this so as you can see here I'm gonna have a turret and he's actually shooting this guy named Kirk but if I put the wall in between them as you can see the turret will stop shooting because they know it knows there's a wall there and I can move the wall it'll keep shooting again and I can move to Kirk and actually it stops shooting though because Kirk is now dead but as you can see it's very cool because it's like an automatic turret and you know it can it can rotate it can do this it can do that and yeah it's awesome anyways guys that's what learning into this video also start from a bit quiet I am it is kind of late right now so yeah okay so how do you do this you may ask well we're about to find out so first things first go ahead and get a new baseplate open and actually before I go ahead and go into depth and show you exactly what like that is right before I show you the auto turn and how to make that it's it's pretty simple to make but I want to make sure you understand what recasting is first because there's honestly not much recasting used in that but there is some so first let me show you what recasting is okay so first things first what is ray casting so basically what ray casting is is it allows you to detect whether a part is intersecting a line segment now what that means is if you guys don't know what a ray is like a ray and geometry if you ever taken geometry or for it's basically like oh it's a long line that's infinite in one direction so it'll go on forever in one direction but it's fin it in the other so it only likes start here and then it'll go all the way over here forever and ever and ever now actually roblox Ray's arm like that they kind of just stop at a certain point but yeah but you can't make them infinite if you want to but okay so what I'm gonna do right now is show you're gonna make a ray and basically how it works okay so I'm gonna go ahead make a part right here and I'm gonna make this a little bit bigger so like a square I like squares and then I'm going to go out here into the surface inputs right on the surface or no service yeah service and I want to change this little top service right here I want to change this to a hint and now what that does basically hinders you can use like make cars and roadblocks but we're not gonna be doing that the for this purpose I'm using a hand adjust to show you which way the top surface is so it's that way but yeah and then I'm gonna go ahead and actually I'll leave it like that but I'm gonna put a script inside of it and then I'm going to start writing some code so yeah I put a script in there now I'm gonna go ahead and do local part is equal to the script parent alright so what I'm gonna do right now is I'm gonna make go ahead and make a ray okay and then I'm going to cast the Ray every second and see if there's a part intersecting it okay so just a simple demonstration okay so I'm gonna go ahead and do local ray is equal to or I shall do I'll look I'll do local my first ray cuz I don't like naming stuff just ray because it's bad practicing if I name it it's ray you see it's a key word although you could just use the lowercase R it's not like the key word anymore but still it's just I don't like doing that I'd rather have like a certain name for it so I'll do local my first array is equal to Ray dot new okay and basically you do radon new right and you can see here it says creates a new ray with the give an origin and direction okay so where do you want it to start that's the origin and then where do you want it to go okay so where do you want it to go yeah so we'll do radon new and then we're gonna do the origin so where do you want it to start well we wanted to start right here this part member we wanted to just shoot up like an infinite array so we'll do part or yet we'll do part dot position that's what we wanted to start consider back to three value for the way and then we'll do where you want it to go we want it to just go up so we'll do vector three new and then we'll do comma 50 comma zero comma or 0 15 zero basically just 50 in the y-axis right we wanted to go up okay so and now it's a rate right it exists but we haven't casted this Ray so it exists in the in like the memory but you actually can't use it yet so what we need to do is we need to cast array now we can just do my first ray or no pool actually but wait we don't we don't want to just cast it one time I want to keep casting it over and over so I'm gonna put it in a while loop okay to do while weights 1 do basically I'm just every second this would loops gonna run and then I'm gonna do my first ray press yeah I'm interview yes I'm gonna do local hits comma position then it's may look a little confusing don't worry I did local hit common position basically what this means some sing okay so the part it hits okay and then also the position of where it hits okay it's gonna be these two values whenever you use this function okay so I know it me you may have never defined two variables in one line but basically it's basically returning two values this function about to do and the values are what part it hit and then the position of where it hit right so yeah okay so we're gonna do local hit position and then we're gonna do game now works-based dot find part on ray okay so as you can see here you have to you have to cast in the workspace right but you see it's on fine part on ray and then you have ignore lists so there's you can do it like so you could okay so you can you can ray cast with a with an ignore list basically which means there's a it'll cast array but you can put in a table of what you want it to ignore and then you can do a relay with white list and then you can do just fine part on array with with only one instance that you want it to ignore so we're going to do for this example we'll do an or list because that's just a probably the best one to do I'd say and then we'll go ahead and type in the values and actually mucho and show you where those values are so if I could go ahead and type in this in again easy there um it's might be a little hard to read but it says the to settle the properties for it right the proper number one is the array okay so what ray do you want to do what ready you want to cast right and then you wanted and then in this second property is the ignore list what parts do you wanted to ignore which makes sense and then the third property is terrain cells as cubes that's basically has to do with the rain and then the fourth property is do you want to ignore water do you want the you want that rage need more water so yeah we'll only type in the first two properties or its you first two parameters but because we're not going to be worrying about the second second two and those by default will have a default value so you don't need to actually assign them if you don't want to so yeah okay so we're gonna go ahead and type that out to return to do game now workspace find part on trade with the in or list and then we're gonna go ahead and do ray or my first rake that's right we want to cast and then we don't really want to ignore anything much but we do want to ignore the actual part which is casting from right here so we'll just type in in in these little brackets right here because that's how you make a table because the value insert is a table of list or a list that you want to ignore so we're gonna put in brackets just apart and then part should be eco script out parent and the script you be inside the part right so it ignores the part which makes sense so then we're gonna do if hit so basically we're gonna do if hit so if it returned right if it's if the Ray was casted and it hit apart well then we want to do if hit then prints part was will do yeah yeah well do part was hits okay or actually weld yet will say Ray was hit by another part or what we really--we should do is like Ray intersected another part I guess so yeah okay so we're going to someone type that and then we're going to go ahead and run this and then we're gonna do else we're gonna do and we're going to do prints no part found okay so we're gonna test that out right now and see if this works and basically what it's gonna do is I'm gonna make a part and I'm gonna move it around and show you okay so right now it's printing no part fountain because it's cached in the Ray it's going all the way up just straight into this guy and there's no part there but now let me go and anchor that too I'm gonna go ahead and grab the move tool I mean a new part I'm gonna go ahead and move it up and then I'll move it right here and as you can see it says great intersected another part and if you wanted to you could print out the part it did or you can delete it you can do ever you want right basically it feels like I get a gun or something like that you could do that but as you can see I moved it so high up it basically what just happened was okay I'm gonna okay okay look so right now it says Ray intersects another part right I moved it so high up that it doesn't actually it didn't actually this is no part found our you it's above it right but that's because we only cast it at fifty studs up okay and this is past fifty studs so it's really not infinite and then in one direction it certainly not infant in one direction but it could be if it wants to so yeah so it's basically it's sort of just like a big line segment that's what a Rea is right so yeah and yeah if I wanted to like I said I could I could do if hit then hit destroy right so basically if the Ray was casted and it found something well then destroy that thing right that can be kind of cool right so okay right there and boom see you destroyed that's kind of cool but yeah anyways that's how that works okay so that's basically what recasting is and how it works and that's pretty much all you need to know actually but I want to make it a little bit more fun I want to actually make it turrets that's what we doing in today's video so yeah okay anyways let's go and make a turn now so I'm gonna go and delete this stuff and if you want to say it but you can of course but I'm gonna delete it okay so now we have a bank another blank baseplate okay so I'm gonna go ahead and just grab my my character that I had basically it's just an art in our six character with a torso and a humanoid which is the most important thing that's all I really need for this character and yeah so I'm just you need this horse to know a torso and a humanoid so if you want to get that just go into your tool box right here and you can just look up like NPC and then just answer any NPC pretty much and also I recommend deleting all this like other stuff like just delete delete all this weird stuff and all that and then there you go you can use it so yeah also delete any scripts that are in there too and there you go that's tragic receipt so I shall use this one just because okay there you go so now they had this guy what I'm gonna do now is I'm gonna go ahead make a script and I'm going to show you I'm gonna make a turn first so if you saw on the thumb that I use this turret that looked pretty cool now you could use this turn if you wanted to but it's gonna be kind of difficult or rotate the top part of it because I didn't make it obviously so yeah I'm not gonna even bother with it I'm just gonna insert a little basic it turn it doesn't look very pretty I know I made it myself it's basically just one part at the bottom right and then there's one part up top - it's a little barrel and then I inserted the script in there but you gotta leave that for now but yeah that's the turret and then I just made it a model right I could put like more stuff at the base if I wanted to make it pretty obviously but I I'm not a builder scripted right so yeah okay that's my turn it's pretty it's very pretty okay so what I'm gonna do now is I'm gonna go ahead and just start the recasting okay I'm gonna start the script okay so I'm gonna go ahead and insert a script into my barrel so if you make your turn make sure the top part name it barrel actually you can in whatever you want I need a barrel but I put a hinge on it too so that doesn't nothing remember keep in mind that it's nothing it just looks like nice and also anchor - let me show that's anchored and there you go okay so yeah I'm gonna put a script inside of my barrel so I'm gonna go ahead and insert a script in there and here we go okay so first things first we need to we need to get a bullet to shoot right that makes sense we actually get a bullet for the gun to shoot so I should I'm gonna go ahead and make a bull real quick I'll make a little small tiny little bullet just like that there we go I'll make it anchored actually nope don't make a naked sorry government do not make an anchored make it black I'm gonna get black oh I'll turn can collide off and then that should be good alright and now I'm gonna rename this one - bullet - that's pretty important you know bullet and I'm gonna move that in rep storage okay so basically I moved it her I moved it into my replicated storage which basically chose like a storage so I can episo-- I can clone it and then insert it and then make it shoot out of my gun okay that's what I'm gonna do with this guy so first things first we probably get the bullet then so I'll go I'd be real local replicated storage let's quit a Gamecock cat service rep storage and I want to do a local bullet or local bullet as equal to your rep storage wait for child bullet I'm getting a little bit basically all I'm doing right here and I'm gonna go get the turns well I'm gonna do local turret it's equal to strip parent now I know the turn itself actually is an eco script out parent it's equal to the script parent chip parent parent but I don't want the model I just want the actual base or I want the I want the burial so I'll just keep that turn to make it look nicer okay and then we're gonna actually define a couple more variables - like how fast you wanted to shoot so like every every how many seconds I'll do every like I'll say every like half seconds every 0.5 seconds and then I'll do the damage how much damage you wanted to do I'll say it'll do 10 damage or something like that out of 100 right and then we'll do the speed of the bullet so how fast you want to go I'd say I'm maybe 150 I can always change values later and then I'll do a local agro distance okay so basically what that means is how how close does a turn have to be for it to actually shoot it to person right those are certain distance that that's what it's saying right so we'll do a hundred 100 studs okay so now we're gonna do we're gonna go ahead and make a while loop to basically loop through a function where I try to find the player and then shoot them every time or every 0.5 seconds which is the fire rate so we're gonna do wild weights fire rate a that rounds I'm a rapper now do and then we're gonna do now is very first of all we're gonna find the target okay and then we're going to detect if it's realistic to shoot now what does that mean basically I'm saying I want to find the target right and then I want to detect it was realistic to shoot so if there's a wall in between the two targets or if there's a wall in between the turn the target it's not realistic to shoot right you don't you normally wouldn't know that so that's not realistic so basically I'm gonna make sure there's there's a wall between it will not shoot what that means okay so now we're gonna go ahead and first of all loop through the entire workspace to see if we can find any targets okay so also I'm gonna make it variable up here called local target I wanna make it I'm gonna make it equal to nil so just for later when we set the target you go to something we'll have it up here so yeah okay it's a local target equal to nil and I'm gonna do for IV in pairs game workspace not get children do so I'm looping through everything the workspace and I'm gonna set the human equal to V dot fine first child's humanoid okay so let's say it's finds the NPC so that'll do it'll do MP sir NBC that find first child humanoid and then it'll do torso movie I find first child torso okay and then we want to make sure they exist because right now anything it could look there anything right I can loop the turret and look for it but obviously in my I actually have it so we'll do if human and torso and also we're going to do and if like or will you and human dots and human dot health that's greater than zero right so this we don't want it to shoot at the human when it's dead so we'll do and human health is greater than zero so if it's not dead right and then we'll do we'll do that see it now basically we know we found a target now we want to check if it's close enough right so we'll do if torso dot position so it's a position of the torso - the turret position dot magnitude which basically converts it into studs right it is greater than a grow distance or less than tiger distance then okay so basically it's basically said is that saying okay is the torso okay is the turret less than a hundred shots away from the torso or from the player okay that's what it's saying also if you don't know what magnitude is I made a tutorial on that definitely go watch that it'll clear that up for you so yeah okay next we're gonna go ahead and do actually no we'll skip this for now all right so now we're gonna do okay so if we found it so that's good right so then we found the target right we found we found target so you have to target right here we found them and make sure they were human and stuff we made sure they have more than zero health and we checked to see if they were close enough so then we'll do target is equal to torso right that's that simple and then we're gonna go ahead and go in here and now that we have target so we'll come down here we'll do if target so that way it doesn't run when there's no target so we'll do if target then local tour so actually they make a little confusing local new torso is equal to actually okay I'm gonna I wanna make it simple all right so basically I'm gonna set torso equal to it you could put a different value okay so I already have it up here which I could just keep it but I'm gonna not use this one I'm gonna make a new tour so I'm gonna set it equal to target right I could just do target down here or which which is the same thing but I'm going to make it I'm gonna make a local tour so equal to targets and now I have the torso right here okay that's what I'm doing so yeah okay I'm gonna do turn it dot C frame you going to see if a new turret position and then torso top position now what does this do I'm not sure if actually explain this in my seeded material also we don't know what C frame is to go walk my tutorial in that mates were on that too but basically I'm not sure if I spied in this part but basically when there's two values in a C frame about new it basically changes the front surface of a part to face the second value right so basically what's gonna do it's gonna make this front surface so which is which has the hinge coming out of it because I made it like that right I made the front sort of an inch it makes it face the target which is this guy right here so that's what that's what that line of code does right there so yeah so turn the turret to face its target okay and then we're gonna do and then we gonna make me didn't they get the bullet right when you get the ball to shoot so we're gonna do local turret bullet actually will you local a new bullet is equal to bullet clone because we don't want to just take the bullet right from rep storage because then it won't have a bullet anymore so we're gonna clone the bullet into a new thing and then we're gonna make the new bullet position go into the turret right we're gonna make the turn we want to make a shoot from the turret right that makes sense so the turret local bullet position is equal to turret position okay and and then we're gonna do turn it our new bullet dot parents is equal to game network space we're gonna make sure it's in the workspace we can actually shoot alright and now now it's in the workspace it actually doesn't miss not moving yet right so we need to make it actually shoot now here's where velocity comes in now I'm not sure if I'm sure if you guys have ever used velocity before I've actually never made it so on it or spoke about it before in my videos so basically what velocity is if you guys don't know it's a vector3 value but basically what it does is it makes a part move or it makes an yeah it make something move alright so if I were to set the velocity of just a part right here right to 100 I'm not sure if it's actually work because there might be a little bit too much friction but I said the velocity okay it went up in the sky rikes I sent the velocity on the y-axis to why didn't anything kids by the way I just saw that guy fall I'm sorry with that okay alright so yeah um I said the velocity to this to 100 and Y axis originally what that just happened was it flew in the air and then it came back down because the gravity right and now the velocity at zero again but if I change it if I change it on the x-axis to 100 it'll fly right there all the way that way and eventually it stopped it slows down now why is that you may ask well that's because of friction okay roblox is built in roblox is built an engine has friction all right that makes sense okay and there we go so yeah okay all right also I gotta arrow down here that air basically I actually spelled something wrong on accident so I did Ming it to I must do magnitude there we go but yeah that's what if that's what a vector he does so we need to make sure our bullet has some velocity so it actually shoots right makes sense so uh okay so we'll go ahead and do new bullet dot velocity as you go to vector three wait no no you can't do that right you need to actually you don't know where it needs to shoot need to find the direction you to shoot so how you find in the direction it needs to shoot is we're gonna look at the front of the surface so which ways its facing right because it'll make a turn we're gonna make a turn to the character the torso right so we're gonna do new bullet the velocity is equal to turret dot C frame okay so let's see from the turrent dot look vector which basically is saying the forward Direction component of the yet deceiver right so basically you're getting the forward direction okay so it's gonna shoot forward or from the front surface forward okay and since the the turns gonna lock on it's gonna shoot right to the right to the torso right makes sense now also if you saw earlier though the bullet the bullet slowed down after a while and in the air what that's gonna look like it's gonna look like bullet drop right there but what's gonna fall down so if your bullet speed isn't fast enough your bullet speed your bullets might not actually reach your turret or your might actually reach your target so that's something to keep in mind as well anyways alright so now we have that now we're pretty much done now but now we need to make the bullet actually do damage so we're gonna do if or not if we're gonna do new bullet dot touched connect function part hit or I'll do object hit that's the parameter and I'm sure I'm sure you've used the touched of it before if you have it then you need to go out to my beginner tutorial series but yeah so we're gonna do new below that touched do local human it's equal to part hits or object hit parent fine first child humanoid right we're gonna we're gonna basically it's gonna the the bullets gonna hit apart it's gonna look at the parent and that's going to look for the fit the humanoid okay and then we're gonna do if human so if it actually did hit a human then we're gonna do human take damage bullet damage all right there we go that's pretty much it oh also um this is not finished so okay I got the velocity right the velocity or the look vector and we got the luke fight right here I forgot I forgot something here the velocity of the bullet ray we got the look vector but that's not saying the speed we wanted to go that's just where we wanted to go right so how fast you wanted to go well we wanted to go as fast as W set the bullet speed which is 150 so we're gonna do times bullet speed and basically that's how that's saying how fast you want it to go okay so there we go now we've used every single variable up here so yeah okay that's good alright sweet so now we're gonna do is we're gonna go ahead and a run this and see if it works run and oh okay look at that so what's happening right now it's it's actually working don't get me wrong it's working but what's happening is the like I said the bullet speed isn't fast enough so the bullet drops because of gravity right so the bullet drops because gravity pulls all things down toward the center of the earth and actually barely escapes the bottom of the feet doesn't hit it yet and since it has can collide off it's obviously going through the ground so that makes sense right but it's not close enough to the target so what we need to do either is move the target closer or we need to make the bullet speed faster so it goes farther so we're gonna go ahead and just move them closer to for the demonstration as you can see there he is taking damage and he's dying now you may be wondering also like why is dying so fast because if the bullet speed was 10 and he has a hundred health it should take ten hits now is obviously not ten hits and the reason why if you guys don't know that that why I literally like five hits or something like that and that's because basically when the bullet actually hits the target it actually can hit a multiple part so it can hit the it can hit the like the blue the blue body but they can also like go through the arm as well and that will count as two hits even though it's just one bullet so obviously if you wanted to fix that you'd actually go through and just do if it's hit then you know do that that and obviously that's a lot more complex tedious code which you guys probably figure out on your own so I'm not gonna slow down today's video but uhm yeah okay so that's pretty much it right that it works I mean it works but there's one thing we're missing if you guys haven't realized it yet we haven't even used recasting we haven't even use recasting at all and now what that means right you're like well okay who cares who need raycasting when it actually works already right well here's the problem okay we don't have a very casting right but you know we know what what the problem is okay you want to see watch this okay just just watch okay you see and look all right it's shooting at the wall now it's actually going through the wall because Kenny collides off but as you can see it's shooting through the wall like that's not okay right we hope you don't want it to shoot through walls right so what we need to do is actually cast array and detect to see if the player is casting on that Ray so we're gonna go up here again because we actually didn't we actually didn't detect if it's realistic to shoot up here we didn't we didn't hide that code in yet and I did that on purpose I don't want to confuse you guys so yeah okay so now we're gonna go up here again we're gonna go ahead and go in this little area before we before we set the target we're gonna do um local bullet ray so we're gonna make it right for the bullet local blu-rays you go to Ray not new then we're gonna do turret dot position and then yeah that's where we wanted to start we want to start it right and then we want it to do this is a little bit listen this is gonna look a little bit complex don't worry looking up next to me at first - so then we're gonna put a parenthesis and we're gonna do torso dot position - turret dot position now basically what this means and then dot a unit and then also times the aggro distance okay now this don't worry if was to look fusing so far I know and may but what this means basically is it's saying okay so the word wherever you want the Ray to start we want already to start at the turret position okay it makes sense that's where we wanted to start now where do you want it to go okay we want it to go at the torso right we want it to go right to the torso so what we're doing is rating the torso position and we're subtracting it from the turn position so it's going like - this - this okay and then dot unit which is like basically how you get the unit for it I know it's a little bit weird I don't know how to explain it honestly there's no good way to explain it but basically just know that this formula is gonna give you the direction you want it to go right and also how yeah that's giving you the direction okay that's what this highlighted section does now what this does is the x over distance this gives you the distance how far you wanted to go okay so we wanted to we wanted to get ready to cast a hundred studs one way so then you know you know the I'm saying if you wanted to cast honors head one way that's how far you want the ready to travel okay now obviously you can make it longer if you want doesn't matter because we're already gonna have the Eiger justice checkup here but I'll make it like five hundred to make it nicer and yeah okay okay so now we have that we're gonna go ahead and do local hit comma position right we want to cast array so we're gonna do local hit composition is equal to game that workspace find part on ray with ignore list and then we're gonna do bullet ray optics that's the Raby wanted that we were you want that's already when I cast and we wanted to ignore the turret you know we don't want to see a you know go to the turn and then obviously you may think well you wanted to ignore the wall to or already wanted to ignore the player or whatever but no don't worry we're gonna put one about a year which is just a turret which is itself right words shooting out of okay now that we have that we're gonna do if hit so if it hits something right which it's gonna it's gonna hit the wall right when they could when the Ray is casted and there's a wall in front of it it's it's gonna hit the wall obviously right even if it's facing or even if it's looking at here it because the radius cast are there it's gonna hit the wall right so if the hit because it's gonna hit if hit is equal to torso okay so if there's no wall there then we just gonna hit the torso right is gonna get in alright to the torso so there's no wall there it's anyway the torso so if hit is equal to torso then the targeting we get to a torso so basically don't sit the target unless it hit the unless the Ray hit the target or hit the torso right so then we'll do else so then else then prints prints object in no way okay so now this should work and now it should not shoot unless the art should not shoot if the wall is in the way so we're going to test it out and see if it works and here we go so we're gonna hit run now as you can see it's not shooting it's printing out right there there's object in the way and with that there is object the way and we'll move the wall and then they can see there it's not pertain anything and that's we didn't we didn't set it to print anything there but as you can see it is shooting right shoots when this is what it's not there and stopped because a player died but as you can see whenever the player is there and there's no wall there it will shoot now this has been the longest video I've ever made in my life so I'm gonna end the video now but yeah that is very consoling guys I hope you did enjoy the video seriously guys I would hope we're working down pretty well honestly I feel like this could be like a very good tutorial for a casting I feel like I made it pretty simple so I hope it I hope it was something for you guys honest I really do and yeah leave a like if it helped you out guys please leave a like it does help the channel and subscribe if you're new i do upload roblox scripting tutorial all time on this video or all the time when i channels to make sure to subscribe for more awesome roblox to do supernat orioles and yeah that's pretty much it guys hope you enjoyed it use code tap i'll see you guys in the next one peace
Info
Channel: TheDevKing
Views: 196,856
Rating: undefined out of 5
Keywords: TheDevKing, Roblox Scripting Tutorial, roblox, studio, coding, tutorial, how to script on roblox, roblox raycasting, roblox raycasting tutorial, raycasting roblox studio, how to raycast roblox, roblox studio raycasting, raycasting roblox, roblox studio, advanced roblox scripting, advanced roblox scripting tutorial, how to use rays roblox, roblox ray, ray roblox
Id: CPcaJgQkxM8
Channel Id: undefined
Length: 30min 43sec (1843 seconds)
Published: Fri Dec 27 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.