¿How can i fire multiple RayCasts?

Hello developers

I’m making a cooking system

This is how it works right now:

as you can see, the collisions aren’t the best, the blocks can pass other blocks.

i think that the problem is that i’m only using one ray.
if the ray hits the upper ingredient, it will land correctly, but if the ray hits the last one, it will pass the upper one.

And i think that, by doing multiple rays i can check all the hits of the rays and pick the correct one.

I’m not good at raycasting, i did my first try yesterday so…

I hope this image help you to understand what i mean.

image

How can i achieve this and check which of the hitted parts by the ray is the upper one, so i can move the ingredient to that position?

If you need more info, tell me

Try using attachments! Create a grid of attachments on said part in studio, and create a function that casts a ray from each attachment in said part and handle collision from there.

This may be tedious but it would be a lot better than doing math magic and figuring out each possible point.

1 Like

You may be interested in WorldRoot:GetPartBoundsInBox and similar functions.

That’s not accurate enough for his purpose; you would have to constantly tweak and slowly shift the hitbox up until it stops registering any collisions

2 Likes

You may also be interested in Raycast Hitbox 4.01: For all your melee needs!, then :slight_smile:

Also if your object happens to have a complex shape I’m pretty sure this would not work either.

1 Like

Yes i saw that one, but i don’t actually know how to use it for this purpose

for now, i’m using only blocks, but in the future probably meshes or other type of shapes

Using math isn’t that tedious (maybe)

local raysPerQuadrant: number = 5 --imagine the surface of the patty as a grid
local raysPerSide: number = raysPerQuadrant * 2 + 1
local gap: number = ingredient.Size / raysPerSide --studs between each raycasting
local dir: Vector3 = -ingredient.UpVector * 50 --raycast direction; 50 studs range
local center: Vector3 = ingredient.Position - Vector3.yAxis * ingredient.Size.Y --bottom surface so it's subtracting

local highest: Vector3 = Vector3.new(0, -math.huge, 0)
for x = -raysPerQuadrant, raysPerQuadrant do
    for z = -raysPerQuadrant, raysPerQuadrant do
        local ray: RaycastResult = workspace:Raycast(center + Vector3.new(x, 0, z) * gap, dir)
        if ray and ray.Position.Y > highest.Y then
            highest = ray.Position
        end
    end
end

Should work in theory, didn’t test anything

3 Likes

Ok, but how can i make one function to make all the rays that i need and pick the correct one to land the block there?

I said tedious because of the possibility of other shapes making it even more annoying to code vs them only having to code a function that gets all attachments in object, and casts rays from them and checks if it hits whatever it needs. If it does, then it sets the Y value and is finished.

How i’m going to implement that to my code ;-;

That’s for you to figure out lol, I have no idea how your game is structured etc

1 Like

Ok i will give you the place so you can figure it out if you want ofc.

CookingSystem.rbxl (54.7 KB)

Somethings are in spanish but it doesn’t matter.

Check the script in StarterCharacterScript, please don’t ask why it is in the client, i’ll change it later