Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

2D buoyancy

Discussion in 'Scripting' started by raycosantana, Jun 5, 2014.

  1. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    Hi

    EDIT: I figured it out by my self, so as is custom I leave the finished script here for others who might need it.

    Code (CSharp):
    1. public class Buoyancy : MonoBehaviour {
    2.     public float UpwardForce = 12.72f;
    3.     public GameObject SplashParticles;
    4.     private bool isInWater = false;
    5.     private bool splash = false;
    6.     void OnTriggerEnter2D(Collider2D Other){
    7.         if (Other.tag == "Water") {
    8.             isInWater = true;
    9.             rigidbody2D.drag = 5f;
    10.             rigidbody2D.angularDrag = 5f;
    11.         }
    12.         if (Other.tag == "Water" && !splash){
    13.             Instantiate(SplashParticles, transform.position, Quaternion.identity);
    14.             splash = true;
    15.         }
    16.    
    17.     }
    18.  
    19.     void OnTriggerExit2D(Collider2D Other){
    20.         if (Other.tag == "Water"){
    21.             isInWater = false;
    22.             rigidbody2D.drag = 0.5f;
    23.             rigidbody2D.angularDrag = 0.5f;
    24.         }
    25.     }
    26.  
    27.     void FixedUpdate(){
    28.         if (isInWater){
    29.             Vector2 force = Vector2.up * UpwardForce;
    30.             this.rigidbody2D.AddForce(force, ForceMode2D.Force);
    31.             Debug.Log(isInWater);
    32.         }
    33.     }
    34. }
    I even added some splash particles, you can erase that part if you want. Its not perfect but it works.

    Here is a little video



    Instructions:

    1. Add this script to the gameobject.
    2. Add rigidbody to gameobject.
    3. Create a plane or whatever your water will be.
    4. Add a BoxCollider 2D to your water object and set it to trigger.
    5. Tag the Water with the "Water" tag.
    6. ???.
    7. Profit!.
    Any help to upgrade the script is welcome.
     
    Last edited: Jun 5, 2014
    RodrigoSeVeN, BusyCat and Naphier like this.
  2. Naphier

    Naphier

    Joined:
    Aug 4, 2014
    Posts:
    114
    Well that was awesome!
    Thank you so much. I've looked at about a dozen examples and they were all overly complicated. This hits the nail right on the head and saved me a few hours of experimentation!!
     
  3. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    You are welcome, I'm glad it helped you.
     
  4. Ash-Blue

    Ash-Blue

    Joined:
    Aug 18, 2013
    Posts:
    102
    While I love the idea, it doesn't work in my game's 2D environment. As adding constant force makes the item move when it shouldn't for me.
     
  5. raycosantana

    raycosantana

    Joined:
    Dec 23, 2012
    Posts:
    319
    Well that's because that's how buoyancy works in real life, in water objects are constantly moving, but you are encouraged to play with the values, adapt or modify the script to suit your needs, the script is free for any use.
     
    Last edited: Aug 22, 2014
  6. Ash-Blue

    Ash-Blue

    Joined:
    Aug 18, 2013
    Posts:
    102
    Well there are equations to taper off of the water pressure near the top of the water. For a more realistic effect if you want that kind of thing. I used the following script in this video to make my crates double as platforms in the water.