# "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()

clonedInstance:Destroy()
createdModel:Destroy()

return (size[axis.Name])
end

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
Limit

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.