Need help with moving parts on vehicles

Hey all,

I’ve been working on a small project for fun. It’s going to encompass forklifts to pick up crates and take them from point A to point B. I’ve successfully followed a vehicle rigging tutorial and my forklift drives around nicely.

However, I’ve hardly worked with vehicles and especially not moving parts on such vehicles. I can handle moving static things in a heartbeat, but moving things that are unanchored and constantly changing position is new to me. I’m stuck on securing a good method to move the lift on the forklift up and down. Not knowing where to start, I blindly typed out some atrocious code and at a glance it works but if my forklift is orientated at all the lift will drift out of place.

Code
UserInputService.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.E then
		raising = true
		while raising == true do
			lift.Position = Vector3.new(lift.Position.X, lift.Position.Y + 0.05, lift.Position.Z)
			wait(0.01)
		end
	elseif Input.KeyCode == Enum.KeyCode.Q then
		lowering = true
		while lowering == true do
			lift.Position = Vector3.new(lift.Position.X, lift.Position.Y - 0.05, lift.Position.Z)
			wait(0.01)
		end
	end
end)

UserInputService.InputEnded:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.E then
		raising = false
	elseif Input.KeyCode == Enum.KeyCode.Q then
		lowering = false
	end
end)

Result:

I understand why this is happening, I think I just need to move the lift not relative to the world. Though I still believe my method is garbage in a case like this and so I’d appreciate anyone willing to throw another method my way. What’s the best way to handle something like this, moving parts on vehicles?

Thanks in advance.

I would personally use constraints, and then control them through the script. So for the up and down motion you can use a prismatic constraint. If you want the lift to rotate on another axis while also going up and down on the first axis, then use a cylindrical constraint instead. Be sure to set Limits for the range of motion, and if you have trouble setting the torque or velocity, then circle back here for more help.

You can read up about both here:

https://developer.roblox.com/en-us/api-reference/class/PrismaticConstraint
https://developer.roblox.com/en-us/api-reference/class/CylindricalConstraint

But honestly YT has some really good tutorials. These constraints aren’t too hard to set up and are simple enough to control through script as well.

2 Likes

Set up a prismatic constraint between the “elevator” part and the lift and added a basic way to control it through UserInputService. This is definitely what I needed. As it actually uses physics it interacts with parts much better when picking them up and the lift is constricted if an anchored part gets stuck in it’s way. Thanks for the help.

Just thought it’d be worth mentioning this in case anyone has a similar problem; I did have some problems with weight at first. My vehicle is set up in a way where all parts are set to Massless = true and the mass of the vehicle is actually determined by one main part as to easily edit it and keep the center of gravity low. What I did not realize is that if a part with Massless = true is not welded to a part that has mass, the property will be rendered ineffective. As my lift part was now connected via a prismatic constraint, Massless was useless and my forklift would tilt forward so much it wasn’t drivable. So, with parts like my lift here that are connected via constraints and cant be welded, if you enable CustomPhysicalProperties and tweak the density of the object you can circumvent this issue.

2 Likes

Awesome im glad it worked, the video looks great! And yes the roblox reference for these really should mention using Massless as its not the first time ive heard people tackling that issue, but glad you figured it out. I also learned that one over the course of a looong night lol. FYI another tip with constraints is setting the tool mode to Physical will allow you to test its real physics in test mode when moving parts connected to the constraints.

2 Likes