- Home /

**closed**Jul 01, 2016 at 12:03 PM by cimo for the following reason:

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

# 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!

### 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

### 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