How would I make a BodyVelocity's Velocity change to slide along a wall when the part it's parented to hits it?

What do I want to achieve?

I want to make the character (being moved with a BodyVelocity that is created when you jump.) slide along a wall when it hits it.

What is the issue?

I’m not sure how to achieve this, and the reason I want to is because if it would keep the Velocity as it is the character would stick to the wall.

What solutions have I tried so far?

I looked for solutions for a while, I could find some things that helped but they didn’t fully solve my problem, the fix I am currently using works but only if the wall is facing a certain direction, otherwise it would reverse the direction of the jump.

Here’s how the code looks.

-- This is in a runservice.stepped event
local JV = HRP:FindFirstChildOfClass("BodyVelocity")
if JV then
	if string.find(string.lower(JV.Name), "jump") then
		local Jr = workspace:Raycast(HRP.Position, JV.Velocity.Unit * 2.5, RayParam)
		if Jr then
			local DotProduct = JV.Velocity:Dot((Jr.Normal:Cross(Vector3.new(0,1,0))))
			JV.Velocity = (Jr.Normal - Jr.Normal * -DotProduct)
			JV.Velocity = Vector3.new(JV.Velocity.Z, 0, JV.Velocity.X)
		end
	end
end
1 Like

To achieve sliding along a wall when the character hits it, you can use a combination of the Raycast() and BodyVelocity methods.

local JV = HRP:FindFirstChildOfClass("BodyVelocity")
if JV then
    if string.find(string.lower(JV.Name), "jump") then
        local Jr = workspace:Raycast(HRP.Position, JV.Velocity.Unit * 2.5, RayParam)
        
        if Jr then
            local DotProduct = JV.Velocity:Dot((Jr.Normal:Cross(Vector3.new(0, 1, 0))))
            JV.Velocity = (Jr.Normal - Jr.Normal * -DotProduct) * 0.5 
            local slideVelocity = JV.Velocity:Cross(Jr.Normal) 
            
            
            local BV = Instance.new("BodyVelocity")
            BV.Parent = HRP
            BV.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
            BV.Velocity = slideVelocity
            delay(0.5, function() BV:Destroy() end)
        end
    end
end
1 Like

This just stops all movement, I didn’t attempt creating a different bodyvelocity because that would be impractical, but I wouldn’t expect a different result aside from the character just getting stuck in the air aswell. Sorry. :slightly_frowning_face:

1 Like

Nevermind, I found a solution.
I used the method in this post, How to Cancel Out Vector Directions - #2 by Eestlane771, and subtracted it from the velocity.

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