Im trying to make rotating wheels for my train based on it’s velocity and I have made progress but Im stuck.
local script inside wheel:
local RunService = game:GetService("RunService")
local wheel = script.Parent
local velocity = script.Parent.Parent.Parent.CoachB.noot.Velocity.Magnitude
local function MoveWheel(Wheel) --Moves the wheel in accordance of an attatchment due to the fact the train moves.
local newCFrame = Wheel.CFrame
newCFrame = newCFrame - newCFrame.Position + script.Parent.Parent.BogieFrame.FrontWheel.WorldCFrame.Position
Wheel.CFrame = newCFrame
end
RunService.RenderStepped:Connect(function()
local rotation = CFrame.Angles(0,0, math.rad(2))
wheel.CFrame = wheel.CFrame * rotation
MoveWheel(wheel)
end)
“math.rad(2)” should be calculating the rotation based on the velocity but im unsure on how to do that. Also when i join the game after 2-3 seconds the wheel disappears, vanishes from the world, I dont visibly see it falling through the world due to unanchored issues.
You should be able to rotate it by velocity very easily, though tweaking the numbers will be up to you. I’ve put / 12 but you will need to try it out and lower the number as you need it.
local rotation = CFrame.Angles(0,0, math.rad(velocity / 12))
The other issue you will have is that you’ve calculated the velocity at the start of the script and saved it in a variable, this needs to be recalculated once per render stepped
local RunService = game:GetService("RunService")
local wheel = script.Parent
local Coach = script.Parent.Parent.Parent.CoachB.noot
local function MoveWheel(Wheel) --Moves the wheel in accordance of an attatchment due to the fact the train moves.
local newCFrame = Wheel.CFrame
newCFrame = newCFrame - newCFrame.Position + script.Parent.Parent.BogieFrame.FrontWheel.WorldCFrame.Position
Wheel.CFrame = newCFrame
end
RunService.RenderStepped:Connect(function()
local velocity = Coach.Velocity.Magnitude -- recalculating velocity
local rotation = CFrame.Angles(0,0, math.rad(velocity / 12))
wheel.CFrame = wheel.CFrame * rotation
MoveWheel(wheel)
end)
Is this an anchored train moving by CFraming it?
If not you could just use a HingeConstraint but instead of Motor or Servo set it to None. The wheel should just drag on the track and rotate without scripting.
If the train is Tweened (an anchored Part that is tweened, with all the other Parts welded to it a HingeConstraint should work.
If you didn’t want the wheels touching anything you could set the HingeConstraint to Motor and rotate the Hingeconstraint at a value that would spin it in relation to the speed of the train movement.
Seems to me if you are using physics, the wheels should spin on their own, if they are attached to an axle with some friction. I had trouble getting my cart wheels to spin at first, but it was due to collisions.
I Haven’t tried to use a hinge for the wheels however Im using a hinge for the axle/bogie but that is off topic. I prefer to try CFrame to get my wheels to spin as thats what most games use.
Alright, I can’t even test it properly because the wheel keeps vanishing for some odd reason. The wheel only deletes when the script is enabled so it’s something to do with the script which is extremely odd. At first I thought it was the script moving the wheel somewhere but the wheel doesn’t exist anymore, not visible in the explorer.
If it’s physics why not just use HingeConstraints as I stated above?
I’ve built an entire train model, with tilting/hinged bogies, multiple cars, which can change elevation and rotation. The wheels all use HingeConstraints.
Here’s the model: Test Train, bogeys with tilt (VectorForce or Hinges).rbxl (174.8 KB)
Thanks it works but Im not sure how to keep the wheel in place without a weld. The current method of updating the wheels position relative to an attatchment is causing the wheel to lag and the mysterious bug where the wheel vanishes when i play.
Edit: managed to get the wheel to stay fixxed in place however the wheel’s orientation doesnt update with the attatchment when i go around a curve and the wheel is only client sided so on the server the wheel is just sitting there doing nothing. Also the rotation doesnt seem to visibly work now but it seems to change if i print it every frame on the client.
local script:
local RunService = game:GetService("RunService")
local wheel = script.Parent
local Coach = script.Parent.Parent.Parent.CoachB.noot
local FrontAttatchment = script.Parent.Parent.BogieFrame.FrontWheel
RunService.RenderStepped:Connect(function()
local velocity = Coach.Velocity.Magnitude
local rotation = CFrame.Angles(0,0, math.rad(velocity / 12))
wheel.CFrame = wheel.CFrame * rotation
print(wheel.Orientation.Z)
wheel.CFrame = CFrame.new(FrontAttatchment.WorldPosition)
end)