What is a reliable way of detecting surface normal collision using Raycast?

I am creating a custom character controller with a capsule collider.
I want the capsule collider to bounce/reflect off the wall based on the angle of where the capsule touched.
I’ve effectively achieved it by casting a ray 360 around the player and getting the normal to reflect the ray, here’s a snippet (can’t show much):

-- reflect function
local function reflectVector(vector: Vector3, normal: Vector3)
	return vector -2 * vector:Dot(normal) * normal -- r = v - 2n(v ⋅ n) * n
end
-- radial cast
for angle = 0, 360, 1 do  -- ik it's not good for performance
   local orig = rootpart.Position 
   local dir = (rootpart.CFrame * ang(0, rad(angle), 0)).LookVector  
   local result = Raycast(orig, dir) 
   if result then 
      reflectVector(moveDir, result.Normal)  
   end 
  end 
 end

It does work but it is not reliable, as the ray is also cast in a plane axis and sometimes it won’t even detect collision.
I’ve been thinking of utilising Spherecast but I have no idea how would I cast a sphere in the same position.

1 Like

Are you sure the rays are being cast the way you want them to?
Maybe try visualizing them using parts.

You can get the normal of a surface through a ray cast like you’re doing already. I’m not fully sure what you want to do. Does the capsule move in the direction of ‘reflectVector’?

The issue might be that the amount of rays is interfering with movement. I can imagine multiple rays would detect hits. How are you handling movement?

I am visualizing them using CastVisuals and I can confirm the ray is cast 360 around the player.

The capsule is welded together with the character.

I had encountered this issue before and what I did was add a debounce after every hit ray. But still, I fear that this isn’t a reliable way.