- Home /

# Calculate ripple normals

Hey I'm generating wave ripples with this function

```
public Vector3 GetSurfacePosition(Vector3 point) {
Vector3 ripplePosition = (initPosition * -1) * 2;
float offset = ((point.x * point.x) + (point.z * point.z));
offset += ((point.x * ripplePosition.x) + (point.z * ripplePosition.z));
var value = amplitude * Mathf.Sin(timer * speed + offset * frequency);
if(Vector3.Distance(point, initPosition) < rippleDistance)
return new Vector3(0, (value * amplitude), 0);
return Vector3.zero;
}
```

It works well for getting the positions of the ripples, but now i would like to also get the normals. I have tried with this function, but it doesn't seem to return the correct values.

```
public Vector3 GetSurfaceNormal(Vector3 point) {
Vector3 ripplePosition = (initPosition * -1) * 2;
float offset = ((point.x * point.x) + (point.z * point.z));
offset += ((point.x * ripplePosition.x) + (point.z * ripplePosition.z));
float x = -1 * speed * amplitude * Mathf.Cos(-1 * timer * speed + offset * frequency);
float y = 1;
float z = speed * amplitude * Mathf.Cos(-1 * timer * speed + offset * frequency);
if(Vector3.Distance(point, initPosition) < rippleDistance)
return new Vector3(x,y,z).normalized * 2;
return Vector3.zero;
}
```

Here's a screenshot of what I have right now.

**Answer** by syltefar
·
Jan 31, 2017 at 02:43 PM

Does this work, Asger?

```
public Vector3 GetSurfaceNormal(Vector3 point)
{
Vector3 ripplePosition = (initPosition * -1) * 2;
float a = amplitude * amplitude;
float f = frequency;
float x = point.x;
float z = point.z;
float p = timer * speed;
float rx = ripplePosition.x;
float rz = ripplePosition.z;
Vector3 slopeX = new Vector3(1f, a * Mathf.Cos((x*x + z*z + x*rx + z*rz)*f+p) * (f*2*x + f*rx), 0f);
Vector3 slopeZ = new Vector3(0f, a * Mathf.Cos((x*x + z*z + x*rx + z*rz)*f+p) * (f*2*z + f*rz), 1f);
if (Vector3.Distance(point, initPosition) < rippleDistance)
return Vector3.Cross(slopeX, slopeZ).normalized;
else
return Vector3.right;
}
```

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

Change Camera Clipping/Culling Mode 1 Answer

Why does inverting two components of a normal break them? 1 Answer

3D. How to get 1 pixel to equal 1 degree camera FOV 0 Answers

Create a straight gradient equation with a grid of 3d objects 1 Answer

How to scale/calculate GameObject Z distance to the real physical world... 0 Answers