YouTube2Text

Raycasting (Guns, Visualizers, Shapecasts) - Roblox Advanced Scripting #37 (2025) — Transcript

by BrawlDev · 9,048 words · 1,227 segments · language en · Watch on YouTube

Full transcript

  1. 0:00This is raycasting. A simple to
  2. 0:02understand idea yet very difficult
  3. 0:05concept to implement on Roblox. And it
  4. 0:08has many practical use cases as seen in
  5. 0:11very popular Roblox games. Think of
  6. 0:13games like blocks fruits where they have
  7. 0:15projectile based abilities that use ray
  8. 0:17casting or games like bedwars to
  9. 0:19determine what blocks can be destroyed
  10. 0:22by the players when they click on them.
  11. 0:24Those only scratched the surface of what
  12. 0:26you can do with ray casting, but in
  13. 0:28general, it's one of the most popular
  14. 0:29Roblox scripting concepts ever. My name
  15. 0:32is Baldev and in this advanced scripting
  16. 0:34tutorial episode, we'll be covering what
  17. 0:36ray casting is and how we can implement
  18. 0:38this inside of our own Roblox games. And
  19. 0:41by the end of this video, my goal is to
  20. 0:43equip you with the knowledge to
  21. 0:44understand ray casting on a fundamental
  22. 0:47level and can script ray casting for
  23. 0:49your own systems. But before we do,
  24. 0:51let's think about this for a minute. The
  25. 0:53first thing we want to ask ourselves is
  26. 0:55what the heck is raycasting and how can
  27. 0:57we use raycasting inside of our scripts
  28. 1:00to make really cool systems. So I have
  29. 1:02this little visual example here and to
  30. 1:04give you a fundamental definition of
  31. 1:07what ray casting is, it's the process of
  32. 1:10sending out an invisible line from a
  33. 1:13starting point in a specific direction
  34. 1:16with a defined length. So to help you
  35. 1:19understand what I'm talking about here,
  36. 1:20we have this red line here that's going
  37. 1:22to represent our ray. And we have this
  38. 1:25blue part here that represents our
  39. 1:26starting point of this line. And so if
  40. 1:29we were to take this line and we start
  41. 1:31it over here, then what we're going to
  42. 1:34do is we're going to use ray casting to
  43. 1:37essentially shoot a ray in a specific
  44. 1:39direction with a defined length. and
  45. 1:41it's going to try and detect if there's
  46. 1:44an object that intersects with that
  47. 1:47line. So, in other words, when we shoot
  48. 1:50this ray, we cast the ray to the world,
  49. 1:53hence the name ray casting, and it's
  50. 1:55just going to shoot a line like this,
  51. 1:56and it's going to try and detect an
  52. 1:58object that's in the way of it. And if
  53. 2:00it does detect an object, then we're
  54. 2:02going to get information about that
  55. 2:04object in specific to then do stuff with
  56. 2:07our scripts. So, it seems pretty cool
  57. 2:10and all, but how does this actually help
  58. 2:12us as Roblox developers? Well, let's say
  59. 2:14for instance, we have a system like uh a
  60. 2:17turret where if a player walks in front
  61. 2:19of the turret, then we want to get
  62. 2:20information about whether this is a
  63. 2:22player that's standing in front of the
  64. 2:23turret. And if it is a player, then we
  65. 2:26want to start firing the turret in this
  66. 2:28direction. Or let's say we have a gun
  67. 2:30system where if we shoot in a specific
  68. 2:33direction, then we want to use ray
  69. 2:36casting to fire a bullet from the gun to
  70. 2:39where the player clicked on their mouse.
  71. 2:41Let's say another use case is if we want
  72. 2:43to have a placement system where we want
  73. 2:46to get the player's mouse and we want to
  74. 2:48locate it with respect to the game's
  75. 2:50world to be able to place down items
  76. 2:52inside of the game. That also uses ray
  77. 2:54casting. There's even things like weapon
  78. 2:56hit detection. So, if you have like a
  79. 2:58sword and you want to be able to detect
  80. 2:59if a player is being hit by your sword's
  81. 3:02hitbox, then that's another practical
  82. 3:04use case of raycasting. And it's, as I
  83. 3:06mentioned before, the list just goes on
  84. 3:07with ray casting. It's a very widely
  85. 3:09used concept in Roblox scripting, but it
  86. 3:11can be challenging to those who have
  87. 3:13never used raycasting before. And my
  88. 3:16goal for this video is to teach you the
  89. 3:18fundamentals of raycasting and how you
  90. 3:20can use raycasting in your scripts to
  91. 3:22make really cool systems like this. So
  92. 3:24with that being said, let's go ahead and
  93. 3:26go straight into implementing ray
  94. 3:28casting. Now that we understand it,
  95. 3:30let's implement ray casting inside
  96. 3:32Roblox Studio. Now it's as I said
  97. 3:34earlier in this episode, in order to
  98. 3:35perform ray casting, we need two pieces
  99. 3:38of information. One is the starting
  100. 3:40point, also known as the origin, and the
  101. 3:42other is the direction that we want to
  102. 3:45take from that starting point. And an
  103. 3:47easy way that we can do this is by using
  104. 3:49a part as the starting point. And then
  105. 3:52we specify our own direction based off
  106. 3:54of that starting point. So what we're
  107. 3:56going to do is go into model and we're
  108. 3:58going to insert a part. Now with this
  109. 4:00part, I'm going to scale down just a
  110. 4:01little bit. I'm going to change the
  111. 4:03brick color to something different. I'm
  112. 4:04going to set anchored to true. And I'm
  113. 4:07also going to rename this part to origin
  114. 4:10part just like this. Now what we're
  115. 4:12going to do is go inside of server
  116. 4:13script service, hit the plus sign, and
  117. 4:15we're going to insert a script. Now from
  118. 4:17here we're going to first make reference
  119. 4:19to our origin part by saying local
  120. 4:21origin part equals workspace.org part
  121. 4:24just like this. Now we need to specify a
  122. 4:27direction. Now this direction can be any
  123. 4:29vector 3 value that we pick. But let's
  124. 4:31say from the origin point we want the
  125. 4:35ray to be 100 studs going this way. So
  126. 4:39if we were to specify the direction by
  127. 4:41saying local direction equals vector 3.
  128. 4:45Uh then what we're going to do is put
  129. 4:47100 for the X and then zero for the Y
  130. 4:50and then zero for the Z. So when we
  131. 4:53combine our origin part with its
  132. 4:55direction, the ray is going to look
  133. 4:57something like this where this is the
  134. 4:59starting point and then it goes 100
  135. 5:00studs this way. So this is what the ray
  136. 5:03is going to look like when we cast it
  137. 5:04into the world. Okay. So now that we
  138. 5:07have our pieces of information, how do
  139. 5:09we actually cast the ray into the world?
  140. 5:11Well, we can do this using the
  141. 5:13workspaces raycast method and we're
  142. 5:15going to be doing that under a loop by
  143. 5:17either using a while loop or by using
  144. 5:19run service. But in this case, we're
  145. 5:20going to use run service. So at the top
  146. 5:22here, we're going to make reference to
  147. 5:24run service by saying local run service
  148. 5:26equals game get service run service just
  149. 5:30like this. Now down here we're going to
  150. 5:32say run service heartbeat colon connect
  151. 5:36function open close parenthesis enter.
  152. 5:38Now we're going to use that raycast
  153. 5:39method that I mentioned before. So, what
  154. 5:41we're going to do is say workspace colon
  155. 5:45raycast open and close parenthesis. And
  156. 5:47this takes in three pieces of
  157. 5:49information, but right now we're going
  158. 5:50to be focused on the first two, which is
  159. 5:52the origin. So, we're going to say
  160. 5:54origin part.position.
  161. 5:56And then we're going to throw in the
  162. 5:58direction as well. So, this casts our
  163. 6:01array into the world. And this is going
  164. 6:04to return what's called a raycast result
  165. 6:07that gives us information about the
  166. 6:09object that this ray has hit. So we're
  167. 6:12going to throw this inside of a uh
  168. 6:14variable by saying local ray result
  169. 6:17equals and then the workspace raycast
  170. 6:19method here. So from here we're going to
  171. 6:22say if ray result then this is basically
  172. 6:25going to tell us that there was an
  173. 6:26object that actually hit the ray. So,
  174. 6:28we're going to say something like
  175. 6:30something has hit the array. Uh,
  176. 6:34otherwise, we're just going to say
  177. 6:36nothing has hit the array or something
  178. 6:39like that just to tell us information
  179. 6:41about whether there is an object that
  180. 6:43was actually in the way of the ray or
  181. 6:44not. Okay. So, in order for us to tell
  182. 6:47whether this is actually working, let's
  183. 6:49insert another part here. I'm just going
  184. 6:51to make this like a big wall just like
  185. 6:53this. And what we're going to do is go
  186. 6:55to test and hit run. And basically
  187. 6:59inside the output it's going to say that
  188. 7:01nothing has hit the ray multiple times.
  189. 7:04So right now the ray looks something
  190. 7:05like this. And if we were to move this
  191. 7:08object in the way of the ray then as you
  192. 7:11can see in the output it says something
  193. 7:13has hit the ray. And if we move the wall
  194. 7:15away again then it's going to say
  195. 7:17nothing has hit the ray. So this is the
  196. 7:19basic idea behind ray casting. It's to
  197. 7:22shoot array into the world and it's to
  198. 7:24give us information about objects that
  199. 7:27intersect that array. That's the whole
  200. 7:29idea behind it. Now, you might be
  201. 7:31wondering what information does this
  202. 7:33raycast actually return. Well, if we go
  203. 7:35back into our script and let's say that
  204. 7:37ray result was successful and we say ray
  205. 7:39result dot, we can see that there's five
  206. 7:42pieces of information that ray result
  207. 7:44does provide to us. The first one is the
  208. 7:46instance which basically tells us what
  209. 7:48was the instance that hit the ray. The
  210. 7:51second one is the exact position as to
  211. 7:54where the ray and the part hit each
  212. 7:57other. Uh the third thing is the
  213. 7:58material of the object that the ray hit.
  214. 8:01And then we have a normal vector and we
  215. 8:03also have a distance which tells us how
  216. 8:06far apart was the ray origin and the
  217. 8:08point at which both the ray and the
  218. 8:11object hit. So depending on what you're
  219. 8:12creating, these pieces of information
  220. 8:14can be really helpful in terms of what
  221. 8:16you want to accomplish with ray casting.
  222. 8:18Okay. So before we move on, I want to
  223. 8:20help you visualize these rays that we're
  224. 8:22creating by using a function that
  225. 8:24basically shows us what these rays look
  226. 8:26like so that we're not working with
  227. 8:28invisible rays throughout this entire
  228. 8:29episode. And so basically what I did was
  229. 8:31I created this function here called
  230. 8:33visualize ray that takes in two
  231. 8:35parameters. One is the origin and the
  232. 8:37other is the direction. So if we open
  233. 8:38this up, basically what's happening here
  234. 8:40is we're creating a new part. We're
  235. 8:42setting some properties and we're
  236. 8:43setting the parent of this to workspace
  237. 8:45and we're using debris to automatically
  238. 8:47get rid of this part after 0.5 seconds.
  239. 8:50Now what we need to do to complete this
  240. 8:52function is by defining some variables
  241. 8:54and by changing the size and the C frame
  242. 8:56of this part to accurately reflect on
  243. 8:59what the rays will look like and where
  244. 9:01it's placed. So we need two pieces of
  245. 9:03information which I'm going to explain
  246. 9:04what these are in just a little bit. But
  247. 9:06we need to define the length and the
  248. 9:08midpoint. So what we're going to do is
  249. 9:09say local length equals direction
  250. 9:13domagnitude
  251. 9:15just like this. And then we're going to
  252. 9:17define the midpoint by saying local
  253. 9:19midpoint equals origin plus direction*
  254. 9:240.5. So length is what's used to tell us
  255. 9:28how big we want this laser to be. And
  256. 9:32then midpoint is what's going to tell us
  257. 9:34where we want to position this ray that
  258. 9:37we create. Now the reason we want
  259. 9:39midpoint rather than let's say starting
  260. 9:41this laser at where the origin is is
  261. 9:44let's say we had a laser that looked
  262. 9:46like this. So we positioned this laser
  263. 9:48to be where the origin is. But if we
  264. 9:50were to change the size of this to make
  265. 9:52it look like this, then obviously this
  266. 9:54isn't where we want the laser to be.
  267. 9:57Instead, we want the laser to be like
  268. 9:59this instead because this reflects our
  269. 10:02ray more accurately than what I was
  270. 10:03showing you just a second ago. And in
  271. 10:05order for us to get this accurate
  272. 10:07position, we need to take the origin and
  273. 10:11the direction. So this is the origin and
  274. 10:13this is the direction. And we want to
  275. 10:14get half of that which is in the center
  276. 10:17here. This is the midpoint and this is
  277. 10:19the the the exact position that we want
  278. 10:21when we're trying to cast this ray into
  279. 10:23the world and by visually showing this
  280. 10:26as well. So that's what I'm trying to
  281. 10:28show with both the length and the
  282. 10:29midpoint. And now we can change the size
  283. 10:31in the C frame with these pieces of
  284. 10:33information. So what we're going to do
  285. 10:34is say new part do size equals vector 3
  286. 10:39new. We can set X and Y to be a really
  287. 10:41small number like 0.1 and 0.1. And then
  288. 10:45for Z, we're just simply going to throw
  289. 10:47in the length just like this. And now
  290. 10:49what we're going to do is say new part.
  291. 10:50Cframe equals Cframe new. And then
  292. 10:54inside of here, we're going to start it
  293. 10:57at the midpoint. So it's going to start
  294. 11:00right here in the center. And for the
  295. 11:03look vector, we're going to say origin
  296. 11:06plus direction just like this. And this
  297. 11:08is all that we need to do for our
  298. 11:10visualize ray function. So now what we
  299. 11:12can do down here is inside of our
  300. 11:14heartbeat loop. As soon as we get the
  301. 11:16ray result, we're going to say visualize
  302. 11:18ray and then we're going to pass in the
  303. 11:20origin part.position and then we're
  304. 11:22going to throw in the direction just
  305. 11:24like this. And this is our visualize ray
  306. 11:26function that's going to be helpful for
  307. 11:28us. So if we go into the game, I'm just
  308. 11:30going to delete this origin part. And if
  309. 11:32we hit run, then what we should see is
  310. 11:34now we have a visual array that we can
  311. 11:36look at to really see what is actually
  312. 11:39happening inside the workspace. So, if I
  313. 11:41were to move this part in front of the
  314. 11:43ray, then what it's going to do is it's
  315. 11:45going to say that something has hit the
  316. 11:46ray. And if we lift it back out, then
  317. 11:48it's going to say nothing has hit the
  318. 11:49ray. And so, this is what we're going to
  319. 11:51be using for the rest of this episode to
  320. 11:52really help us visualize what these
  321. 11:54lasers are going to look like. Okay. So,
  322. 11:56right now, we have a one-part raycast
  323. 11:59system where we have a part that acts as
  324. 12:02the origin point and then we have a
  325. 12:04direction that tells us how far do we
  326. 12:07want this ray to shoot out. So, it looks
  327. 12:09something like this currently. And we
  328. 12:10can move this starting point around to
  329. 12:13adjust where the ray is inside the
  330. 12:15world. But what if we wanted to create a
  331. 12:17two-part system where we have a starting
  332. 12:20point that's going to be a part and then
  333. 12:22we have an ending point or a goal that's
  334. 12:24also going to act as a part. So it's a
  335. 12:27two-way system where if we change the
  336. 12:28goal or the starting point, then it's
  337. 12:30going to affect how the raycast is
  338. 12:34displayed in the world. We can do that
  339. 12:35really quickly if we hit stop. And what
  340. 12:37I'm going to do is duplicate this origin
  341. 12:39part, move it over here. Uh I'm going to
  342. 12:41rename this to goal part. And I'm going
  343. 12:45to change the brick color this to
  344. 12:46something like yellow. So if we go
  345. 12:47inside of our script, what we're going
  346. 12:49to do is first make reference to the
  347. 12:51goal part by saying local goalpart
  348. 12:54equals workspace.galpart.
  349. 12:56And then what we're going to do here is
  350. 12:58we need to specify the direction based
  351. 13:01off of the goal parts position and the
  352. 13:03origin parts position. So, I'm going to
  353. 13:05comment out this direction definition up
  354. 13:08here, and I'm going to move it down here
  355. 13:10by saying local direction equals
  356. 13:13goalpart.position
  357. 13:15minus origin part.position. And then
  358. 13:18we're going to use this new direction to
  359. 13:20define our ray inside of the world. So
  360. 13:24if we now hit run, then what's going to
  361. 13:26happen is we're going to use ray casting
  362. 13:29to shoot array from the origin point and
  363. 13:32to the destination as well. And if we
  364. 13:35were to change either of these parts,
  365. 13:37then as you can see, the raycast is
  366. 13:39being updated to reflect on these new
  367. 13:42positional changes either on the goal or
  368. 13:45at the origin. So, this is pretty cool
  369. 13:47if you know the destination plus the
  370. 13:50origin so that you can create raycasts
  371. 13:52into the world that reflect on both of
  372. 13:55these parts just like this. Now, you
  373. 13:58might notice something. It says down
  374. 13:59here in the output that something has
  375. 14:01hit the ray and it keeps displaying this
  376. 14:03for some reason. There isn't anything
  377. 14:05blocking it. So, what's the problem
  378. 14:07here? Well, the problem here lies in how
  379. 14:10we specified that any object whatsoever,
  380. 14:14it doesn't matter what object it is, it
  381. 14:16is going to detect anything that's in
  382. 14:18the way of this ray. And what is this
  383. 14:20part? Well, the part that this ray is
  384. 14:23intersecting is this goal part right
  385. 14:25here. But what if we don't want this
  386. 14:27goal part to be part of the raycast
  387. 14:31detection? Well, this is a perfect
  388. 14:32transition into what we're going to be
  389. 14:34talking about next, which is called
  390. 14:35raycast parameters. So, raycasting has
  391. 14:38this cool feature that allows us to
  392. 14:40choose which specific instances can
  393. 14:43either be included or not be included in
  394. 14:46the list of instances that can be
  395. 14:49intersected with the rays that we shoot
  396. 14:52into the world. So, how this can be
  397. 14:54useful is with the example I just
  398. 14:56explained earlier and how we have a
  399. 14:58starting point, which is this part right
  400. 14:59here. So, we shoot the ray starting from
  401. 15:01here and then it goes into the goal
  402. 15:03part. But on the output, it's been
  403. 15:04saying that something has hit the ray
  404. 15:06even though nothing is actually in
  405. 15:08between these two points. And that's
  406. 15:10because it's detecting this goal part
  407. 15:12right here, which we don't want to
  408. 15:13include in the list. So, in order to
  409. 15:16handle situations like this, we're going
  410. 15:18to create what are called raycast
  411. 15:20parameters and add this goal part to the
  412. 15:24list of instances that are not allowed
  413. 15:26to be intersected with the ray. So let's
  414. 15:29go to our script so that we can make
  415. 15:31this change. And what we're going to do
  416. 15:33is create raycast parameters by saying
  417. 15:36local raycast params equals raycast
  418. 15:40params new open and close parenthesis.
  419. 15:43And now inside of here we're going to
  420. 15:45say raycast params dot filter descendant
  421. 15:50instances equals open and closed curly
  422. 15:53braces. And this is the table of all the
  423. 15:56instances that we don't want to include
  424. 15:58in the list or instances that we do want
  425. 16:00to include. So in this case, we're going
  426. 16:03to say goalpart here as the only
  427. 16:05instance in the list. And then we're
  428. 16:06going to say raycast params dot filter
  429. 16:10type equals enum dot
  430. 16:14raycast filter type dot. And then we
  431. 16:16have two options with either excluding
  432. 16:18or including whatever is inside of this
  433. 16:20list. But for this example, we're going
  434. 16:22to say exclude because we don't want to
  435. 16:25include this goal part as one of the
  436. 16:27instances that can be detected by this
  437. 16:29array. And finally, what we're going to
  438. 16:31do is we're going to throw in a third
  439. 16:33argument inside of our raycast method.
  440. 16:37And we're just simply going to pass in
  441. 16:38the raycast params just like that. So
  442. 16:42now if we go into the game and hit run,
  443. 16:44what we should see in the output is now
  444. 16:46nothing is hitting the ray, even though
  445. 16:49it technically is hitting this goal
  446. 16:51part, but we chose to ignore this goal
  447. 16:54part with this ray that's being fired.
  448. 16:56And if we move the wall over to here,
  449. 16:58then it's going to say that something
  450. 16:59has hit the ray, just like so. And if I
  451. 17:02lift it back out, then it's going to say
  452. 17:03that nothing is hitting the ray again.
  453. 17:04So raycast parameters are very useful if
  454. 17:07we don't want specific instances to be
  455. 17:09intersected with the rays or if we want
  456. 17:12only a specific number of instances to
  457. 17:15be intersected with the rays and we just
  458. 17:17ignore everything else. Okay, so with
  459. 17:19what we know, let's create a gun system
  460. 17:22using ray casting. And I want you to
  461. 17:24follow exactly what I do as we create
  462. 17:26the system. And the first thing we need
  463. 17:27to do is create the gun itself. So what
  464. 17:30we're going to do is go to model and
  465. 17:31we're going to hit part. Now, with this
  466. 17:33part, I'm going to scale it down just
  467. 17:34like this. Scale it up like this. And
  468. 17:37I'm going to rename this part to handle.
  469. 17:40And I'm going to go down here and turn
  470. 17:42off can collide. Then I'm going to hit
  471. 17:44the plus sign inside of workspace,
  472. 17:46insert a tool. I'm going to rename this
  473. 17:48tool to gun. And I'm going to put the
  474. 17:50handle inside of this gun, just like
  475. 17:52this. Next thing I'm going to do is
  476. 17:54duplicate the handle, scale it down, and
  477. 17:56move it to the right like this. And I'm
  478. 17:59going to rename this to part. And then
  479. 18:01I'm going to duplicate it one more time,
  480. 18:02move it over here, and call this the
  481. 18:05point, which is going to be where our
  482. 18:07bullets are going to be fired from. So
  483. 18:09to indicate this, I'm just going to
  484. 18:10change the brick color to, let's say,
  485. 18:12black. And now we have our gun. But we
  486. 18:14need to attach all these parts together
  487. 18:16so that they don't fly off when we u
  488. 18:18actually use it in the game. So we're
  489. 18:20going to use weld constraints by hitting
  490. 18:21the plus sign inside a part, inserting a
  491. 18:23weld constraint. We're going to set part
  492. 18:26zero to the part and then part one to
  493. 18:29the handle. And then I'm going to copy
  494. 18:30this weld constraint, put it inside a
  495. 18:32point, and I'm going to change part zero
  496. 18:34to be the point instead. And now
  497. 18:36everything should be connected together.
  498. 18:38But I want you to be careful because
  499. 18:40depending on how you design this, when
  500. 18:42you hold the gun, it's probably not
  501. 18:43going to be oriented correctly. And I'm
  502. 18:45going to show you a neat trick that you
  503. 18:47can use to check to see if you're facing
  504. 18:50the model correctly based on how you're
  505. 18:52designing it. So, with this handle, what
  506. 18:54we're going to do is we're going to
  507. 18:55right click on this handle, and there
  508. 18:57should be this option called show
  509. 18:58orientation indicator. And there's this
  510. 19:01little arrow here that shows you where
  511. 19:03the front-facing side of the handle is.
  512. 19:06And so, now all we need to do is just
  513. 19:08move these parts over here so that we
  514. 19:10can orient them correctly, just like
  515. 19:13this. So, I'm just going to right click
  516. 19:14on this and then hide orientation
  517. 19:16indicator. And if we now move the gun to
  518. 19:18starter pack and if we hit test and hit
  519. 19:21play, then what we should see is when we
  520. 19:24use our gun, then it should be oriented
  521. 19:27correctly. And now we have a gun for the
  522. 19:30player to use. So that's how you create
  523. 19:32the gun. And now we need to create the
  524. 19:34functionality of the gun. So we need
  525. 19:36three things with this gun. And before I
  526. 19:38do, I'm just going to take the part and
  527. 19:39the point and put it inside the handle
  528. 19:41just to make it more organized. So we
  529. 19:43need three things. We're going to hit
  530. 19:44the plus sign inside of gun. We're going
  531. 19:46to insert a script. We're going to
  532. 19:48insert a local script. And we're going
  533. 19:50to insert a remote event. So, with all
  534. 19:54of these three things, what we need to
  535. 19:56do essentially is make it so that when
  536. 19:58the player clicks their mouse on a
  537. 20:00specific direction, then we want to send
  538. 20:02that information to the server so that
  539. 20:04we can create a ray that will shoot out
  540. 20:07from the point of the gun to the
  541. 20:11direction of where the mouse is. And
  542. 20:12that's the whole idea behind this gun.
  543. 20:14uh while we use raycasting. So I'm going
  544. 20:16to rename this remote event to fire gun.
  545. 20:19And inside of our local script, we're
  546. 20:21going to make reference to the tool by
  547. 20:23saying local tool equals script.parent.
  548. 20:25Then we're going to say local fire gun
  549. 20:28equals tool colon waitfor gun just like
  550. 20:32this. And what I'm also going to do is
  551. 20:34say local player equals
  552. 20:37game.players.local
  553. 20:38player. And then we're going to get the
  554. 20:40player's mouse. So, we're going to say
  555. 20:42local mouse equals player colon get
  556. 20:46mouse open and close parenthesis just
  557. 20:48like this. And now what we're going to
  558. 20:49do is say tool.activated
  559. 20:52col connect function open close
  560. 20:54parenthesis enter. We're simply just
  561. 20:55going to say fire gun colon fire server
  562. 20:59open close parenthesis. And there's two
  563. 21:01pieces of information that we need. So
  564. 21:03we need the origin point and we also
  565. 21:06need the direction which we specified
  566. 21:08before with raycasting. And basically
  567. 21:10what we need is the point part. So we're
  568. 21:12going to say local point part equals
  569. 21:15tool colon wait for child handle colon
  570. 21:18wait for child point just like this. So
  571. 21:22we're going to say point part.position.
  572. 21:25This is going to be our origin. And then
  573. 21:26we're going to say mouse.h hit.position
  574. 21:30which is basically the position of where
  575. 21:32the player fired their mouse. So now
  576. 21:35we're done with the local script. So, if
  577. 21:36we go into our normal script, I'm just
  578. 21:39going to copy the tool reference and
  579. 21:41just put it inside of here. What we're
  580. 21:42also going to do is make reference to
  581. 21:44the fire gun remote event by saying
  582. 21:46local fire gun equals tool. Fire gun
  583. 21:49just like this. Then we're going to say
  584. 21:51fire gun onserver event connect function
  585. 21:55open close parenthesis. So, there's
  586. 21:56three pieces of information here.
  587. 21:58There's the player, there's the origin,
  588. 22:02and then there's the mouse position. So
  589. 22:05with these three pieces of information
  590. 22:06we can now start creating raycasts on
  591. 22:09the server side. So the first thing that
  592. 22:11we need to do is calculate the direction
  593. 22:13which is basically let's imagine that
  594. 22:16this is the origin point like this is
  595. 22:17the point part and we want to make it so
  596. 22:20that we get the direction from where the
  597. 22:24mouse position was versus where the
  598. 22:27origin point was which is always going
  599. 22:28to be the point part. So if we were to
  600. 22:32let's say calculate it, it would look
  601. 22:34something like this. This is the result
  602. 22:36that we're trying to get. And the way we
  603. 22:38can do this is we just simply uh
  604. 22:40subtract the mouse position with the
  605. 22:42origin position, get a unit vector of it
  606. 22:44so that it looks something like this,
  607. 22:45and then multiply it by some sort of
  608. 22:47scaler, which shows us how far we can
  609. 22:49shoot the gun, which let's say in this
  610. 22:51case is 50 studs. It's going to look
  611. 22:53something like that. So we want to be
  612. 22:54able to calculate that inside of our
  613. 22:56script. So if we go back to our script,
  614. 22:58we can say local direction equals open
  615. 23:01close parenthesis mouse position minus
  616. 23:05origin dot unit times and then we can
  617. 23:09say however many studs we want. We can
  618. 23:12say 50. Now that I think about it, we
  619. 23:14can actually just create a uh variable
  620. 23:16here by saying local distance multiplier
  621. 23:20just like this. And then we can say
  622. 23:21something like 50 just so that we can
  623. 23:23make it adjustable later if we want to.
  624. 23:26So, now that we have that, we can go to
  625. 23:28our original script that we used earlier
  626. 23:31in this tutorial guide and we can just
  627. 23:32copy the same raycast parameters here
  628. 23:34because when we're going to be firing
  629. 23:36our gun, there are certain things that
  630. 23:37we don't want to intersect with the ray.
  631. 23:40So, we're going to go to our original
  632. 23:41script, copy this, and then paste it
  633. 23:43inside of here. Now, for the filter
  634. 23:45instances, what we can do is create a
  635. 23:47variable here called local excluded
  636. 23:51parts equals open and close parenthesis.
  637. 23:53And then we can just replace that with
  638. 23:55this. And now what we're going to do is
  639. 23:58we're going to use the raycast method by
  640. 24:01saying local ray result equals workspace
  641. 24:04colon raycast. And then we're going to
  642. 24:06throw in the origin the direction and
  643. 24:10then the raycast params just like that.
  644. 24:13And we're going to check if the result
  645. 24:15was successful. So we're going to say if
  646. 24:17ray result then we're going to say we
  647. 24:20have found something. So, result has
  648. 24:22been found. And then we're just going to
  649. 24:24print the ray result for testing
  650. 24:25reasons.
  651. 24:27And then what we're going to do is we're
  652. 24:29going to go back to our original script
  653. 24:31and we're literally going to copy the
  654. 24:32exact same visualize ray function and
  655. 24:35we're just going to paste it inside of
  656. 24:37here. Just like this. Except one thing
  657. 24:39we can do is probably change the color
  658. 24:40of this to let's say orange. And what
  659. 24:43we're going to do here is we're going to
  660. 24:44say visualize ray origin direction. Just
  661. 24:48like that. So what do we want this ray
  662. 24:51to actually do? Well, we can do a couple
  663. 24:53things. Uh, one thing is we can change
  664. 24:55the color of the instance that the
  665. 24:57bullet has hit to a different color. So
  666. 24:59what we can do is say ray results do
  667. 25:02instance.color
  668. 25:04equals color3.RGB.
  669. 25:07And then we can change this to orange.
  670. 25:10And if this ray was not successful, then
  671. 25:12what we're going to do is we're just
  672. 25:14simply going to say result not found.
  673. 25:18And then we can do the same thing with
  674. 25:20the visualize ray function. We're just
  675. 25:22going to put that here as well. Okay. So
  676. 25:24now if we go into the game, hit test,
  677. 25:28and hit play, then what we should see
  678. 25:30with this gun is if we were to fire it
  679. 25:33at a certain direction, then it's going
  680. 25:35to shoot a ray from the origin point,
  681. 25:38and then it's going to fire off in the
  682. 25:40direction of the player's mouse. And
  683. 25:42it's going to tell us if a result has
  684. 25:44been found or if a result has not been
  685. 25:46found. So if we were to shoot this wall,
  686. 25:48then it did find an instance and it
  687. 25:51changed the color of that instance to
  688. 25:53the color that we specified inside of
  689. 25:54the script. And we can do this with
  690. 25:56basically any instance that this has
  691. 25:58detected. And so that is uh something
  692. 26:01that we can do with this gun. But let's
  693. 26:03add another functionality. Let's make it
  694. 26:05so that if this gun detects a player,
  695. 26:08then we want that player to take damage.
  696. 26:10And we can do that inside of here by
  697. 26:12first locating the character. So we're
  698. 26:13going to say local character equals
  699. 26:15array result.instance
  700. 26:18colon find first ancestor of class open
  701. 26:23and close parentheses model. And the
  702. 26:24reason we say it like this is because a
  703. 26:26player's character is a model. And if we
  704. 26:29find the first ancestor of a specific
  705. 26:32instance, which is likely going to be
  706. 26:33the player's body part, like its arm or
  707. 26:36its torso or anything like that, then
  708. 26:39we're going to see if this is an actual
  709. 26:40character based off of that information.
  710. 26:43So then if we do find the character then
  711. 26:45we're going to try to find the humanoid.
  712. 26:47So we're going to say local humanoid
  713. 26:48equals character find first child of
  714. 26:51class and then we're going to say
  715. 26:52humanoid. So if the humanoid does exist
  716. 26:55then we're just simply going to say
  717. 26:56humanoid colon take damage and then
  718. 27:00we're going to specify like a damage
  719. 27:02amount like five or something. We can
  720. 27:04even set that here as well. So local
  721. 27:07damage equals five. And then we can just
  722. 27:09replace that with this damage. And I
  723. 27:12meant to say humanoid here. Okay. So now
  724. 27:15we can test this even further. If we go
  725. 27:17to avatar and we click on rig builder,
  726. 27:19we could just select any rig. I'm just
  727. 27:22going to say block avatar. And if we now
  728. 27:24go into test and hit play, then we can
  729. 27:27test out this gun by firing it at the
  730. 27:29NPC. And as you can see, it's taking
  731. 27:31five bits of damage every single time.
  732. 27:33And that's the result that we wanted to
  733. 27:35achieve from this. Now, this is a
  734. 27:37simplified way of creating a gun system
  735. 27:39using ray casting. But I hope you've
  736. 27:41been able to learn something from using
  737. 27:43ray casting to create this gun system
  738. 27:45that we just created. Okay, so one thing
  739. 27:47I forgot to do was add to our excluded
  740. 27:50parts table. Now you might be wondering
  741. 27:52what should we add to the excluded parts
  742. 27:54table? Well, there's two important
  743. 27:56things. So what we're going to do is say
  744. 27:57table.insert
  745. 27:59and then for excluded parts, we're going
  746. 28:01to include the player.c character. And
  747. 28:03the reason we want to do this is because
  748. 28:05we don't want to make sure that we hurt
  749. 28:07ourselves when we fire the ray. And also
  750. 28:09another thing is over here in our
  751. 28:11visualize array function, we want to
  752. 28:14make sure that we add the ray itself
  753. 28:18into the excluded parts because we don't
  754. 28:20want the part to be able to interact
  755. 28:21with each other when we shoot multiple
  756. 28:24rays at the same time. So we're just
  757. 28:26going to copy the same uh thing here and
  758. 28:29paste inside of here, but instead we're
  759. 28:31just going to add new part instead to
  760. 28:34our visualize rate function. So, I
  761. 28:36wanted to clear that up to make this gun
  762. 28:38system even better that actually uses
  763. 28:39the raycast parameters. Hey, post edit
  764. 28:42broad here. So, it turns out there's
  765. 28:44actually a few mistakes that I made with
  766. 28:46the raycasting gun script that we just
  767. 28:48created. And I just wanted to quickly
  768. 28:50address what these problems are and how
  769. 28:52we can create a fixed version for this
  770. 28:54because the script that we have right
  771. 28:55now technically works, but there are a
  772. 28:58few issues with it that can make this
  773. 29:01problematic. And so, I just want to
  774. 29:02quickly address them. So if we go inside
  775. 29:04of our scripts, there are two problems
  776. 29:06here. Uh one is we're using excluded
  777. 29:09parts as a global table which can
  778. 29:11actually cause memory leaks based on the
  779. 29:13way that we've implemented this. And
  780. 29:15another problem is if we scroll down
  781. 29:17here, we can see that raycast is being
  782. 29:20fired before the visualize ray method.
  783. 29:23Now you might be thinking why is this a
  784. 29:25problem? Well, if we go inside of the
  785. 29:27visualize array method over here, uh
  786. 29:30we're using table.insert insert to
  787. 29:32insert this new array that we shoot and
  788. 29:34put it inside of the excluded parts
  789. 29:36table. Now, the reason we do this is
  790. 29:38because we want to add all of these rays
  791. 29:41as filters for raycast parameters so
  792. 29:44that when we fire the bullets, the other
  793. 29:47visualized bullets are not going to be
  794. 29:48included with the ray casting, which is
  795. 29:50the whole point behind this. But if you
  796. 29:52still don't see the problem, basically
  797. 29:55when we fire raycast, we want to take
  798. 29:58into consideration all the excluded
  799. 30:00parts that are in here. But if we're
  800. 30:02adding these new parts to excluded parts
  801. 30:05after the raycast has already been
  802. 30:08fired, then there's no point of doing it
  803. 30:11because the raycast method has already
  804. 30:13been fired. So we actually need to fire
  805. 30:15visualize ray before the raycast method
  806. 30:18over here. So those are some issues that
  807. 30:20we need to change. And one thing that I
  808. 30:22want to mention with the whole memory
  809. 30:24leakage thing is when we use debris to
  810. 30:27get rid of these new parts, the
  811. 30:29references to these parts inside of
  812. 30:32excluded parts are still inside the
  813. 30:35table, which is why it can accumulate
  814. 30:37over time and create a memory leakage.
  815. 30:39So if I were to show you what I'm
  816. 30:41talking about, I'm just going to write a
  817. 30:42print statement and I'm going to print
  818. 30:44the number of items that are inside of
  819. 30:46excluded parts. So if I fire with my
  820. 30:48gun, then as you can see in the output,
  821. 30:50the number that's being displayed is the
  822. 30:53number of entries that are inside of
  823. 30:55excluded parts. And as you can see, it's
  824. 30:57not going away because the rays
  825. 30:59disappear, but the references to those
  826. 31:01rays inside of excluded parts are still
  827. 31:04inside the table. And if we keep firing
  828. 31:06this, then this is just going to
  829. 31:07accumulate more and more over time,
  830. 31:09which is not good. Which is why we need
  831. 31:11to fix this as one of the problems. And
  832. 31:13I hope that this part is making sense so
  833. 31:15far. Okay, so I created a fixed version
  834. 31:18of the gun ray casting script. Um, so if
  835. 31:20we go inside of here, then uh I I
  836. 31:23basically commented out all the parts
  837. 31:24that we don't need anymore. So this
  838. 31:26global reference to excluded parts is no
  839. 31:29longer here because we moved it down
  840. 31:32here every single time the player fires
  841. 31:35the gun. So we create a new excluded
  842. 31:38parts table every single time. And
  843. 31:40another thing that I changed was this
  844. 31:42visualize array function is now being
  845. 31:44fired before the raycast uh method. So
  846. 31:48if we go inside of visualize array, the
  847. 31:51things that I've changed here are a few
  848. 31:53things. So instead of setting the parent
  849. 31:55of the bullet inside of workspace, we're
  850. 31:57instead putting it inside of what's
  851. 31:59called a visualization folder. So this
  852. 32:01is a little folder that I created called
  853. 32:03bullet visuals and it's stored inside of
  854. 32:05the workspace. So, it's basically this
  855. 32:08bullet visuals folder right here. So, if
  856. 32:10I were to fire my gun, then all the
  857. 32:13bullets are being stored inside of this
  858. 32:15one folder. And the reason it's like
  859. 32:17this is because this makes it a lot
  860. 32:19easier for us to keep track of all the
  861. 32:21bullets that are going to be included in
  862. 32:24the raycast parameters so that we don't
  863. 32:27include them when we're firing the
  864. 32:29bullets uh inside the game. So, we also
  865. 32:32got rid of the debris method and instead
  866. 32:35what we're going to do is simply return
  867. 32:37the new part and and have it be a
  868. 32:40variable here called visual part so that
  869. 32:42we can use it inside of this part of the
  870. 32:45script. So, with excluded parts, we want
  871. 32:48to include the player's character and we
  872. 32:49want to also include everything that's
  873. 32:52inside the visualization folder with all
  874. 32:54the bullets. So, another thing is I also
  875. 32:56got rid of all the visualize array
  876. 32:58methods over here. And I also completely
  877. 33:01got rid of this else uh branch in this
  878. 33:04if statement right here because we don't
  879. 33:06really need it. And at the very end
  880. 33:08here, I added the game debris add item
  881. 33:11method and I just got rid of the visual
  882. 33:14part after 0.5 seconds. So, this is
  883. 33:17basically how we create a more robust
  884. 33:20raycast gun script that not only
  885. 33:24prevents memory leaks from happening
  886. 33:26because we're creating an excluded parts
  887. 33:28table every single time the player fires
  888. 33:31the gun instead of having it be global,
  889. 33:33but also this is a more appropriate way
  890. 33:36of using raycast parameters because now
  891. 33:39this raycast is actually taking into
  892. 33:41consideration the new bullet visual that
  893. 33:44has been created inside the game before
  894. 33:47we fire the raycast method. So, I just
  895. 33:50wanted to quickly address this because
  896. 33:52if I left the current script as is, then
  897. 33:54you probably would have been misusing
  898. 33:56the raycast methods inside of this video
  899. 33:59and I did not want that. So, I just
  900. 34:01wanted to quickly address it. And if you
  901. 34:03still are watching up until this point,
  902. 34:05then I really appreciate it a lot. I do
  903. 34:07put in a lot of time into the scripting
  904. 34:09tutorials, and there's not that much
  905. 34:11left on this episode. So, I want you to
  906. 34:13keep in mind as you follow through the
  907. 34:15rest of this video is that I'm still
  908. 34:16going to be misusing the raycast
  909. 34:18parameters. So, I just want you to keep
  910. 34:20an open mind as you follow through the
  911. 34:22rest of this episode. That is all for
  912. 34:24me. Thank you so much. Now, you might be
  913. 34:26asking yourself, is it possible to
  914. 34:29change the shape or the size of these
  915. 34:31rays? Because theoretically, ray casting
  916. 34:34is when you shoot a very tiny thin
  917. 34:37invisible ray uh inside the world. But
  918. 34:40if we wanted to utilize the
  919. 34:41functionality of ray casting, but
  920. 34:43instead using different shapes and
  921. 34:46different sizes, then it actually is
  922. 34:48possible. We can use things like block
  923. 34:51casting, shape casting, and sphere
  924. 34:53casting that can make bullets that look
  925. 34:55something like this. So we can use
  926. 34:58things like cylinders with different
  927. 34:59sizes. And we can even use things like
  928. 35:01blocks to achieve the same effect, but
  929. 35:04instead have like a bigger volume. So
  930. 35:07that's the definition of these shape
  931. 35:09casts and block casts and things like
  932. 35:11that. It's basically ray casting, but if
  933. 35:14it had volume. And so I think this is
  934. 35:16something really important to understand
  935. 35:18when you're utilizing this inside of
  936. 35:19your Roblox games. And we're just simply
  937. 35:21going to take our existing gun script
  938. 35:23and we're just going to modify it to use
  939. 35:26these casts instead of ray casting. So
  940. 35:29let's go ahead and go do that right now.
  941. 35:30Okay. So, we're going to modify our
  942. 35:32current gun system to use sphere casting
  943. 35:35to increase the size of our bullets. And
  944. 35:38basically what we're going to do is
  945. 35:39we're going to go to our script and
  946. 35:41inside of our remote event, we're just
  947. 35:44going to change two things. The first
  948. 35:45thing is instead of firing raycast,
  949. 35:48we're going to fire sphere cast instead.
  950. 35:51And this takes in four parameters
  951. 35:53instead of three. So we still have the
  952. 35:55origin, the direction, and the raycast
  953. 35:57parameters, but as part of the second
  954. 35:59parameter, this is taking what's called
  955. 36:01a radius, which determines the radius of
  956. 36:05the spherical shape in studs. So we can
  957. 36:08set this to be a number like two. But we
  958. 36:10can also just create a variable up here
  959. 36:11by saying local bullet uh radius equals
  960. 36:162. And then we can just replace this two
  961. 36:18with the variable we just created. Next
  962. 36:20thing we're going to change is instead
  963. 36:21of visualize ray, we're going to create
  964. 36:23our own function for visualizing the
  965. 36:26shape because we're not visualizing
  966. 36:28array anymore. We're visualizing a
  967. 36:29shape. So we're going to create a new
  968. 36:32function up here by saying local
  969. 36:34function visualize shape and then it's
  970. 36:37going to take in the origin, the
  971. 36:39direction, but this is also going to
  972. 36:41take the ray result as well. And this is
  973. 36:44what we're going to be calling instead.
  974. 36:46So, we're going to say visualize shape
  975. 36:49origin direction and then the ray
  976. 36:52results just like that. And I'm also
  977. 36:54going to copy that and paste it down
  978. 36:56here as well. Okay. So, here's how this
  979. 36:59is going to work. We're going to create
  980. 37:01a visual bullet inside of Studio and
  981. 37:04we're going to clone that bullet and
  982. 37:06shape it so that it's going to match
  983. 37:08where the player is going to be firing.
  984. 37:10And we're also going to adjust the size
  985. 37:11of it as well. So, what we're going to
  986. 37:13do is go to model, hit part, and we're
  987. 37:15going to create a cylinder, and we're
  988. 37:18going to rename this to bullet, and then
  989. 37:22we're going to change the brick color of
  990. 37:23this to, let's say, orange. And we're
  991. 37:25going to change the material of this to,
  992. 37:27let's say, neon. I'm going to set can
  993. 37:31collide off, and I'm going to turn on
  994. 37:33anchored, just like this. Okay. So now
  995. 37:36that we have this bullet, I'm going to
  996. 37:37throw this inside of server storage. And
  997. 37:39we're going to make reference to this
  998. 37:41bullet inside of our script by saying
  999. 37:44local bullet equals game.server storage
  1000. 37:48dot bullet. Just like that. So now what
  1001. 37:51we're going to do inside of visualize
  1002. 37:53shape is we're just simply going to
  1003. 37:55clone the bullet. So we're going to say
  1004. 37:57local new bullet equals bullet
  1005. 38:02colon clone. And then we're going to set
  1006. 38:05the parent of this new bullet. So new
  1007. 38:06bullet.parent equals workspace. And then
  1008. 38:08we're going to use table.insertexcluded
  1009. 38:12parts and then new bullet just so that
  1010. 38:14the bullets are not going to be
  1011. 38:15interacting with each other. And then
  1012. 38:17we're going to say game. debris colon
  1013. 38:19add item new bullet. And then we're
  1014. 38:21going to get rid of it after 0.5
  1015. 38:23seconds. Just like this. Okay. So what's
  1016. 38:26left to do here is we need to make a
  1017. 38:28couple calculations so that we can be
  1018. 38:30able to change the size and the C frame
  1019. 38:33of this bullet accurately. Okay. So we
  1020. 38:35need to calculate a few things before we
  1021. 38:37continue. So the first thing is the
  1022. 38:39position of the end result or the
  1023. 38:42position of the goal if that makes
  1024. 38:43sense. So basically what I'm trying to
  1025. 38:45say is that we either hit something with
  1026. 38:47the ray that we shot and we're just
  1027. 38:48going to get the position of that or if
  1028. 38:50we don't hit anything then we're just
  1029. 38:52simply going to get the origin plus
  1030. 38:53direction. That's going to give us the
  1031. 38:55information about the position. Another
  1032. 38:58thing we need to calculate is with that
  1033. 39:00position and with the origin point, we
  1034. 39:02want to be able to get the distance
  1035. 39:04between them as well because that's
  1036. 39:06going to be useful for us to know the
  1037. 39:08size of the bullet. And we also want to
  1038. 39:10get the midpoint so that we know where
  1039. 39:12to position the bullet when we shoot it
  1040. 39:15out into the world. So, if we go inside
  1041. 39:17of our script, uh we're going to
  1042. 39:18calculate these three things on top of
  1043. 39:20here. So we're first going to say local
  1044. 39:22position equals and this can either be
  1045. 39:25one of two values. So we're going to say
  1046. 39:27if ray result then so if uh if we
  1047. 39:32actually did get a result from shooting
  1048. 39:34the ray then we're just simply going to
  1049. 39:35say ray results.position.
  1050. 39:39Otherwise we're just going to calculate
  1051. 39:41the origin plus the direction just like
  1052. 39:43this. Next thing we're going to do is
  1053. 39:45calculate the distance. So we're going
  1054. 39:46to say local distance equals open and
  1055. 39:49close parenthesis. We're going to take
  1056. 39:50the position we just calculated. We're
  1057. 39:53going to subtract that from the origin,
  1058. 39:55so where the the gun was shot from. And
  1059. 39:57then we're going to say dot magnitude
  1060. 39:59because we just want the distance. Like
  1061. 40:01we want a fixed number that's going to
  1062. 40:03be telling us the distance. And then the
  1063. 40:05midpoint, which is literally the exact
  1064. 40:07same calculation as we did in visualize
  1065. 40:09ray. So I'm just going to copy and paste
  1066. 40:11that into here. Okay. So now that we
  1067. 40:13have this, let's change the size and
  1068. 40:16frame of this. So with this bullet, this
  1069. 40:19bullet is a cylinder. So we need to
  1070. 40:21figure out a way to make this cylinder
  1071. 40:24work with its size and also its C frame.
  1072. 40:26And I'm going to show you what that
  1073. 40:27looks like here. So we're going to say
  1074. 40:29new bullet do size equals vector 3. And
  1075. 40:33inside of here, we want the X to be the
  1076. 40:36distance. So if we were to fire from the
  1077. 40:39origin over to that wall over there,
  1078. 40:41then the X is just going to be the
  1079. 40:43distance. So for the Y and Z, these two
  1080. 40:46are going to be the same numbers because
  1081. 40:48it's going to be a perfect symmetrical
  1082. 40:50sphere that looks like this. And if you
  1083. 40:52have taken geometry, basically a radius
  1084. 40:55is like half of a circle if you were to
  1085. 40:58determine the distance from the midpoint
  1086. 41:00to one of the outer edges of the circle.
  1087. 41:04So if we want a full circle, we want the
  1088. 41:07diameter, which in this case that's what
  1089. 41:09it's called, which is basically the
  1090. 41:11bullet radius* 2. And for the Z, it's
  1091. 41:15going to be the exact same thing here as
  1092. 41:17well. So this is how we calculate the
  1093. 41:21size for the new bullet. And now what
  1094. 41:23we're going to do is change the C frame
  1095. 41:26of it. So we're going to say new
  1096. 41:27bullet.cframe equals CF frame.look at or
  1097. 41:31you can use Cframe new. It's up to you.
  1098. 41:33We're going to start this at the
  1099. 41:35midpoint. So, this is the location of
  1100. 41:37where this new bolt's going to be. And
  1101. 41:40then we want it to look at the position.
  1102. 41:43So, we want to make it look at the
  1103. 41:45result of the array. Or if it didn't
  1104. 41:47find a result, then it's just going to
  1105. 41:49get the origin plus direction, and
  1106. 41:50that's where it's going to be facing.
  1107. 41:53Okay. But here's the problem, though.
  1108. 41:55So, right now, if we go inside the game,
  1109. 41:58hit test, and hit play. And if we were
  1110. 42:00to try and fire this right now. So what
  1111. 42:03this is going to do is as you can see
  1112. 42:06the bullet is not oriented correctly.
  1113. 42:08But you can see that the size of our
  1114. 42:09bullet is a lot bigger now because we're
  1115. 42:12using sphere casting instead of ray
  1116. 42:15casting. But we just simply want to
  1117. 42:17orient this in a way so that we can
  1118. 42:18actually make the bullet fire from the
  1119. 42:21point to the direction that it's trying
  1120. 42:23to hit. And what we can do is just
  1121. 42:25simply change the orientation of it. So
  1122. 42:27if we go inside of our script, we're
  1123. 42:29just going to multiply this by Cframe.
  1124. 42:34And this takes in three parameters which
  1125. 42:36are all in radians. So we want to change
  1126. 42:39the yaxis specifically. So the x is
  1127. 42:42going to be zero. And then the y is
  1128. 42:44going to be the thing that we're going
  1129. 42:45to change. And then z is just going to
  1130. 42:47be zero as well. So for the y, because
  1131. 42:49this takes in radians and not degrees,
  1132. 42:51we're going to say math.rad open close
  1133. 42:54parenthesis. And we just want to flip
  1134. 42:55this 90°. And that is all that we need
  1135. 42:58to do for this part. So now we're
  1136. 43:00basically done. If we hit play, then
  1137. 43:02what we should see is if we take our gun
  1138. 43:04and we shoot it in a specific direction,
  1139. 43:06then you can see that the bullet is now
  1140. 43:08a lot bigger and we have more range with
  1141. 43:11the bullet. We have a much bigger hitbox
  1142. 43:14because we're using sphere casts rather
  1143. 43:16than ray casting. And I think this is a
  1144. 43:18really useful way of using sphere casts.
  1145. 43:21But of course, there's many practical
  1146. 43:23use cases that you can use with sphere
  1147. 43:25casting and ray casting as a whole. And
  1148. 43:28I hope that the takeaway from this video
  1149. 43:29is you learned a lot about how ray
  1150. 43:32casting works and how to incorporate ray
  1151. 43:35casting inside of your scripts. And this
  1152. 43:37is definitely one of the most fun
  1153. 43:38episodes that I've been able to work on
  1154. 43:40on the channel. And I apologize that it
  1155. 43:42took me a really long time to actually
  1156. 43:44get to this episode. I really wanted to
  1157. 43:46save this episode for one of the last
  1158. 43:47ones just because I think it's really
  1159. 43:49cool just to learn this stuff. It's as I
  1160. 43:52said in the beginning of this episode,
  1161. 43:53raycasting has a wide range of practical
  1162. 43:56use cases that I will not be able to
  1163. 43:58cover inside of one single tutorial
  1164. 44:00episode. So, my hope for this video is
  1165. 44:03that since you've watched all the way
  1166. 44:05through till the end of this really long
  1167. 44:06video, I know that you're definitely
  1168. 44:08dedicated to learning about raycasting
  1169. 44:10and being able to use what you learned
  1170. 44:12from this video and incorporate them
  1171. 44:14inside your scripts or your systems that
  1172. 44:17you're going to be creating for the
  1173. 44:18future. And I can say with confidence
  1174. 44:20that you can definitely use what you
  1175. 44:23learned from this video and incorporate
  1176. 44:25inside your scripts. It just takes a
  1177. 44:26little bit of practice and a little bit
  1178. 44:28of trial and error, but I think you
  1179. 44:29definitely got this. There were a couple
  1180. 44:31things that I left out from this
  1181. 44:33tutorial, otherwise it would have just
  1182. 44:34been too long. Like debunking my turret
  1183. 44:37system that I showed you in the
  1184. 44:38beginning of this episode that is more
  1185. 44:40on the advanced side. And I decided that
  1186. 44:43I'm going to make that a individual
  1187. 44:45video that's going to be over on my
  1188. 44:46Patreon page. So, if you want to support
  1189. 44:48the work that I do and you love the
  1190. 44:50content that I put out, then I will
  1191. 44:51leave a link to my Patreon page in the
  1192. 44:53description if you want to get access to
  1193. 44:54my scripts and also some occasional
  1194. 44:56bonus content that I might put out over
  1195. 44:58there as well. I would also like to give
  1196. 45:00a shout out to my Patreon members for
  1197. 45:01their continued support. Their support
  1198. 45:03has really been helping me continue
  1199. 45:04making the videos that I've been putting
  1200. 45:06out, and I can't appreciate them more
  1201. 45:08than enough. Please share this video
  1202. 45:10with anyone that you think might be
  1203. 45:12interested in learning about raycasting
  1204. 45:14just to get the word around and to help
  1205. 45:15boost this video into the algorithm by
  1206. 45:18also doing things like leaving a like on
  1207. 45:20this video and leaving a comment as
  1208. 45:21well. You can literally comment about
  1209. 45:23anything. Just anything that can help
  1210. 45:25boost this video into the YouTube
  1211. 45:26algorithm. That would be really
  1212. 45:27appreciative. Like I do spend a lot of
  1213. 45:29hours researching each and every one of
  1214. 45:31these videos to try to make them as high
  1215. 45:33quality as they can be for you guys to
  1216. 45:36to really learn these scripting
  1217. 45:37concepts. and I'm really appreciative of
  1218. 45:39you for sticking around this long in the
  1219. 45:42advanced scripting tutorial guide and
  1220. 45:43what we've been able to accomplish
  1221. 45:45throughout this entire journey and I'm
  1222. 45:48really thankful for that. So that is
  1223. 45:50going to be it for this episode on
  1224. 45:52raycasting. I hope you enjoyed this
  1225. 45:54episode and you learned a lot from it
  1226. 45:56and I will see you in the next one.
  1227. 46:00Take

About this transcript

This page contains the full transcript of Raycasting (Guns, Visualizers, Shapecasts) - Roblox Advanced Scripting #37 (2025) by BrawlDev, generated from the public captions YouTube serves with the video. The transcript has 9,048 words across 1,227 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.