How would I go about making a raycast pad?

Hello, I am wanting to know how I can make a raycast pad that print("Stepped on") when a player steps on it, and print("Stepped off") when a player leaves the pad. I am wanting to do this on the client side so I can later have UIs pop up.

I know the basics of ray casting such as Ray.new(), :FindPartOnRayWithWhitelist(), etc, however, I do not know how to set it up properly.

In my final product I am wanting there to be multiple pads in a folder (ex. two shops in different locations) that can perform the same actions.

Any help would be greatly appreciated, thank you!

Instead of using Ray’s to detect when a pad is touched, why don’t you just use the .Touched, and .TouchEnded events? I guess I don’t really understand the purpose of using Ray’s in this case.

Raycasting at a extremely fast pace could be harmful to performance, especially if you are doing it in more than one place as you mentioned.

1 Like

.TouchEnded is very unreliable as it will call multiple times since players have different body parts, and it sometimes gets called at random times if a player jumps or even slightly moves off the platform.

1 Like

This is unfortunate true. To minimize the amount of processing power use happening, I would recommend using a .Touched event to detect when a player enters, and then either use Raycasting, or Region3s, to detect when they leave.

What exactly don’t you understand about a system like this? Are you just looking for some example code, to demonstrate how to incorporate Ray’s into a system like this?

1 Like

U can shoot rays from a humnoidrootpart like every 0.25 seconds and checking if he stepped in, to make it more efficient u can also use region3 to detect when a player us nearby

2 Likes

Also more hacky solution would be using floor material as a humanoid property, but thats obly in case ur button would be deifferent material than others in the region3

2 Likes

I’m mainly trying to figure out an efficient way to do it, for example, is it better to ray cast from the pad or from the HRP in the character?

Do you know how to achieve this? I’ve tried before in a local script and it didn’t work. Also, is it better to ray cast from the pad, up or from the HRP, down, in the character?

This depends on how big your pad is. If it is tiny, then you can just use one Ray going up from the pad. However, if it is bigger, you would need to use multiple Rays, or Raycast downwards from the HRP.

Honestly, if your pad is bigger in size, it might be better to use Region3s, and workspace:FindPartsInRegion3. This will allow you to detect if your character is inside a set amount of 3d space. They use a little more power than Rays, but would most likely end up being more consistent, since they are meant for 3d space operations, instead of single casts in one direction towards one endpoint.

1 Like

I have pads about 5x1x5 studs in dimension, and I’m interested in doing the ray casting from a character. In the future I am wanting to have 20+ pads so I feel region3 might be unnecessary in the situation.

In this case, you could do something like this:

Pad.Touched:Connect(function(Part)

if Part.Parent:FindFirstChild("Humanoid") ~= nil then --check to make sure touched object was a child of a character
  Character = Part.Parent
  --Pad is touched by a Character

  while true do
    Ray1 = Ray.new(Character.HumanoidRootPart, Vector3.new(0,-10,0)

    HitPart = workspace:FindPartOnRayWithWhitelist(Ray1, {Pad}) -- Raycast looking specifically for the Pad

    if HitPart == nil then
      break
    end
    wait(0.1)

  end
  --Character is no longer is above the part

end
end)
1 Like

Thanks, this helped a lot! I managed to figure out a solution using parts of the code you provided, very helpful!

1 Like