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:
https://gyazo.com/42bbdc6e8cc234a427d1dbc56c1ccfb6

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_ = Ray.new(shipOrPos, Vector3.new(0, -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 = Vector3.new(oPos_.X, oPos_.Y - _magnitude + sh.Size.Y / 2 / 2, oPos_.Z) --sh.Position - Vector3.new(0, _magnitude, 0) + Vector3.new(0, sh.Size.Y / 2 / 2, 0)
		tween:Create(sh, TweenInfo.new(.1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out), {
			Position = fPos
		}):Play()
	end
end
run.RenderStepped:Connect(function()
	local targetPos = Get3DPosition(mouse.X,mouse.Y)
	ball.Position = targetPos
	updateShPosY()
end)

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:
https://gyazo.com/e485d42a3e777a8a0161fd5f44a63bf2

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)
end
1 Like

Thanks, dude

this works much better now
heres some clip on lerp
clip2


this is the code that fixed it btw

tysm