Shooting Through Walls

Hello!
I made a RayCasting gun, by using the default ray that somehow shooting through walls:

https://gyazo.com/efe6e45fb8c70d9279a653384d31d61a

Anyone knows why this keeps happening.

1 Like

You’re going to need to use game.Workspace:FindPartOnRay() to determine if you’re hitting a part other than your target (e.g. a wall) and then end the ray there.

1 Like

Your ray starts inside the wall

1 Like

Then how do I can fix this? :neutral_face:

Please post your code when you are asking for help. Review the guidelines for this category to learn how you should structure your posts.


I assume you are casting the Ray from the gun’s barrel or something. (Again no way to tell because no code) To fix this you should set the start point of the Ray to your characters Head, so the Ray does not originate inside of the brick.

7 Likes

I used default ray, the start position is from an attachment, and the end position is where the mouse clicked.

In this case I have assumed correctly and you should read my suggestion in the previous reply.

Can I have a link for that post?

I can’t use this cause my ignore folder will break.

local ignore = workspace.NoRay:GetChildren();
table.insert(ignore, char)
	
local part, position, normal = game.Workspace:FindPartOnRayWithIgnoreList(ray, ignore)

Does your ignore list include the wall? If so then that’s why the wall isn’t a part of your raycasting. Otherwise, maybe you should rescript how your ignore list works. Otherwise, @H_mzah brought up a valid point. Don’t raycast from the barrel of the gun, do it from the head. Then, if successful, create the bullet from the barrel.

Please be sure to read replies to your thread thoroughly so you don’t miss solutions.

7 Likes