# How can I convert a plane area position to terrain area position ?

The plane is above the terrain. The plane size in this case is 320x320
What I want is to calculate the plane area on the terrain. So when I get each time the plane size and if the plane is above the terrain and I spawn object on this plane area then spawn the object on the terrain same area.

The plane is like a marker for area to spawn on the terrain.

This is how I create the plane :

``````[ExecuteAlways]
[RequireComponent(typeof(MeshFilter), typeof(MeshRenderer))]
public class CustomPlane : MonoBehaviour {

public float width = 1;
public float length = 1;

private float oldWidth;
private float oldHeight;

public void Start()
{
oldWidth = width;
oldHeight = length;

Create();
}

private void Update()
{
if(oldWidth != width || oldHeight != length)
{
Create();

oldWidth = width;
oldHeight = length;
}
}

private void Create()
{
MeshFilter meshFilter = gameObject.GetComponent<MeshFilter>();

Mesh mesh = new Mesh();

Vector3[] vertices = new Vector3[4]
{
new Vector3(0, 0, 0),
new Vector3(width, 0, 0),
new Vector3(0, length, 0),
new Vector3(width, length, 0)
};
mesh.vertices = vertices;

int[] tris = new int[6]
{
// lower left triangle
0, 2, 1,
// upper right triangle
2, 3, 1
};
mesh.triangles = tris;

Vector3[] normals = new Vector3[4]
{
-Vector3.forward,
-Vector3.forward,
-Vector3.forward,
-Vector3.forward
};
mesh.normals = normals;

Vector2[] uv = new Vector2[4]
{
new Vector2(0, 0),
new Vector2(1, 0),
new Vector2(0, 1),
new Vector2(1, 1)
};
mesh.uv = uv;

meshFilter.mesh = mesh;
}
}
``````

My problem is to convert the plane size area as area on the terrain.
How to convert the positions or size of the plane to be on the terrain.

Iâ€™m sorry but iâ€™m afraid i do not understand what you are doing or asking in the slightest. What i got is that you have a plane above some terrain. And thenâ€¦ what? What is supposed to achieve? Why are you generating it? What is the usecase that lead you to this approach? A bit more information would really help me give a more useful answer.

You talk a lot about the â€śposition or size of the plane on the terrainâ€ť, but i have no idea what thatâ€™s supposed to mean, or how the terrain and the plane even relate. One seems larger than the other. Are you trying to map positions? Or get the position on the terrain below the plane?
Maybe this is a language barrier kind of problem. It would probably help if you tried to explain what your goal is and why you approach it like that. Once people understand what you are tryin to do it will be easier to help.

1 Like

Both this :

â€śAre you trying to map positions? Or get the position on the terrain below the plane?â€ť

Yes. I want to use the plane as marker for spawning on the terrain. Where the plane is above the terrain position on the terrain below the plane. The plane is just like a marker where to spawn objects on the terrain. Instead spawning on all the terrain are than spawn on where the plane is.

The routines in this utility almost certainly contain enough information to get you what you need:

https://www.youtube.com/watch?v=FykNmJ3NIpI

Linky to the code:

https://gist.github.com/kurtdekker/f9e7b0bdf4b2f9c0455a99f7f0f4a77a