Keeping the player in a straight line in a 2D game

Hey there! I’m currently making a 2.5d platformer game

I’ve ran into an issue where I cant keep the player on a straight path throughout the game, if the player hit A object they are moved out the desired place.

I’ve looked a the dev forums for this issue, But there doesn’t seem to be many solutions, I’ve tried using invisible barriers but those seem impractical and tedious to make for each level in the game.

Please let me know if there are any solutions to this. I’ll be happy to example in more detail if you are still confused.

Constantly run Humanoid:MoveTo() (and yes it does work since the ClickToWalk tool I made does). I know exactly what you mean here.

Additionally, you can constantly set the CFrame and bind it to RenderStep.

For the “Line runner” template, I modified the ControlModule script so that it moved it in the correct X axis (which was -torso.Position.X)

Hey there! Would this cause lag? I should probably add a wait() to the Renderstep, but surely constantly running Humanoid:Moveto() would cause lag.

Maintaining performance is fairly trivial.

while true do
	Humanoid:MoveTo(Vector3.new(0, 0, 0)) --Example position (this would change periodically).
	Humanoid.MoveToFinished:Wait()
end

2D Game:

Look into disabling keys.

Disabling controls wont stop the player from getting pushed by objects, though you definitely do need this if you’re making a 2.5d platformer game.

I myself just set the player’s X or Z position to be stuck on one value. I used a renderstepped function and then if the position has changed, set it back to what you want to lock the value to. This way your character wont jitter around too much because it wont have to set the cframe EVERY renderstep. To make it more smooth you should maybe first do math.round() on the axis of the player and THEN check if it’s not the same as your lock position.

task.wait(2)
local Character = script.Parent
local Root = Character:WaitForChild("HumanoidRootPart")

local BodyPosition = Instance.new("BodyPosition", Root)
BodyPosition.MaxForce = Vector3.new(0, 0, math.huge)
BodyPosition.Position= Vector3.new(0, 0, Root.Position.Z)
BodyPosition.D = 0

put inside StarterCharacterScripts

5 Likes

Yeah, true point. Humanoids maintain performance and are quite good when on the client. The issue comes when the humanoid is actually moving. I recommend modifying the control module to change the Humanoid:Move() vector’s X axis to (-torso.Position.X). This would fix your issue entirely.

BodyPosition is deprecated I believe. You are also not parenting the instance properly.

But I’d consider this a quick fix.

Hey there! I know its been a while but I need a more permanent fix, anybody have any suggestions?

What I did was in the control script I used Humanoid:Move() like what I said in my ancient previous post:

So, in the movement script, you need to add that into your X axis of movement. If you don’t have one, you need to create it.

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