How to create a moving platform with BodyMovers?

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?

Now I’ve properly read it, something along the lines of:

local Time = 5
local Pos1 = Vector3.new(0, 0, 0)
local Pos2 = Vector3.new(5, 0, 0)

local TweenService = game:GetService('TweenService')
local tweenInfo = TweenInfo.new(Time, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)

local BodyPosition = workspace.Platform.BodyPosition

while (true) do
    local Tween1 = TweenService:Create(BodyPosition, tweenInfo, {Position = Pos1})
    Tween1:Play()
    Tween1.Completed:Wait()
    local Tween2 = TweenService:Create(BodyPosition, tweenInfo, {Position = Pos2})
    Tween2:Play()
    Tween2.Completed:Wait()
end

Something along those lines should work. Sorry for the spam, I somehow missed an entire paragraph of your question while looking through.
Using a BodyGyro in tandem with this to maintain the orientation would sort out your wonky platforms. Although the answer mentioned below seems to work perfectly.

This should fix that issue :slight_smile:

2 Likes

I’ve actually tried this solution already, as I’ve seen it being used in other threads. However, I don’t know if it just doesn’t work or I’m missing something completly? I’m getting this happening: https://gyazo.com/4ae510a562b6d37f938cdb76a23fe199.

The parts fall because they’re unanchored, but they’re unanchored because BodyMovers don’t work if they’re not.

1 Like

Have you set the MaxForce property of the BodyPosition?

1 Like

Huh, that’s embarrassing. I did not. I’ve only been working with BodyMovers for a short while and I already hate how tedious they are to get a desired result.

This does work, however the parts don’t fully reach their destination before turning around, which I could probably flesh out by changing P and D, and there’s a slight but obvious sliding of the player.

For those reasons, I’ll be going with the solution that @3rdhoan123 linked as it works just fine and won’t require me to pull any more hair out stressing over BodyMovers.

2 Likes

Good choice ;p

I personally dislike BodyMovers and the lack of flexibility they offer.

1 Like