This site uses strictly necessary cookies. More Information

X- Home /

# height of a point in a mesh

hello!

I'm currently working on a buoyancy system and I need a way to get the water level height of a specific point (the transform of my boat) I tried to do It myself, however, I had no success

I currently have two scripts working together to create the ocean effect:

```
[System.Serializable]
public class Wave
{
public float noisewalkSpeed;
public Vector3 scale = new Vector3(0.1f, 0.1f, 0.1f);
public Vector3 speed = new Vector3(1.0f, 1.0f, 1.0f);
public Vector3 waveDistance = new Vector3(1.0f, 1.0f, 1.0f);
public Vector3 noiseStrength = new Vector3(1.0f, 1.0f, 1.0f);
public Vector3 noiseWalk = new Vector3(1.0f, 1.0f, 1.0f);
public float waveHeight;
public Vector3 GetWave(Vector3 coord)
{
Vector3 xyz_coord1 = Vector3.zero;
Vector3 xyz_coord2 = Vector3.zero;
xyz_coord1.x += Mathf.Cos((Time.time * speed.x + coord.z) / waveDistance.x) * scale.x;
xyz_coord1.x += Mathf.PerlinNoise(coord.y + noiseWalk.x, coord.z + Mathf.Sin(Time.time * 0.1f)) * noiseStrength.x;
xyz_coord1.y += Mathf.Sin((Time.time * speed.y + coord.z) / waveDistance.y) * scale.y;
xyz_coord1.y += Mathf.PerlinNoise(coord.x + noiseWalk.y, coord.z + Mathf.Sin(Time.time * 0.1f)) * noiseStrength.y;
xyz_coord2.y += Mathf.Sin((Time.time * speed.y + coord.x) / waveDistance.y) * scale.y;
xyz_coord2.y += Mathf.PerlinNoise(coord.x + noiseWalk.y, coord.z + Mathf.Sin(Time.time * 0.1f)) * noiseStrength.y;
xyz_coord1.z += Mathf.Cos((Time.time * speed.z + coord.x) / waveDistance.z) * scale.z;
xyz_coord1.z += Mathf.PerlinNoise(coord.x + noiseWalk.z, coord.y + Mathf.Sin(Time.time * 0.1f)) * noiseStrength.z;
waveHeight = xyz_coord1.y + xyz_coord2.y;
return xyz_coord1 + xyz_coord2;
}
```

}

and

public class Ocean : MonoBehaviour {

```
Mesh waterMesh;
private Vector3[] newVertices;
private Vector3[] originalVertices;
public Wave waveScript = new Wave();
public float height;
// Use this for initialization
void Start()
{
waterMesh = this.GetComponent<MeshFilter>().mesh;
originalVertices = waterMesh.vertices;
}
// Update is called once per frame
void Update()
{
moveSea();
waveScript.noiseWalk.z += waveScript.noisewalkSpeed * Time.deltaTime;
}
void moveSea()
{
newVertices = new Vector3[originalVertices.Length];
for (int i = 0; i < originalVertices.Length; i++)
{
Vector3 vertice = originalVertices[i];
vertice = transform.TransformPoint(vertice);
vertice += waveScript.GetWave(vertice);
newVertices[i] = transform.InverseTransformPoint(vertice);
}
waterMesh.vertices = newVertices;
waterMesh.RecalculateNormals();
}
```

}

any advice would be greatly appreciated!

**Answer** by Glurth
·
Sep 04, 2020 at 04:40 PM

The hard way would be to use the oceans transform, to InverseTransform the boats position, putting it into the ocean mesh's local space. Then loop through all the ocean mesh vertices to find the one closest to the boat. Use the oceans transform to transform that vertex into world-space. Get the Y coordinate of that vertex for the ocean height. For more precision, you could find a FEW of the closest vertices on the mesh and compute the average height, weighted by the distance from the boat.

Simpler: Use a mesh collider on the ocean, and cast a ray(https://docs.unity3d.com/ScriptReference/Physics.Raycast.html) down from a safe distance over the boat's position . The RaycastHit result (https://docs.unity3d.com/ScriptReference/RaycastHit.html) contains the "hit point" which should be the surface of the ocean. (you may need to use tags to prevent a hit detection on the boat itself).

Simpliest: Adjust your wave function such that it provides the vertex coordinate itself, rather than an OFFSET (line 29) from the current coordinate. Then just call GetWave(boatPosition) to get the height of the ocean.

### Your answer

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

### Follow this Question

### Related Questions

Damping forces to stop boat oscillation 0 Answers

Buoyancy using shaders 2 Answers

Making buoyancy script not mass based 1 Answer

How do i make floating ship ? 0 Answers

Object buoyancy in water on different water levels? 1 Answer