Raycasting weapon can shoot through walls

Hi, so I just built a sample wall for maps I might create, but I noticed a bug. Apparently, when the character is close to the wall, depending on your angle you can actually shoot through it.

Here’s a clip: https://gyazo.com/95345179dee150df65a6f54972fc4850

Are there any easy ways I can bypass this issue?? Thanks.

1 Like

I’m assuming you’re starting your ray at the tip of the barrel. If that barrel is inside another part and you cast a ray, that part that “should be counted” wont be counted since from my understanding, rays only impact outside faces.

You can solve this by shifting your origin of the ray backwards into the player’s shoulder (where the player’s collider starts) and projecting your ray from there. Make sure you include your player’s character into the ignorelist.

3 Likes

Hello C_Sharper,

Make sure you are checking to see that there are no parts between the players head and the barrel with raycasting. This will also stop shooting around corners.

Hope this helps, -HiddenKaiser

1 Like

Hi Locard,

If you are starting the raycasting further back, you might run into problems longer down the line. I feel as if the way I suggested may be easier but please respond if you can refute that.

Best regards, -HiddenKaiser

1 Like

I see, thanks guys!

Maybe I could cast another ray from the player’s head, in the player’s looking direction to a very short distance to determine if the player is too close to an object, and if that’s true, don’t allow them to shoot.

I’ll toy around with this. Thanks again!

1 Like

I’d like to hear these problems if you can state them! I don’t really thing there’s much changed with pushing the origin back. It’s also much less expensive than firing another ray within the same frame.

2 Likes

Hey, some of the problems I was thinking of is when the player shoots around the corner and instead of not firing, fires into a corner. I have seen this in some games result in team kill, especially guns with high damage or DPS. Another reason is bugs and glitches down the line, what if they have their arm inside a wall sideways and they fire, the gun and their shoulder are both in the wall and like you said, raycasting cannot pick up inner faces so the gun would shoot normally.
Combined with shift locking this could still result in wall shooting.

Raycasting from the head to the barrel means that they cannot do this as the head is collidable.

-HiddenKaiser

1 Like

Wouldn’t it just be fine to raycast from the HRP to the target instead then? You wont have to fire a second ray and since it’s inside a collidable it should function similar to how you stated it.

2 Likes

I’ll try it. Only thing I’m unsure of is how it’d affect the drawn bullet’s trajectory, if it makes it look awkward.

Thanks.

You don’t need to rely on this ray necessarily to draw your bullet btw

Just get the look vector from the barrel to the point that you hit with your ray, and then you’ll basically know the rest.

local hit,pos = workspace:FindPartOnRay(yourRay)
local lookVector = (pos - barrelTipPosition).LookVector
local cf = CFrame.new(barrrelTipPosition,barrelTipPosition + lookVector)

… should do the trick! Fill in the variables with what you think you should. :stuck_out_tongue:

1 Like