Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Buoyancy effector not fully working in 2D tilemap. I sink if I go under the surface. Ideas?

Discussion in '2D' started by bdilloughery_mvla, Sep 28, 2022.

  1. bdilloughery_mvla

    bdilloughery_mvla

    Joined:
    Sep 22, 2017
    Posts:
    39
    I have a 2D tilemap set up.
    I created a separate tilemap for the water, which allows for one defined surface level. I am using a tilemap collider that is a trigger, and that is using a composite collider, as well as the buoyancy effector.

    The surface of the water works great, but if you jump in with a high enough falling velocity, you just fall under the surface and sink. All of the water has the buoyancy effector, yet it seems like only the surface is registering the buoyancy effector.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    One hack would be to clamp your velocity to some "Safe" value immediately upon striking the water. In real life this tends to happen because more and more energy is lost the faster you hit the water, plus when falling in a dense fluid like our atmosphere, objects quickly reach a peak terminal velocity.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    Too much going on here to give you a meaningful answer.

    Buoyancy is simply about relative density between the objects in question and how much of the collider area is below the surface level you set, that's it. It doesn't matter which colliders you state or combination, the effector has no concept of them.

    If you're using a CompositeCollider2D then presumably you're not using Outlines which are just edges. Those are open-shapes and don't have an "inside" as opposed to all the other shape types like Polygons which are closed-shapes and do have an "inside".

    I'd say spin-up a project and do a test to confirm you're using it correct etc.

    I'm not going to write a tutorial on it, there's plenty on line but your "fluid" density is set via BuoyancyEffector2D.density and collider density defaults to 1. If you turn-on auto-mass you can explicit set the Collider2D density in the inspector.

    There's a bunch of examples for this in my PhysicsExamples repo: https://github.com/Unity-Technologies/PhysicsExamples2D/tree/master/Assets/Scenes/Effectors
     
  4. bdilloughery_mvla

    bdilloughery_mvla

    Joined:
    Sep 22, 2017
    Posts:
    39
    Oh man, that was it. Thank you.

    I had the Composite Collider 2D's "Geometry Type" on the default setting of "Outlines".

    I've never used that before. Switching it to "Polygons" now includes the inside of the water as part of the effector. Some more playtesting revealed that with the "Geometry Type" set to "Outlines", the buoyancy effector was only applied on the exterior ring of tiles. For example, in a body of water that is 8x12, only the ring of outer tiles had the effector applied and not the inner 6x10 ... out at least that was the resulting effect, as whenever I touched the outer "edge" of the body of water with my player I could float until I left the edge.

    Thank you!
     
    MelvMay and Kurt-Dekker like this.
  5. Elliot1990

    Elliot1990

    Joined:
    Jan 6, 2023
    Posts:
    3
    When I read your problem, I somehow suspected it was to do with the geometry type. I have just discovered the difference after almost a year of trying to learn Unity, and it has blown my mind.