"Inverted" raycast

Suppose we had a ray inside of a part. We cast that ray, and it would eventually leave the bounds of that part. I need to know the position of where this happens. Is there a type of ray that does what I described ?

Look at the diagram below. The shape is the part.

What shape is the part going to be? Is it going to be arbitrarily shaped like the image?

Yes, possibly. So I don’t think you can cast a ray outside of the part facing the direction of the point we want to raycast to. Something else from the shape, so maybe an overhang like in the image, could perhaps be in the way of the ray.

AKA. The part we’re colliding with is concave

1 Like

This is the simplest solution to your problem I could think of that doesn’t involve tons of math:

  1. Make a huge part that covers the entire map of your game, name it air if you want. Make it invisible and anchored, and turn CanCollide and CanTouch to false but leave CanQuery to true
  2. Make a raycastParams and add only the air part to the FilterDescendantsInstances table
  3. Set the FilterType of the raycastParams to Enum.RaycastFilterType.Include
  4. Remember to include the raycastParams to the workspace:Raycast

This way the raycast will only be able to hit the air part, giving you the position of its bounds

Drawback to this system: You’ll need to remember to exclude the air part in other raycasts else they will always keep hitting it

If the air part covers the entire map, so it also covers the part i’m casting insdie from, then all rays with these parameters will immediately hit airpart when casted…

1 Like

That’s true, didn’t think of that but I’ll see if I can find a way that works

Nevermind, i’ll just go with the compromise solution of casting outside of the part. You can still tell me if you have better ideas

By the way, what do you need this for? If it’s for a hitbox you can use workspace:GetPartsInPart() inside of a loop (I recommend RunService loops) to get all the parts that are inside of your part no matter how it’s shaped

I need this to measure the width of a shape at a certain direction. Look at the original diagram, the ray is coming out of the center of the part. You’ll see what i’m using that for shortly, i’m still sorting things out. I’ll let you know

I think this topic I found should be helpful for that:

1 Like

Here is something hacky I put together to find the size of an instance given an axis.

local function returnAxisWidth(instanceToCalculate: Instance, axis: Enum.Axis)
	local clonedInstance = instanceToCalculate:Clone()
	local createdModel = Instance.new("Model")
	clonedInstance.Parent = createdModel
	local _, size = createdModel:GetBoundingBox()
	return (size[axis.Name])

print(returnAxisWidth(workspace.Baseplate, Enum.Axis.X))

(this size is not always accurate, it is just an approximation)

That’s exactly what I’m doing

If it works correctly and reliably, then why change it for a different system that might give you a less accurate result?

The original thing I proposed with “raycasting air” is impossible it feels like and I didn’t continue
So that’s why i’m raycasting outside, from both sides, now

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.