• 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 /
This question was closed Jul 01, 2016 at 12:03 PM by cimo for the following reason:

La domanda ha avuto risposta, è stata accettata la risposta giusta

avatar image
0
Question by cimo · Jun 29, 2016 at 04:42 PM · wavesphysicbuoyancy

Buoyancy system wave

Hello, i hope you can help me with t$$anonymous$$s problem. I wrote a buoyancy system with waves, but i have a problem for synchronize waves and buoyancy.

I would like that the object go up and down with wave's movement and continue with buoyancy.

T$$anonymous$$s is for waves

 private void wave()
     {
         if (GetComponent<MeshFilter>() != null) {
             Mesh mesh = GetComponent<MeshFilter>().mesh;
 
             Vector3[] vertices = mesh.vertices;
 
             for (int i = 0; i < vertices.Length; i++)
             {
                 Vector3 globalPosition = transform.TransformPoint(vertices[i]);
 
                 Vector3 globalPositionVertexProject = projectPointOntoSurface(globalPosition);
 
                 Vector3 localPosition = transform.InverseTransformPoint(globalPositionVertexProject);
 
                 vertices[i] = localPosition;
             }
 
             mesh.vertices = vertices;
 
             mesh.RecalculateNormals();
             mesh.RecalculateBounds();
         }
     }
 
     private Vector3 projectPointOntoSurface(Vector3 globalPosition)
     {
         return new Vector3(globalPosition.x, GetComponent<Collider>().bounds.max.y + waveAmplitude * (waveLogic(globalPosition) - 1.0f), globalPosition.z);
     }
    
     public float relativeHeightAtPoint(Vector3 globalPosition)
     {
         return GetComponent<Collider>().bounds.size.y + waveAmplitude * (waveLogic(globalPosition) - 1.0f);
     }
 
     private float waveLogic(Vector3 globalPosition)
     {
         return Mathf.Sin(globalPosition.x * 0.5f + Time.time) * 0.8f + Mathf.Sin(globalPosition.z * 0.5f + Time.time) * 0.2f;
     }

T$$anonymous$$s is for buoyancy

 void FixedUpdate()
     {
         wave();
 
         foreach (KeyValuePair<string, WaterObject> entry in elements)
         {
             foreach (Vector3 position in entry.Value.voxels)
             {
                 Vector3 globalPosition = entry.Value.collider.transform.TransformPoint(position);
                
                 if (globalPosition.y < waterLevel)
                 {
                     float k = (waterLevel - globalPosition.y);
 
                     if (k > 1)
                     {
                         k = 1f;
                     }
                     else if (k < 0)
                     {
                         k = 0f;
                     }
 
                     float arc$$anonymous$$medeForce = waterDensity * Mathf.Abs(Physics.gravity.y) * entry.Value.volume;
                     Vector3 arc$$anonymous$$medeLocalForce = new Vector3(0, arc$$anonymous$$medeForce, 0) / entry.Value.voxels.Count;
 
                     Vector3 velocity = entry.Value.rigidbody.GetPointVelocity(globalPosition);
                     Vector3 damping = -velocity * 0.1f * entry.Value.rigidbody.mass;
                     entry.Value.force = damping + (Mathf.Sqrt(k) * arc$$anonymous$$medeLocalForce);
                    
                     //float rp = relativeHeightAtPoint(globalPosition);
                     //entry.Value.force.y = entry.Value.force.y - ((waterLevel - rp) * 2);
 
                     entry.Value.rigidbody.AddForceAtPosition(entry.Value.force, globalPosition);
                 }
             }
         }
     }

Thanks for your help!

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

1 Reply

  • Sort: 
avatar image
0

Answer by cimo · Jul 01, 2016 at 12:17 PM

Solved, i have used different force, thanks at all...

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can I make 3D waves in Unity? 2 Answers

Terrain height map influences ocean wave heights? 2 Answers

How would I go about making infinite waves that get harder over time? 1 Answer

Having trouble to getting the height from an object being displaced by a shader 1 Answer

Create Physical 3D Hook? 1 Answer


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