Raycasting going through solid objects

So I am creating an RCL gun system, but I ran into an issue with raycasting. Sometimes, the rays will go directly through the part and not register it even exists. An example of this can be seen here.

If anyone knows how to fix this please let me know!

My code for raycasting:

func.Raycast = function(origin,endpoint,ignoreList)
	local params = RaycastParams.new()
	params.FilterType = Enum.RaycastFilterType.Blacklist
	params.FilterDescendantsInstances = ignoreList
	params.IgnoreWater = true
	
	local cast = workspace:Raycast(origin,endpoint-origin,params)
	return cast
end
4 Likes

Just a thing to ask you:

What is the ignorelist you passed (we dont know how you made the content on it)

And by the way, the second argument for :Raycast() shouldve been (endpoint - origin).Unit multiplied by how far you want the script to detect it (like how many studs away from the origin)

The ignorelist I use with this is just a folder in workspace called Rays and the player’s character.

What is the content inside the folder thats not the character then?

You can create a hit function and if it’s a BasePart add the ray to a debris folder

It’s just an empty holder for bullets.

have you printed cast we need to know if it’s even detecting anything…
meant for @Madonox

Oh yeah, can you make it print the objects it hit? Maybe it does detect but the ray extends till your mouse so it goes through.

Yes. I have printed the results and it returns nil mostly. Madonox guns - Roblox Studio (gyazo.com)

Yeah just to understand, why are you substracting endpoint by origin?
and can also tell me what they are…

1 Like

I think it was supposed to detect the distance between the two points but as I said before, it shouldve been made with Unit since it needs a unit direction to work as intended.

I’ve changed it to unit and the issue is still happening. Also, the origin is the position of the blue block and the endpoint is the mouse’s hit position.

Yeah but I don’t really like to assume sometimes we do things out of consciousness. Endpoint might have already been solved by (destination-origin).Unit

How do you get mouse hit position in your case?

I get the mouse position from mouse.Hit.p.

I hate this stupid quick keybind

Are you sure that you haven’t set the Part up inside your ignoreList?

Yes, the part is in the workspace. It is not in the ignore list.

Ok, so from what i can tell if thats endpoint then you are calculating you’re ray direction wrong.

destination = (endpoint-origin).unit * distance -- include a DISTANCE

replace endpoint-origin with this variable

Since you didn’t give a distance it returned nothing because it hit nothing…

Alright, let me test it out now.