Best way of how to make a beam "detect" someone

Hi, I have a beam for my npc and want it do detect if players who are in the beam. I do not know what the best way to have the most accurate reading however

1 Like

Look Vector would be handy. You could also use raycasting.

Raycasting would be the best way to do this.

How would I use raycasting, isnt it a small ray?

Check out this article.

Will the ray be like the beams width?

Raycasts have no width. They are an infinitely thin ray with finite length.

Is it possible I can make it wider to fit the beam?

Like I said, no. You can’t change the width of a raycast.

So what else can I do to make the beam as accurate

A raycast would be fine for this in my opinion.

You could also do 4 raycasts to sort-of make a rectangle that covers the entire beam.

Or you could use a region3 at the end of the beam. But that seems like a really crude way to do it imo.

If you’re not gonna use raycast, my only suggestion would be to make transparent massless parts that are the size/position of the beam, and write a function that gets the player’s humanoid based off who’s touching the massless parts.

Although that can be somewhat tedious.

Another way to do this is to create a ray from the npc’s head to the head of each player. The player is detected if nothing is obscuring the ray and the ray is within a certain angle from the lookvector of the npc’s head.

If it was for a player, just add a ImageLabel for such player and that’d be the “crosshair”, where everything would be detect. But since this is for a NPC, I guess you should use raycasting and that stuff. Or just make it so anything that touches the beam, if it is a humanoid then start damaging, or whatever you need. I do not know how to make it so it can’t see through walls, however. You should make it so if the object detected is a part or union and isn’t along with a humanoid object, then shorten the beam to the wall.

Raycasting. But you could also use,

  • Region3
  • :Dot()
  • Object.Touched

magnitude or overlaps and then drag the beam’s attachment using tween to the target