How to get the direction facing a slope?

I have a sliding mechanic I am working on in my game. When you start sliding your speed will decrease over time. However if you go down a slope the speed will increase again. Here is my code. (I haven’t added the speed increase yet)

slideactive = true
					running.Value = false
					attacking.Value = true
					slidetrack:Play()
					
					local direction = char.HumanoidRootPart.CFrame.lookVector
					local pos = Vector3.new(direction.X*maxslidevel,char.HumanoidRootPart.AssemblyLinearVelocity.Y,direction.Z*maxslidevel)
					
					repeat
						task.wait()
						hum.WalkSpeed = 0
						
						local SLIP_ANGLE = 20 -- slope angle
						local SLIP_COS = math.cos(math.rad(SLIP_ANGLE))
						local RaycastResult = raycast2(char, char.HumanoidRootPart.Position, Vector3.new(0,-5,0))
						
						if RaycastResult ~= nil then
							if RaycastResult.Normal.Y < SLIP_COS  then
								print("FOUND SLOPE INCREASE SPEED")
							end	
						end
						
						char.HumanoidRootPart.AssemblyLinearVelocity = Vector3.new(pos.X,char.HumanoidRootPart.AssemblyLinearVelocity.Y,pos.Z)
						pos = Vector3.new(pos.X*slidereduction,char.HumanoidRootPart.AssemblyLinearVelocity.Y,pos.Z*slidereduction)
						-- stun check air check etc
					until slideactive == false
					
					if stuntime.Value <= 0 then
						hum.WalkSpeed = 16
					end
					
					slideactive = false
					
					slidetrack:Stop()
					attacking.Value = false

https://gyazo.com/2fe1fa6cbabe303a07f8b74b4c78cb91
here is a gif of the sliding. As you can see in the gif the player can slide sideways on a slope which is unrealistic. I want to make it so the direction changes to the direction of the slope. How can I get the direction that the slope is pointing down towards?

Still clueless. I tried playing around with the raycast normal to get the direction but nothing I tried worked.

if RaycastResult ~= nil then
							if RaycastResult.Normal.Y < SLIP_COS  then
								direction = Vector3.new(RaycastResult.Normal.X,0,RaycastResult.Normal.Z)
								pos = Vector3.new(direction.X*maxslidevel,char.HumanoidRootPart.AssemblyLinearVelocity.Y,direction.Z*maxslidevel)
								slidetime = maxSlideTime
							end	
						end

This here seems promising but when the direction changes its slower.

still struggling with this. If anyone can help that’d be great :frowning:

If the character is in a slope, you can use CFrame.lookAt with its third parameter to rotate the character perpendicular to the slope normal. To avoid seeing weird glitches, insert it inside a RenderStepped and a Heartbeat.

local hrp = char.HumanoidRootPart
hrp.CFrame = CFrame.lookAt(hrp.Position, hrp.Position+hrp.CFrame.LookVector, RaycastResult.Normal)

Using Mover Constraints might work, but I’m not sure how considering how a humanoid is very hard to deal with using movers.

1 Like

This looks very promising. I am going to try this when I get the chance. Thank you for the help!
(I will mark you as a solution if it ends up working. If not I will leave another response)

Worked almost perfectly. Just had to change it up to this though

hrp.CFrame = CFrame.lookAt(hrp.Position, hrp.Position+hrp.CFrame.LookVector + Vector3.new(RaycastResult.Normal.X,0,RaycastResult.Normal.Z))
local direction2 = char.HumanoidRootPart.CFrame.lookVector
direction = Vector3.new(direction2.X,0,direction2.Z)
pos = Vector3.new(direction.X*maxslidevel,char.HumanoidRootPart.AssemblyLinearVelocity.Y,direction.Z*maxslidevel)

Thank you so much for the help. I was struggling with this more than I should have lol. Don’t know why I didn’t think of this.