Home > OS >  Unity: Finding Lowest Vertices on Mesh
Unity: Finding Lowest Vertices on Mesh

Time:06-07

I'm a very newbie to using unity and C#. I have a question about spawning an object on the lowest Y coordinate on the mesh.

  1. How to get the lowest Y vertices with other coordinates to spawn an object at that point.

Thank you in advance everyone :)

public Mesh terrain;
public GameObject agents;

void Start()

Mesh terrain = GetComponent<MeshFilter>().mesh;
Vector3[] meshVertices = terrain.vertices;

float minY = float.MinValue;
int count = terrain.vertexCount;
List<Vector3> vertices = new List<Vector3>();
terrain.GetVertices(vertices);

for (int i = 0; i < vertices.Count; i  )
{
    Vector3 pos = vertices[i];
    minY = Mathf.Min(pos.y,-pos.y);
    Vector3 position = transform.TransformPoint(vertices[i]);

    if (position.y == minY)
    {
        Instantiate(agents, position, Quaternion.identity);
    }
}

terrain.RecalculateBounds();

CodePudding user response:

If you're looking for a minimum value, then you need to start with something that's so big that anything will be less than that. Something like float.MaxValue. Then you need to go through all the points and compare your current minimum to the running minimum and cache the current point if it's less. Once you're done with all the points then you can use your cached point as the instantiation location. Consider the following:

public Mesh terrain;
public GameObject agents;

void Start()
{
    Mesh terrain = GetComponent<MeshFilter>().mesh;
    Vector3[] meshVertices = terrain.vertices;

    float minY = float.MaxValue; // <--- Change
    Vector3 minimumVertex; // <--- Change
    int count = terrain.vertexCount;
    List<Vector3> vertices = new List<Vector3>();
    terrain.GetVertices(vertices);

    for (int i = 0; i < vertices.Count; i  )
    {
        // Vector3 pos = vertices[i];   // <--- Change
        // minY = Mathf.Min(pos.y,-pos.y);   // <--- Change
        Vector3 position = transform.TransformPoint(vertices[i]);

        // if (position.y == minY)   // <--- Change
        if(position.y < minY)   // <--- Change
        {
            minY = position.y;   // <--- Change
            minimumVertex = position;   // <--- Change
            //Instantiate(agents, position, Quaternion.identity);   // <--- Change
        }
    }
    Instantiate(agents, minimumVertex, Quaternion.identity);   // <--- Change

    terrain.RecalculateBounds();       
}

CodePudding user response:

In fact, I was looking for an answer for you that summarizes a large part of the code, all this is possible thanks to system.linq, the following code sorts vertices by y coordinate and puts them in a position list, just enough Set list[0] as position. The other vertices are also arranged in order, which is an advantage.

using System.Linq;
public void Start()
{
    var meshFilter = GetComponent<MeshFilter>();

    var list = meshFilter.mesh.vertices.Select(transform.TransformPoint).OrderBy(v => v.y).ToList();
    
    Debug.Log(list[0]); // lowest position
    
    Debug.Log(list.Last()); // highest position
}
  • Related