- 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 this 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.

This 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;
}
```

This 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 archimedeForce = waterDensity * Mathf.Abs(Physics.gravity.y) * entry.Value.volume;
Vector3 archimedeLocalForce = new Vector3(0, archimedeForce, 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) * archimedeLocalForce);
//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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.