Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by boxing_rex · Jun 18, 2014 at 08:48 PM · c#2dphysicsrigidbodywater

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image boxing_rex · Jun 19, 2014 at 06:41 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How would I get collision at different speeds? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges