Creating a 2D Platformer 'Rail' System

Greetings everyone,

I am currently working on a 2D platformer esque game that uses some simple code to achieve a 2D locked camera effect. Don’t get me wrong, it works just as intended and I could continue working on the prototype from there, however I thought of a way I could ‘one-up’ that approach by adding bends to the path. The player would hold ‘A’ or ‘D’ to walk in that direction, and when approaching a curve in the path the player would slowly adjust their rotation to walk on that curve perfectly and would adjust the position and yaw (angle) of the camera accordingly. I thought that the best way to accomplish this would be to create an invisible ‘rail’ that I would hand trace throughout the map, and then program the character to follow that rail back and forth matching the player’s ‘A’ and ‘D’ inputs! However only being a novice (intermediate if you’re being generous) Lua scripter, I am unsure of the best route to take. So I would like to hear your thoughts on what I should do. Having this come to my head I absolutley want to achieve it as it could really add some immersion to the project down the line, rather than just a straight-forward map.

I’ll attach my current code below along with a video so you can take a look at what I am using right now and feel free to snag it for your own projects!

This is a local script inside of starter character scripts.


--: Services
local playerService = game:GetService("Players")
local contextActionService = game:GetService("ContextActionService")
local runService = game:GetService("RunService")

--: Variables
local player = playerService.LocalPlayer
local camera = workspace.CurrentCamera

--: On-Startup
contextActionService:UnbindAction("moveBackwardAction")
contextActionService:UnbindAction("moveForwardAction")
camera.CameraType = Enum.CameraType.Scriptable
camera.FieldOfView = 80 --: Default "70"

--: Wait for Character Generation
local character = player.Character
local humRoot =  character:WaitForChild("HumanoidRootPart")

--: Lock Camera to HumRoot
function onFramePassed()
    camera.CFrame = CFrame.new(humRoot.Position) * CFrame.new(0,5.5,12) * CFrame.Angles(math.rad(-28),math.rad(0),math.rad(0))
end

--: Run Each Frame
runService.RenderStepped:Connect(onFramePassed)

(My apologies for the low FPS video)
robloxapp-20200606-2007391.wmv (1.0 MB)

1 Like

I just wanted to add a small side note, many people have been replying to me via Discord and saying I could use camera nodes to have the camera follow the path. However I am more focused on getting the character to follow that path rather than the camera. To be honest I would be okay with using a static camera angle however I really do want to accomplish this whole bending path ‘rail’ system with the character.

If you have any thoughts that could help me make this a reality, don’t hesitate to drop them down below!

I’ve been searching through different methods of completing this and have come up with nothing, please reply if you know anything that could help me out!

Try this: