- Home /
2D floating object
I have this water scene (Side-on view) where I would like my object to float on the water, I'm using the OnTriggerStay2D(Collider2D Hit) to check when the brick is in the water, but I have tried lerping but it did not work. I get the initial impact position Y of the object and store it in a variable "impactPointY". Just wondering if anyone knew how to simulate realistic water buoyancy by making it float to the surface at a constant speed. At the moment I am using this:
void OnTriggerEnter2D(Collider2D Hit){
impactPointY = Hit.transform.position.y;
}
void OnTriggerStay2D(Collider2D Hit)
{
if (Hit.attachedRigidbody){
Hit.rigidbody2D.AddForce(Vector3.up * 15);
}
}
But this does not work very well as the objects get to the surface and launched up, then they go underwater and the whole things starts again. Just for clarification, I am needing a way to make an object float in 2D without it glitching around.
Thanks for any help in advance
Answer by boxing_rex · Jun 19, 2014 at 10:40 AM
Just to let you know I fixed the problem by doing this:
//Floating
public float waterLevel = 0f;
public float floatHeight = 2;
public float bounceDamp = 0.05f;
public Vector3 buoyancyCentreOffset;
private float forceFactor;
private Vector3 actionPoint;
private Vector3 uplift;
void OnTriggerStay2D(Collider2D Hit)
{
actionPoint = Hit.transform.position + Hit.transform.TransformDirection(buoyancyCentreOffset);
forceFactor = 1f - ((actionPoint.y - waterLevel) / floatHeight);
if(forceFactor > 0f){
uplift = -Physics.gravity * (forceFactor - Hit.rigidbody2D.velocity.y * bounceDamp);
Hit.rigidbody2D.AddForceAtPosition(uplift, actionPoint);
}
}
This seems to of fixed the issue of floating, now i can adjust a whole assortment of factors to let me get it how I want, I just need to figure out how to stop the infinite rotation now :D
Answer by FreeTimeDev · Jun 18, 2014 at 09:28 PM
Move your movement code into a FixedUpdate function and constantly check a boolean to see if you should first be trying to move at all. (For instance, before going to the water you can't move to the surface of it). After your bool is true, apply your force (test out lerping again too to see which one looks nicer). When you're out of the water -- change the bool.
If the water height doesn't change often you could also turn off the lerp/add force after a certain height. Then fake the bobbing if you're at that height.
I've never tried floating/bobbing in water myself, but this seems like a reasonable solution. It's not about making something float in water -- it's about making it look like something is floating in water.
The water changes height, that's why I used the OnTriggerStay2D. By the way, OnTrigger2D acts as an update. Every frame it checks if there is an object in it and if there is then it will do the code, so there is no need for a boolean. But adding vertical force will not work as it causes the objects to go flying
Your answer
Follow this Question
Related Questions
C# Water Rotation 0 Answers
rigidbody2D.addforce in collider 1 Answer
Object jumps right after the attached HingeJoint2D is Enabled 0 Answers
AddForce with 2D Character 2 Answers