Raycasting for Weapon

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I would love to acheive an understanding of raycasting and why and how it is used on a weapon.
  2. What is the issue? Include screenshots / videos if possible!
    Lack of understanding in area of raycasting.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have watched multiple videos on how to make a gun and found out how to make it but they dont explain why raycasting is needed.

Raycasting is used in collision detection, .Touched is usually only used for stationary objects really.

the term Raycasting is self explanitory, It casts a ray. A ray is a line with an Origin and a Direction, you can use roblox’s methods like :FindPartOnRay() to get any intercepting parts.

So the reason guns use raycasting is because, it practically shoots a straight line in the bullet’s trajectory.
Then you use the :FindPartOnRay() method to get a part which the ray has hit. From there you can get the humanoid and damage it etc.

https://developer.roblox.com/en-us/articles/Raycasting

If you have any further questions, let me know.

1 Like

Thank you very much. :grinning:

1 Like

Quick question how would i go about making trajectory? Like a crossbow’s

If you want to calculate a trajectory to mathematically use it, I’d look into mathematical functions and learn how to implement them into roblox.

Otherwise if you just want to make it move like a normal arrow, roblox’s gravity will automatically do that assuming the arrow isn’t anchored

1 Like

Thanks for the tremendous help.

1 Like