Need help locking only z axis of a part

In my most recent project, I am re-creating certain aspects of the game Pro Gymnast which simulates… gymnastics and have run into an issue that prevents me from working on one of the main features of the game, the trampoline.

When doing flips in the game you simply hold a button to tuck in. The character goes moves on a 2d plane (x, y) while the rest of the game is 3d. I simply cannot find a way to keep the part(in my case) from tipping to the left or right and falling off.

Current issue:


issue of part falling off to the left

What I am attempting to do:

My current code is:

while true do

local x = script.Parent.Position.X

local y = script.Parent.Position.Y

local z = 3 --where the part is locked to for the z axis

wait(.01)

script.Parent.Position = Vector3.new(x,y,z)

end

I don’t know much about this and how to go about fixing it so I would appreciate some help. Thanks!

1 Like

It seems that BodyMovers would prove useful for the behavior you are attempting to achieve.
https://developer.roblox.com/en-us/api-reference/class/BodyGyro
https://developer.roblox.com/en-us/api-reference/class/BodyPosition

I do not recommend using BodyMovers for newer projects as they are deprecated. The equivalent AlignPosition/AlignOrientation has the same functionality but uses attachments instead.

Simply set the BodyGyro/BodyPosition’s axis to the desired locking Position/Orientation.

Feel free to play around with the rotation.


Diagram of rotational axis’s: (pitch, roll and yaw)

Here’s an example that I made up for all intensive purposes.

I do use BodyMovers, but old habits die hard…
BouncePart
Now with a little stress test on the Z axis, and a random forward direction on the X axis:

-- stress test parameters
local move = -math.random(5,10)
bounce_part.Velocity = Vector3.new(move,math.random(50,150),move)

stress3

BodyPosition: (merely set Z axis to desired origin point in space, and a really high MaxForce on the Z axis)
BodyGyro Script:

local gyro = script.Parent.BodyGyro
local FULL_PI = math.pi
local TWO_PI = FULL_PI * 2
local rotateFactor = FULL_PI*.25
rotateFactor = rotateFactor%(FULL_PI*0.5) -- ensure rotation does not go past half of pi
game["Run Service"].Stepped:Connect(function(delta: number)
	local sign = math.sign(script.Parent.Velocity.Y) -- direction (-1 || 1 || 0)
	local axisSpin = math.rad(script.Parent.Position.Y) * sign * rotateFactor
	gyro.CFrame = CFrame.Angles(0,0,axisSpin) -- specifying our angle axis's
end)

To change to bend limit, change rotateFactor to any number (MAX math.pi/2)

Model with Trampoline and Part from example images:
PartLockDevForum.rbxm (4.8 KB)

I hope that helps :smiley: !

3 Likes