Help with making ship position flow with surface position Y

So I’m trying to make a ship that will position on the surface of parts
like this:

but I have pathfinding stuff that tweens the position of the ship to where I want it to move into

this is the code that makes it go in the surface only

local function updateShPosY()
	local shipOrPos = sh.Position
	local ray_ =,, -1, 0) * 10)
	local rayHit_, rayPos_ = workspace:FindPartOnRayWithIgnoreList(ray_, getcd())
	local _magnitude = (rayPos_ - shipOrPos).Magnitude
	if rayHit_ ~= nil and _magnitude <= 10 then
		local oPos_ = sh.Position
		local fPos =, oPos_.Y - _magnitude + sh.Size.Y / 2 / 2, oPos_.Z) --sh.Position -, _magnitude, 0) +, sh.Size.Y / 2 / 2, 0)
		tween:Create(sh,, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {
			Position = fPos
	local targetPos = Get3DPosition(mouse.X,mouse.Y)
	ball.Position = targetPos

but when its moving the ship, it bugs out as shown in the video

this is the code where it moves the ship

so basically I raycast on the bottom of the ship to get the surface Y position that I want the ship to be in
but when it path finds its way (using tween), it does this:

any ideas on how to make it work while moving?

1 Like

You can using lerping instead of playing a tween. This way you can change the Y as needed while moving.

I find tweens to be a pain as they stop you from doing some things and also can be a pain to create. It can be much better to make it all from scratch with a lerp.

More or less you get the difference between your start point and end point for all axis and split it into increments.

function Lerp(P1,P2,Inc)
     local PChange = P2 - P1
     return PChange * Inc (Between 0 and 1)
1 Like

Thanks, dude

this works much better now
heres some clip on lerp

this is the code that fixed it btw