In ROBLOX, how can I check what is in front of me?

In ROBLOX Studio I need to check what part is in front of the model Freddy.

I need to set the variable called thingInFront to the part that is in front of the model Freddy so that I can decide whether I should allow him to walk through or not. This is my current code:

thingInFront = nil
if thingInFront.BrickColor == BrickColor.new('Black') then
script.Parent.Parent.CanCollide = true
else
if not thingInFront.BrickColor == BrickColor.new('Black') then
script.Parent.Parent.CanCollide = false
end
end

• Hello, I just removed the greeting, signature, etc. from your post; those are not necessary (your name is on the post already, and we know you're here asking for help), and they actually detract from the important part of the post, which is the question. You can further edit your post or revise my edit. Aug 5, 2015 at 13:45
• And while you're editing, you should include much more detail. You linked to the general page of the engine you are using, but you didn't include any of your code whatsoever except for a couple of variable names. So there is no way anyone can help you unless you include at least some way to look at your current work. Aug 5, 2015 at 13:47

Just like to say that I have little experience in the lua language

BUT,

If you take the x y z position of your model "Freddy" and the x y z position of all your other bricks, from there you could find the distance between "Freddy" and the brick and whether the color of that brick is black or not. Then when "Freddy" is a touching distance from the brick, you can allow him to pass through it or not.

Hopefully this helps.

I know this respond is late but for whoever will check this I highly recommend using Dot product aka scalar product (math) to see if the instances is in front of your "Freddy" character. Dot product returns the scalar which represents the length of the vector calculated by taking 2 vectors and the angle between them.

Why ? Dot product is extremely cheap as its a simple calculation. It's also easy to use and fast to set it up.

-- Note

U should be aware of calculating it by using CFrame.LookVector (depending on your case) so it won't compute World position but the relative position to your character.

• This answer would be even better if it showed an example of a dot product computation and how we would use its value to determine which object is in front. May 28, 2020 at 15:11