Animated Train Wheels

Hey, I have this train that follows a node system thats anchored, but I wanted to make the wheels spin. I tried multiplying the CFrame by CFrame.angles but it doesnt work because the entire train uses a qPerfectionWeld script to weld it all together. How could I rotate the wheels at a specific speed while still keeping the train on the tracks?

3 Likes

I would go for that.

local runService = (game:GetService("RunService"));

local wheel = (...); -- // Whatever.
local wheelsRotation = (0); -- // In Degrees.

local ROTATION_SPEED = (15);
function UpdateWheelsRotation(dt)
	wheel.CFrame = wheel.CFrame:Lerp( CFrame.new(wheel.CFrame.Position) * CFrame.Angles(0, math.rad(wheelsRotation), 0), dt * ROTATION_SPEED)
end;

runService.RenderStepped:Connect(UpdateWheelsRotation)
-- // I don't suggest having a RenderStepped event just for changing the wheels rotation. If you have any existing RenderStepped connection, I suggest changing the rotation there.

What is the dt variable for?

Should stand for “delta time.” Basically the time elapsed between frames, or about 1/60th a second(?).

Standard distance equation.
D = r*t

change in distance = rate * change in time (dt)

It just makes the train spin around rapidly, but I found a solution with attachments

As @GolgiToad mentioned, dt stands for delta time.

I gave it as an ideal way of making this work. The best ways to make it is through HingeConstraints or through Attachments like mentioned. But just make sure to set the network ownership so that it automatically replicates (in case you modify things on the client).

Im just gonna update it on the client via remote function, i found a way to do it with attachments

1 Like