How can you check if a part is grounded?

So now I’m making a molotov, but I need some way to detect if it is grounded before it explodes, so that it doesn’t explode when it hits a wall or something like that. Using :IsGrounded() on BaseParts doesn’t really work that well because the part has to be orientated a certain way to the ground for it to be passed as true. So, how can I do this?

I’ve thought of raycasting, but it could still theoretically explode on a part that isn’t at ground level if the raycast hits it. My only idea right now is to raycast and compare the hit parts’ positions Y position to a global ground vector (0, 0, 0) and then use magnitude to determine how far up it is and only explode it when the magnitude is below a certain number, but I’m not sure how effective this is. Any ideas?

Edit: Actually, my method wouldn’t work because I also want them to explode if let’s say it hit the top of a building. The position of the hit part would be too far from the ground vector

3 Likes

Raycasting should be fine for that, it would work normally.

1 Like

Okay but say I have this scenario:

How would I make it not explode if it hits the ledge of a building like this?

1 Like

What? So you want it to hit only the main part of the building? If its that then you can use the whitelist and whitelist only it.

1 Like

you could duplicate ground and roofs -
and check for collision with those parts where it can explode.
put them on their own collision layer.

1 Like

Actually I think I’ll raycast radially from the molotov’s position, and then detect if I get a hit from all of the raycasts to determine if the part is fully grounded. I think it’ll work

2 Likes

Yeah but then I’d have to whitelist every single edge case like that for my game. The map for the main game is huge and there’s probably a more efficient way to do it

1 Like

Why wouldn’t you want the molotov to explode when it hits a wall or ledge?
That would be realistic.
If you want it to only explode on certain parts why not name those parts “Explode” or something similar, then have the OnTouch signal fire only if the name of the Part it hits is Explode. That way it’ll bounce off all the other Parts and only go boom when it hits one of those ‘ground’ Parts.

2 Likes

It looks like an attempt at a CS:GO-style Molotov, because in the game if a molotov hits a wall it doesn’t break and spread fire, only when it hits the ground.

Although I do agree that it would be realistic if OP just let it explode nonetheless

2 Likes

Yeah. The reason why I’d want it to only explode when it hits the ground is so that the fire spreads correctly and actually has enough space to spread to look right. If it exploded on a wall or a small ledge, the fire would be floating. Also letting people bounce molotovs off of walls would lead to some interesting gameplay

1 Like

I think you should maybe check the name of the part or see if it’s in a walls/floor folder. That’s just how I would approach it.

1 Like

Just check if it touches a specific part that is on the ground. If you have multiple parts put them in a table and simply do a generic for loop.

1 Like

Actually the raycasting sounds like a good idea.

You can check if the surface normal of the ray cast, the top surface is 0, 1, 0 so if the surface normal is that then you have hit the top of something.

1 Like

At first this sounded like a really good idea, but when you’re building, especially a large map, a lot of parts are orientated differently depending on how the builder rotates it. So the wall to a building you see could actually be the part’s top surface, for all you know. So I’m not sure if this idea is really the safest.

I think I’m going to stick with my solution of radial raycasting downwards from the molotov’s position and wait until all of them are detecting hits to detect if the part is on the ground, it seems like the most efficient and safest solution right now. I’ma test it out later