I am currently writing my own character controller, and first I am implementing the collider with raycasting. I have this cylinder that can collide with walls and floors and can move around with a velocity vector. What I am trying to do now is make the collider slide a long a wall it’s currently colliding with. So if you were running into a wall that was a bit rotated, or you were a bit rotated, you’d begin horizontally sliding along the wall.
I cannot get the correct projected vector for achieving this. The Unity engine has a built in Vector function to deal with this sort of projection. Vector3.ProjectOnPlane, of course Roblox lacks this sort of method, so I’m attempting to write my own implementation of it. It works perfectly fine when the walls is axis aligned, but as soon as I rotate it, it gives me the wrong vector.
I’ve looked up many solutions to this issue, I found the math behind Unity’s implementation of this method, so I implemented it as such:
local function SurfaceProjection(v,n)
-- v is direction of movement, n is the surface normal of the wall
local num = n:Dot(n)
local project = n * v:Dot(n)/num
return v - project
end
This results in this behavior shown in the video. I also looked up the math behind projecting a vector, and I implemented it as such, and it still does the same thing.
local function SurfaceProjection(v,n)
-- v is direction of movement, n is the surface normal of the wall
local right = n:Cross(vert) -- vert == Vector3.new(0,1,0)
local up = n:Cross(right)
return right*(right:Dot(v)) + up*(up:Dot(v))
end
I’ve seen one single dev forum post that mentions this but doesn’t go into how it was done.