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.
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?
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.
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
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
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.