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.