How can I lock z axis on a part?

I have been developing a 2d game recently.
the game allow you throw a block.
but I have a problem that some times collision will cause it move on z axis and out of the map
Outofmap
ezgif.com-gif-maker

I try to use plane constraint to fix it but it will stuck at wall
Plane Constraint

I have seen a similar suitation like mine
Need help locking only z axis of a part
I followed the solution but seems like it’s not work now.
probably because bodygyro and bodyposition was deprecated?

I would appreciate your help!

3 Likes

u could make it so it checks if z axis is changed it just changes it back

1 Like

i believe something like this should work

local ZPosition = partdirectory.Position.Z
partdirectory.Position:GetPropertyChangedSignal("Z"):Connect(function()
	partdirectory.Position.Z = ZPosition
end)

if it doesn’t i can change it

you want to update the position after every physics simulation so we would use heartbeat
image

local runService = game:GetService("RunService")
part = ...

runService.Heartbeat:Connect(function(deltaTime)
	part.Position = Vector3.new(part.Position.X, part.Position.Y, 0)
end)

You could try using this:

local part = script.Parent
local initialZ = part.Position.Z
local delay = .1 -- How fast should the script check if the Z axis changed

while true do
	local posZ = part.Position.Z
	if posZ ~= initialZ then
		local posX = part.Position.X
		local posY = part.Position.Y
		part.Position = Vector3.new(posX, posY, initialZ)
	end
	wait(delay)
end


The script should be located inside the part.

I don’t know how to modift your script,

“part:GetPropertyChangedSignal(“Z”)”
this line causes two problem

image
image

prob cuz u forgot part.Position and Z is not a valid property of part itslef

It almost work!
use while true cause a extreme lag
so I use Heartbeat as a alternative.
the result has 3 problem
1.cause a weird collsion physic
noLockOrientation
I use the method in this thread to limit its Orientation
So the problem now is fix
How to lock rotation of a part?

LockOrientation

but it cause a new error

:arrow_forward: Maximum event re-entrancy depth exceeded for Instance.Changed (x1110) - Studio

2.break the block welding

I think I can use some Constraint like AlignPosition to fix it?

  1. When player touch the block, it shake rapidly.
    don’t sure which thing cause the issue.
    tremble

thanks for your reply.
I was frustated about that Roblox lack of 2d physic.
Finally I see a ray of hope!

Thanks for the information! I’ll try it.

never use while true without adding atleast wait() somewhere in there due to what i believe is while true actualy fires more rapidly than heartbeat

Yeah so I never use it. Render Step and Heartbeat are more reliable.

i agree i only use while wait when i want loops to actualy wake a second or something not to repeat rlly fast

Use a part only collide box to block them.