How can I improve this code on player moving on slopes?

I’m currently trying to code a platformer game.So, I was trying to make the walkspeed change relative to the slope. Like when I’m running down , the walkspeed increases and when i’m going up, the walkspeed decreases.After doing some research, I made this :

and here is the code behind the system:

local char = game:GetService("Players").LocalPlayer.Character or game:GetService("Players").LocalPlayer.CharacterAdded:Wait()
local lowerTorso, rootPart = char:WaitForChild("LowerTorso"), char:WaitForChild("HumanoidRootPart")
local humanoid  = char:WaitForChild('Humanoid')

local params = RaycastParams.new()
params.FilterDescendantsInstances = {char}
params.FilterType = Enum.RaycastFilterType.Blacklist

game:GetService("RunService").Heartbeat:Connect(function()
	local ray = workspace:Raycast(rootPart.Position, Vector3.new(0, -char.Humanoid.HipHeight - 2, 0), params)
	if ray then
		--local vector = rootPart.CFrame:VectorToObjectSpace(ray.Normal)
		
		local dot = ray.Normal:Dot(rootPart.CFrame.LookVector)
		
	    
		--lowerTorso.Root.C0 = CFrame.new(0,-1,0,1,0,0,0,1,0,0,0,1) * CFrame.Angles(vector.z, 0, -vector.x)
		local walkSpeed = (16 + ((16 * dot) + 2 ))
		humanoid.WalkSpeed = walkSpeed
	else
		humanoid.WalkSpeed = 16
	end
end)

The thing I wanna do is improve the system like I saw on a post from polyhex. I’ll also link the vid .So u can understand . I tried to search it on devforum but I didn’t get my answers . Is there a way to make this better.?

the vid from twitter post:
https://twitter.com/tylermcbride/status/1044395746232074240

watch the full vid so u can understand the slope thing to increase player’s walkspeed.

2 Likes

In what way do you want to improve it? One difference in polyhex’s video I noticed was that the speed when walking up a slope decreases slowly like it’s being eased or tweened. What you could do is make it so that when you detect a slope your speed slowly decreases by a certain amount until it reaches the calculated speed value for that slope.

1 Like

so, should I use a boolvalue.? like when the value changes, It will trigger the change event and the speed will decrease or increase with the slope angle by being eased or tweened.? or should i use something else…?

improved the code. now it gives the angle of the slope which also considers the direction of the player’s facing direction…

Review this code and please let me know if the calculation is accurate or over 60% accurate…

local char = game:GetService("Players").LocalPlayer.Character or game:GetService("Players").LocalPlayer.CharacterAdded:Wait()
local lowerTorso, rootPart = char:WaitForChild("LowerTorso"), char:WaitForChild("HumanoidRootPart")
local humanoid  = char:WaitForChild('Humanoid')
local slopeDetectBool = Instance.new("BoolValue", rootPart)
slopeDetectBool.Value = false
local params = RaycastParams.new()
params.FilterDescendantsInstances = {char}
params.FilterType = Enum.RaycastFilterType.Blacklist

local walkSpeed = 0

slopeDetectBool.Changed:Connect(function()
	if slopeDetectBool.Value == true then
		print("True")
	end
end)

game:GetService("RunService").Heartbeat:Connect(function()
	local ray = workspace:Raycast(rootPart.Position, Vector3.new(0, -char.Humanoid.HipHeight - 2, 0), params)
	if ray then
		--local vector = rootPart.CFrame:VectorToObjectSpace(ray.Normal)
		
		local dot = ray.Normal:Dot(-rootPart.CFrame.LookVector)
		local angle = math.floor(math.deg(math.acos((dot / (ray.Normal.Magnitude * rootPart.CFrame.LookVector.Magnitude)))))
		print(angle) -- gives the angle of the slope in degrees
		--lowerTorso.Root.C0 = CFrame.new(0,-1,0,1,0,0,0,1,0,0,0,1) * CFrame.Angles(vector.z, 0, -vector.x)
		--walkSpeed = (16 + ((16 * angle) + 2 ))
		--humanoid.WalkSpeed = walkSpeed
	else
		humanoid.WalkSpeed = 16
	end
end)

Hi,
Where are you putting this script, so I can test it out?

Also, what happens when the player jumps up a slope ? Or down a slope?

Is the ray only looking 2 blocks away?

What happens if you jump off of something that is tall?