How could I make a blockcast detect the center of the hitparts surface first?

So recently I have been experimenting with the feature shapecasts. For something I am making, I need the blockcast to detect the center of a flatsurface first. I don’t know a lot about how the raycast algorythm works. But from what it seems like, is that it casts tons of rays rather then 1, starting from the edge of the blockcast and then it moves along the x,z axis until it has casted rays across the whole part.There is a very good chance that I am completely wrong in this assumption but this is what my experimenting seems to tell me.

The issue is when I cast a blockcast torwards a flat surface, instead of raycastresult.Position to be the center of the targetPart, it is the edge of the targetpart. Is there perhaps a way in the scenario that the blockcast is casted on a flatsurface that it returns the center position instead of the edge?

This code casts a blockcast with the same size as the targetpart, and casts directly above the center of the targetpart
The bluepart is what I assume should be the size and position of the shapecast
The red ray is a visualisation of the initial position of the blockcast. And goes torward raycastResult.Position

local targetPart = game.Workspace.targetPart
local originCFrame = targetPart.Position + Vector3.yAxis*10
local size = Vector3.new(10, 1, 10)
local direction = -Vector3.yAxis
local raycastResult = Workspace:Blockcast(CFrame.new(originCFrame), size, direction * 10)
visualiseRay(targetPart.Position + Vector3.yAxis*10, raycastResult.Position)

Blockcast demonstration

I have managed to get this expected result by using spherecasts instead, reason why spherecasts get the center first is because the bottom center of the sphere will touch the targetpart first meaning it will detect the center first and return that as the hitpos. The reason I don’t want to use spherecast is because I believe it should be possible with blockcast, and I think it would be more effficent to do this with blockcasts then spherecasts, because of the less triangles etc.

local originCFrame = game.Workspace.testing.Position + Vector3.yAxis*10
local size = Vector3.new(10, 1, 10)
local direction = -Vector3.yAxis
local raycastResult = Workspace:Spherecast(originCFrame, 5, direction * 10)
visualiseRay(game.Workspace.testing.Position + Vector3.yAxis*10, raycastResult.Position)

Spherecast demonstration(What i want)

1 Like

I might have an idea where i can check if the part that was hit has 0,0,0 rotation and if its a flat part, my concern is meshparts though, because is there any way to check if the meshpart that was hit has a flat surface or?

What if you did the blockcast, then rotate the block 180 degrees and blockcasted again and averaged the position of the two results? That way if the hit part was flat, the two results would be at opposite corners and cancel each other out to give the center.

1 Like

I don’t understand why you want to use blockcast, the performance difference between spherecasting and blockcasting is barely noticeable and if spherecast solves your issue then just use spherecast.

1 Like

Problem is spherecast use radius, so some part of the part will be missed.

This gave me an idea thank you. I came with a simple solution. What I will do is cast a blockcast, and then cast a ray to the center of the part. If the rays have the same height, then it means its a flat surface, and it will use the center position instead.

here is the code for anyone that stumble upon the same issue, I believe it should not have any issues.

local function castBlockCenter(castOrigin : CFrame, Size : Vector3, direction : Vector3, agentParams)
	--cast block ray
	local blockCastResult = game.Workspace:Blockcast(castOrigin, Size, direction, agentParams)
	if not blockCastResult then
		return nil
	end
	--Cast ray at center, if same height as blockCastResult, then ray is pos
	local rayResult = game.Workspace:Raycast(castOrigin.Position, direction, agentParams)
	if rayResult and blockCastResult.Position.Y == rayResult.Position.Y then
		return rayResult
	end
	return blockCastResult
end

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