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 Julian-S · Jun 07, 2013 at 08:52 PM · rigidbodyforcemassoceanbuoyancy

Making buoyancy script not mass based

Hi everyone.

So I'm using this buoyancy script from the community shader project's ocean. As you can see it references the ocean and gets the water level where my object is to help calculate the forces.

My problem with it is that it seems to be very much based on the rigidbody mass. It is practically not functional if I set the mass to anything greater than 3. Of course, I want my object to respond realistically to other collisions and such, so I'm looking for a way to modify this script to make it no longer based on the mass of my object's rigidbody. I can't seem to figure out how.

The script: using UnityEngine; using System.Collections; using System.Collections.Generic;

 /* Give thanks to the Unity3d community, I'm just one of many to work on this.
  * http://forum.unity3d.com/threads/16540-Wanted-Ocean-shader
  * You are free to use this as you please as long as you do some good deed on the day fist usage.
  * Any changes and improvements you make to this, although not required, would be great
  * if at least shared with me (extra karma points if shared with the community at Unity3d).
  * This version has been optimized for mobile devices.
  * Ported to C# by Joaquin Grech joaquingrech@echoboom.es http://www.echoboomapps.com
  * Optimized a bit more to get extra FPS on mobile by MindBlocks team
  * Added realistic sinking method by MindBlocks team
  * */
 
 public class Boyancy : MonoBehaviour
 {
 
     private Ocean ocean;
 // Water plane at y = 0
     private float mag = 1f;
     private float ypos = 0.0f;
     private List<Vector3> blobs;
     private float ax = 2.0f;
     private float ay = 2.0f;
     private float dampCoeff = .2f;
     private bool engine = false;
     public bool sink = false;
     public float sinkForce = 3;
     private List<float> sinkForces;
     
     void Start ()
     {
 
         rigidbody.centerOfMass = new Vector3 (0.0f, -0.5f, 0.0f);
     
         Vector3 bounds = transform.FindChild ("Hull").GetComponent<BoxCollider>().size;
         float length = bounds.z;
         float width = bounds.x;
 
         blobs = new List<Vector3> ();
 
         int i = 0;
         float xstep = 1.0f / (ax - 1f);
         float ystep = 1.0f / (ay - 1f);
     
         sinkForces = new List<float>();
         
         float totalSink = 0;
 
         for (int x=0; x<ax; x++) {
             for (int y=0; y<ay; y++) {        
                 blobs.Add (new Vector3 ((-0.5f + x * xstep) * width, 0.0f, (-0.5f + y * ystep) * length) + Vector3.up * ypos);
                 
                 float force =  Random.Range(0f,1f);
                 
                 force = force * force;
                 
                 totalSink += force;
                 
                 sinkForces.Add(force);
                 i++;
             }        
         }
         
         // normalize the sink forces
         for (int j=0; j< sinkForces.Count; j++)
         {
             sinkForces[j] = sinkForces[j] / totalSink * sinkForce;
         }
         
     }
 
     void OnEnable ()
     {
         if (ocean == null)
             ocean = GameObject.FindGameObjectWithTag ("Ocean").GetComponent<Ocean>();
     }
 
     void FixedUpdate ()
     {
         int index = 0;
         
         foreach (Vector3 blob in blobs) {
             
             Vector3 wpos = transform.TransformPoint (blob);
             float damp = rigidbody.GetPointVelocity (wpos).y;
             Vector3 sinkForce = new Vector3(0,0,0);
             
             float buyancy = mag * (wpos.y);
             if (ocean.enabled && !sink)
                 buyancy = mag * (wpos.y - ocean.GetWaterHeightAtLocation (wpos.x, wpos.z));
             
             if (sink)
             {
                 buyancy = Mathf.Max(buyancy, -3) + sinkForces[index++] ;
             }
             
             rigidbody.AddForceAtPosition (-Vector3.up * (buyancy + dampCoeff * damp) , wpos);
 
         }
     }
     
     //Call this void when you want your boat or any object to sink more naturally
     public void Sink(bool isActive)
     {
         sink = isActive;    
     }
 
 
 }

Any help is greatly appreciated, thanks!

Comment
Add comment · Show 1
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 Julian-S · Jun 08, 2013 at 02:25 AM 0
Share

I've been working away at it, and still no luck yet

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Jun 08, 2013 at 04:17 AM

AddForceAtPosition --> ForceMode --> Acceleration

Keep in mind when you ignore the mass you need to lower the "forces" since they aren't forces anymore but acceleration values.

Comment
Add comment · Show 3 · 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 Julian-S · Jun 08, 2013 at 06:09 PM 0
Share

Ahah! Thank you, I'll give this a shot. I forgot about force modes

avatar image Julian-S · Jun 09, 2013 at 02:17 AM 0
Share

For some reason, no matter which variable in that equation I divide, the boat reacts oddly. It's either the boat rapidly rotates over the x axis, or if I divide it more, it doesn't, but it no longer moves up and down properly.

avatar image Julian-S · Jun 09, 2013 at 02:37 AM 0
Share

I have a feeling it's the damp variable but I can't figure it out. I've tried to make the damp variable equal to rigidbody.GetPointVelocity (wpos).y / Time.deltaTime to try to get it in terms of acceleration but it still likes to jump all over the place. There's got to be something I'm missing.

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

15 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

Related Questions

My jumping script has errors 1 Answer

characters keep getting pushed back 0 Answers

Grow objects 0 Answers

What's the Rigidbody's gravity unit? 1 Answer

Unity First person controller force and mass 0 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