In a game I’m making I want to have moving platforms that the players use to get around the level. I did this with TweenService, but the players don’t move with the parts. I’m now trying to use BodyMovers instead because they rely on physics, and will move the players.
The problem I’m having is that I can’t get the motion of the platforms that I want and have it not colossally break when the player is on the platform.
I’ve done a few things so far. I created this (grey bricks are using tweens, red bricks are using BodyMovers), which has the motion I was looking for, but won’t work if I change it all it. It’s made with changing a BodyPosition, but changing distance and time would ruin the effect.
BodyPosition Code
local BodyPosition = Instance.new("BodyPosition")
BodyPosition.MaxForce = Vector3.new(5000, math.huge, 5000)
BodyPosition.P = 1000
BodyPosition.D = 450
BodyPosition.Parent = part
-- Move part
coroutine.wrap(function()
while true do
BodyPosition.Position = pos2
wait(moveTime)
BodyPosition.Position = pos1
wait(moveTime)
end
end)()
I even went and busted out my limited physics knowledge and tried some kinematics with BodyForces, but I’m not sure I can assume studs = meters and Newtons = whatever Roblox’s physics engine uses. Either way, that doesn’t work (admittedly the code isn’t very nice to the vertical movement.
BodyForces code
local t = model.Time.Value
local m = part:GetMass()
local Fg = m * workspace.Gravity
local d = (pos1 - pos2)
-- Move part
local tarPos = pos2
local count = 2
coroutine.wrap(function()
while true do
local d = (tarPos - part.Position)
local a = (2*d)/(t^2) -- d = (1/2)*a*t^2
local F = m*a
print(F)
BodyForce.Force = F + Vector3.new(0, Fg, 0)
wait(t)
if count % 2 == 0 then
tarPos = pos1
else
tarPos = pos2
end
end
end)()
I tried tweening BodyMover properties as suggested in this thread, but that also didn’t turn out great either.
Tween BodyMover code
local BodyPosition = Instance.new("BodyPosition")
BodyPosition.Parent = platform
local BodyGyro = Instance.new("BodyGyro")
BodyGyro.Parent = platform
platform.Anchored = false
coroutine.wrap(function()
while true do
local Tween = TweenService:Create(BodyPosition, info, {Position = pos2})
TweenService:Create(BodyGyro, info, {CFrame = cfr2}):Play()
Tween:Play()
Tween.Completed:Wait()
Tween = TweenService:Create(BodyPosition, info, {Position = pos1})
TweenService:Create(BodyGyro, info, {CFrame = cfr1}):Play()
Tween:Play()
Tween.Completed:Wait()
end
end)()
So, yeah, I can’t really figure this stuff out. Am I like somewhat close or just completely off? How can I make a moving platform works correctly?