How can I make parts (such as a player, items, or the base of a boat) float up to the surface of a part?

So, if you ever played Galleons or older shipping games, I want to have a part that’s essentially like the water of the map, and if certain parts, like a humanoid or ship chassis, goes under the water, it automatically floats up to the surface.

I already have something like that, but instead the part just removes velocity and gravity from the player to make the illusion of floating, but this is a bit unreliable because it can catch the part at different elevations and isn’t very consistent or precise.
image

I imagine it’s something like making a part have no gravity and then moving the Y up to the water’s Y position, but I’m not sure what’s the best way to do that and how to do it as long as its in the water. Any help is appreciated!

3 Likes

I think you could achieve such result by using Tweening, so you make the Y position of your character interpolates between two points and use Ease out of some sort, I’m speaking pure logic haven’t really tried it out myself in Roblox I’m kinda new to the syntax

Oh I see, yeah I’ll have to take a look at that because I want to make it less physics based and I could probably tune the position if I want something to sink, but I’m not really sure what’s the best way to organize it so I don’t need every unanchored part to have a float script. Maybe somehow just get any part that touches the water with a script and then set that object to tween to the Y position somehow.

You can group multiple parts that you want in a model, choose a primary part for that model and make the primary part float in one script which affects the whole model

So, I’ve tried tweening the player’s position and I’m not sure if it’d work for what I need it to do, the player can’t move during it and I’m not sure if it gives off the same illusion.

This is what I’m trying to go for, but I’m not sure if this is also tweening the player’s position but in a different way, my current code is just.

Which gets rid of the player’s mass and reverses gravity, and tweens the position up, but it doesn’t really work the way I need.

Hey, I just had a really similar issue for my own game. I fixed this with an AlignPosition (I’m not familiar with constraints, so I will explain this really really easily)

Step 1: Create an attachment inside of whatever part you want to float. Make sure the attachment is at the very center of whatever object you want to float:
image

Step 2: Add an “AlignPosition” instance to your part. Set its mode property to OneAttachment, and set the Attachment0 property to the attachment you created in step 1. Turn on the “ApplyAtCenterOfMass” property in the AlignPosition

Step 3: Change the AlignPosition’s ForceLimitMode property to be “PerAxis”, and set its MaxAxesForce property to (0, 10000, 0). This allows the AlignPosition to ONLY affect the vertical force of the object, and it won’t affect the X or Z axis. You may have to increase the Y value of the MaxAxesForce property if the object you want to float is heavy

Step 4: In a script, constantly check if the object is below the top of your water. Here is the script I’m using, but you can make it however you want:

local Part = script.Parent
local WaterHeight = -15

local AlignPosition = Part.AlignPosition

game:GetService("RunService").Heartbeat:Connect(function()
	if Part.Position.Y < WaterHeight then
		AlignPosition.Enabled = true
	else
		AlignPosition.Enabled = false
	end
end)

In this case, the AlignPosition is enabled whenever the part’s Y position goes below -15 (change this number to your liking). Whenever the part goes below -15, the AlignPosition enables, causing the part to float upwards

You would only need to do this entire process one time for each model, assuming that all parts in the model are connected with welds or some other constraint

2 Likes

I get the idea, but I’m a bit confused what you mean. Is the AlignPosition in the water part, and the attachment is in the floating part? Or is it the other way around? I kind of got it set up in a way where there’s an AlignPosition in the water part and the attachment in the humanoid root part, but the humanoid is being pulled back into the ground and not up towards the water, I think.

Sorry, the AlignPosition is inside of the part which you want to float. And the attachment is in the same part.

image

Your part should look something like this

Edit:

This is probably caused by your script. It sounds like your script is enabling the AlignPosition when the part is above water, not when it’s below the water

Oh no nevermind, I just had it all messed up lol. I got it working for the most part, that’s a pretty smart solution though. Thanks for the help!

No problem, make sure to mark my post as a solution so other people with a similar problem can use this post for help :+1:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.