Difficulty with raycasting through an object

I’m looking to raycast through an object, to then find the ‘exit point’ of that ray. This would be compared to the original hit point, to then measure out the thickness of the part the ray went through.

what i want to do:

my current approach:

while this does work, this has other complications which the first approach would fix, plus this requires quite the long raycasts. (which in my case are not beneficial)

2 Likes

Casting an inverse ray does seem like the best way to accomplish this. The only other thing I can think of would be to use Ray.ClosestPoint() to calculate that specific position on the ray. Depending on how you’re designing this system, ClosestPoint() might also be impossible. . .

1 Like

Can you elaborate? I’m not sure how ClosestPoint could work in this case.

1 Like

Honestly, I’m not sure. I was thinking something where you use the part’s size to get the X cord of the exit face, and then loop through Y and Z values until you find the position on the exiting face that is closest to the ClosestPoint() on the ray.

If you’re planning for this to be used in weird orientations it would get really messy.

You’re original method of an inverse ray seems like a good idea. If you don’t mind me asking, what is the problem with using that method?

I’m trying to implement wallbanging. Can’t use part sizes since that is a very unreliable way, especially if a shot came from an odd angle. Raycasting with the first approach would work wonders for this, and the code would be a lot more mess-free.

The inverted rays are long and there would be some issues with say a lot of thin parts stacked behind one another. I mean, it’s possible to work around this, but the better and simpler solution is the first approach. Performance-wise, the first approach is also better.

The problem is that the first approach might create to much of a mess in your code to be effective.

Can’t you whitelist the one part you’re trying to hit and blacklist all others?

Like you have a part that is like a wall and other parts are in front of it … so you’re looking to ray through them other parts to get the bounce off the wall, right?

if so can’t you just not use the parts in from by listing them out of the ray.

1 Like

I’m not sure how it would cause too much of a mess, when this is what most games use for their wallbanging. (as far as i know) Rather the second approach (inverted rays) requires more code workaround.

This was my first idea , but this has proven uneffective, or I just did it wrong.

I’m not sure what you mean, can you elaborate?

looks like JamminRedPandaMan knows way more about rays than me …

Raycasting is only performance heavy at long ranges, try setting the second raycast on the otherside’s lenght to the Part’s size * 1.5, put it’s origin to the first raycast’s hit position + (the lookvector * part size * 1.5), this will ensure it can be shot from any angle, oh and don’t forget to use whitelist params on the second raycast.

to put it in code:

local firstRay = workspace:Raycast(origin, direction, params)
local maxInstanceSize = math.max(firstRay.Instance.Size.X, firstRay.Instance.Size.Y, firstRay.Instance.Size.Z)
local reversedRay = workspace:Raycast(firstRay.Position + (direction * maxInstanceSize * 1.5), whitelistedParams)

local wallDepth = (firstRay.Position - reversedRay.Position).Magnitude

Alright, so what if you simply looped through the positions on the ray, starting at the position where the ray hits the wall. Using this code, you can loop through the points on a ray.

local raycast = workspace:Raycast(script.Parent.Position, script.Parent.CFrame.LookVector * 10000)

for i = 1,10000 do
	local part = Instance.new("Part")
	part.Size = Vector3.new(0.5,0.5,0.5)
	part.Color = Color3.fromRGB(255, 2, 2)
	part.Position = script.Parent.Position + (script.Parent.CFrame.LookVector * i)
	part.Anchored = true
	part.Parent = workspace do
	
	if isPositionInsidePart(part.Position) == false then
		return part.Position
	end
end

Just make sure these values line up.

That allows you to follow the points on a ray.
image
Then you could continue to loop until the position you’re checking is outside of the part. You’d have to code the isPositionInsidePart function. I’m sure you can find another devforum topic on accomplishing that if you need to.

That is a way you could possibly accomplish the first method. However as @karlisjarl said:

You should be able to use the second method just as easily if you use distance and raycast params effectively.

1 Like

This method should in theory also work, it just doesn’t give 100% accurate results. It will also be even more performance heavy if you use it on such a huge scale.

1 Like

Both valid points, which is why the double raycast still seems best imo.

Yeah, 10k parts is crazy ngl. I just managed to work around the inverse ray method. I guess if there is no way to do the first approach with raycasting through the part, ill have to stick with what i got.

You wouldn’t have to do it with parts. I just used them to demonstrate what I was trying to explain.

Here’s what it would look like using only Vector3 values.

local function calculateExitPart(raycastResult)
	for i = 1,ray_length,0.1 do-- 0.1 increments for more accuracy
		-- Raycast.Position is the point where the ray connected with the wall
		local position = raycastResult.Position + (script.Parent.CFrame.LookVector * i)

		if isPositionInsidePart(position) == false then
			return position-- returns exit position
		end
	end
end

Edit:

The method I just explained is still going to be slightly inaccurate, so I would stick with what you’ve got if it’s working.

Yeah, i mean it works fine, its just the extra rays which can be problematic for performance… but oh well. thank you for your help!