Scripting a bullet wallbang with raycasting

I’ve looked at different posts on how to script a wallbang with raycasting. If you don’t know what a wallbang is, it’s a feature in shooter games which allows bullets to travel through thin objects or weak materials (imagine a bullet going through a piece of wood that’s half an inch thick)

The only post that I could find which made sense to me required 3 raycasts:

Raycast number 1:


Detects the wall that the player shoots at, and the 3D position where the bullet enters the wall

Raycast number 2:


Uses TargetFilter on raycast parameters to filter out the wall, allowing the raycast to travel through the wall

I would need to detect where the bullet leaves the wall (I don’t know how to do that) and then find the distance between where the bullet enters and leaves the wall to figure out if the distance is too far for the bullet to wallbang

Raycast number 3:


Actually shoots the bullet (which damages players, creates a bullet hole, etc.)

This requires 3 raycasts per bullet which I could imagine being very performance heavy when the player is using a gun with a fast fire rate (P90 shooting about 1,000 bullets per minute)

Is there a more efficient way to script wallbangs?

1 Like

I have a suggestion for how you could accomplish wallbangs. Suppose you have a normal gun that shoots out raycast 1…

Raycast 1 will be your average gun shot raycast. If it hits someone, it hits. But, if it hits a wall instead, then raycast 2 comes in:

Raycast 2 will fire exactly the same as raycast 1, but ignore all parts except player parts. If this raycast hits, then this means the gun shot has potential to wallbang someone. So, raycast 3 is fired.

Raycast 3 fires directly in reverse from raycast 2, starting from raycast 2’s hit location and going towards its origin, also not ignoring walls. Raycast 3’s hit location will be your key to figuring out the wall thickness: Simply the distance between the hit locations of raycasts 1 and 3.

Short version:
Ray 1: Fires as normal. If wall hit, fire ray 2
Ray 2: Fires like ray 1 ignoring walls. If player hit, fire ray 3
Ray 3: Fires as the reverse of ray 2, does not ignore walls. Will hopefully hit a wall, so the wallbang thickness will be the distance between the hit locations of rays 1 and 3.

This method does not account for the bullet going through two thin walls spaced far apart.

In terms of performance, I don’t think it’s that much of a problem. Consider when all three raycasts will happen:

  1. A player needs to be active and participating in the game.
  2. A player needs to be shooting for one reason or another. (One raycast)
  3. A player needs to be shooting at walls. (Two raycasts, but this is more likely to happen than one raycast)
  4. A player needs to be correctly aiming at someone through walls. (Three raycasts)

Not only that, but only some of the players will be using the fastest firing weapons. The other players will be using slower weapons, semi-automatic weapons or weapons that don’t even use raycasts. Additionally, shooting in FPS games is always in short bursts (in my experience) because enemy encounters usually end very quickly, so raycast calculations shouldn’t be too taxing.

And finally, a complementary diagram:

6 Likes

There are still a few issues, but seeing as you’re the only person who has responded or even tried to help in the last 7 hours, this serves as a good solution for the time being.

Your message was very clear and easy to understand. One last thing that would help, if I use RaycastFiltering on the wall that the player shoots at then their bullet will go through that wall. Is there a way to detect where the ray leaves the wall?

1 Like

To my knowledge, there is no way to know where a raycast exits a part. If I had known of such a method, I would likely incorporate it into my post and have it use 2 or less raycasts. It might be possible with some weird trick, but such a thing is beyond me.

1 Like

Hey, it’s me again. I think I figured out a way to detect where the ray leaves the wall, I figured it could potentially benefit you. Pretend the white square is the origin of the first ray, and the red line is the first ray.

Once the first ray hits, I could make another ray on the other side of the wall, and inverse the direction of the first ray. Basically what this is doing is shooting another ray from the other side of the wall to wherever the first ray hits. This allows you to detect where the bullet leaves the wall

From here, you could calculate the distance between where the bullet enters the wall (first raycast’s hit position) and where the bullet leaves the wall (second raycast’s hit position) with .Magnitude and then see if the distance is too far for a wallbang to occur.

Of course, you can use TargetFilter to filter out other parts in the workspace which you do not want the second ray to collide with.

3 Likes