How can I force objects to only be able to move in two axis?

I am making a 2D sandbox game that involves physics, a sandbox of some sort and I want to make it so that objects can’t go forwards and backwards. I already have an AlignOrientation object to avoid it from orientating improperly with physics, but the objects still bounce out of the proper Z axis, making them incapable of interacting with other objects again. I tried AlignPosition objects but they make the objects violently try to phase through other objects to go to a specific position.

TL;DR: I need to make multiple objects be forced to only be able to traverse the X and Y axis.

(EDIT: pluralized)

1 Like

Maybe use prismatic constraints? The following should help:

It stops the part from rotating though, so that may or may not be a problem. If prismatic constraints don’t work, you could add invisible parts to keep it from moving around.

Adding onto what @GamerGamer0o0 said, if you don’t want to do either of those then you can also use a script to do it.

local RunService = game:GetService('RunService')
local part = -- define part here (ex: workspace.Part)
local lockedAxis = 'x' -- the axis that will always stay 0

RunService.Stepped:Connect(function()
    part.Position *= Vector3.one - Vector3[`{lockedAxis}Axis`] -- xAxis, yAxis, or zAxis
    
    --[[
    The line above is basically the same as doing:

    if lockedAxis == 'x' then
        part.Position *= Vector3.new(0, 1, 1)
    elseif lockedAxis == 'y' then
        part.Position *= Vector3.new(1, 0, 1)
    elseif lockedAxis == 'z' then
        part.Position *= Vector3.new(1, 1, 0)
    end
    ]]--
end)

Here, the Stepped event runs every physics frame. Then we can set the position of part so that it will always stay in lockedAxis.

Sorry if I didn’t really explain well, I’m not very good at explaining stuff.

1 Like

Forgot to mention that, since this is going to be a sandbox game, it is not one object that has to be limited to two axis, it’s multiple objects. so I may utilize your solution of just using a physical barrier for this.

You need a PlaneConstraint. That’s it. Nothing special.

2 Likes

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