How do I add adjusting player to tilt to slope and momentum to my game?

Im trying to make sonic inspired slope momentum, like in this game I found called “Roly Boi Playground” and adjusting player tilt to slopes, but im not sure how I have tried but im new to scripting kinda and need some help making this for regular momentum heres what i have so far. :grinning:

local accelerateRate = 10
local decelerateRate = 500

local max = 100
local min = 16

local plr = game.Players.LocalPlayer
local chr = plr.Character or plr.CharacterAdded:Wait()

local rootpart = chr:WaitForChild("HumanoidRootPart")
local humanoid = chr:WaitForChild("Humanoid")

humanoid.MaxSlopeAngle = min
humanoid.WalkSpeed = min

local lastPosition = rootpart.Position
game:GetService("RunService").Heartbeat:Connect(function(dt)
	if humanoid.MoveDirection ~= Vector3.new() then
		humanoid.WalkSpeed += dt * accelerateRate
		humanoid.MaxSlopeAngle += dt * accelerateRate
	else
		humanoid.WalkSpeed -= dt * decelerateRate
		humanoid.MaxSlopeAngle -= dt * decelerateRate
	end

	lastPosition = rootpart.Position
	humanoid.WalkSpeed = math.clamp(humanoid.WalkSpeed, min, max)
end)

I have TRIED EgoMoose wallstick but I do not know how to make it momentum based any help is appreciated!

2 Likes

How does your current code work? What seems to be the problem with it?

Could you describe what effect you’re looking for?

I’m looking to create a system where the player can reach a maximum speed of 75 on flat ground, but can exceed that speed when going down slopes. Right now, the code limits acceleration to that 75 speed and doesn’t take advantage of the additional speed from downhill movement. Feel free to tweak the code as needed; I’m open to any improvements! also reply if this doesn’t make sense I don’t want to come off rude :slight_smile:

Currently your code also controls the slope that the player can walk up. Is this also something you’re looking for?

Is controlling just the WalkSpeed fine?

If so, you can just do a downwards raycast, then you can get the angle of the ground below using:

local function projectOntoPlane(v, planeVec1, planeVec2)
    -- Compute the normal of the plane
    local normal = planeVec1:Cross(planeVec2).Unit

    -- Compute the projection of v onto the normal
    local projOntoNormal = normal * v:Dot(normal)

    -- Compute the projection onto the plane
    return v - projOntoNormal
end

local function getSlopeAngle(raycastNormal, moveDirection)
    local up = Vector3.new(0, 1, 0)
    moveDirectionRelativeSlope = projectOntoPlane(raycastNormal, moveDirection, up).Unit
    return math.acos(moveDirectionRelativeSlope:Dot(up))
end

then change your max walk speed to be 75 + [some function of the slope angle] and use your acceleration code above.

I don’t know too much about ray casts because my small pea brain can comprehend them, BUT I’m going to start reading the article to understand more so I can tell if something doesn’t count as a solution. You are a natural thank you :slight_smile: also, where do I implement this in my other script

Raycasts send a probe in a line from a starting position in a direction (the direction is equal to the final position minus the starting position). It detects the first surface in the way of the “ray” that it sends out.

You would do a raycast starting at the HRP down maybe 3 studs.

You would calculate the slope in the Heartbeat connection and use it to max out your WalkSpeed (e.g. maxWalkSpeed = 75 + math.deg(slopeAngleFromFunction) / 90 * 50 -- gets a value of 75 for flat ground and 75 + 50 for vertical ground and etc).

yay I did it thanks to you : ^ D