Random point generator?

Hello,
Is there a function than can return a Vector3 location of a random point along any navmesh?

Thank you,
Michael S. Lowe

I use something like this (ignore some of the quirkiness for example the loop that was there ready for me to do stuff like testing for empty spot I haven’t got round to). So I randomise the direction with the x and y negative or positive then simply generate a random number between the mindistance and maxdistance for distance away from the “from” starting point.

public static Vector3 FindLocation(Vector3 from, float mindistance, float maxdistance, bool empty)
    {
        int i = 0;
        float x = 0f;
        float z = 0f;
        float y = from.y;
        bool xneg;
        bool zneg;
        Vector3 newposition = new Vector3(x, y, z);
        while (i < 20)
        {
            x = UnityEngine.Random.Range(mindistance, maxdistance);
            xneg = (UnityEngine.Random.Range(1, 3) < 2);
            z = UnityEngine.Random.Range(mindistance, maxdistance);
            zneg = (UnityEngine.Random.Range(1, 3) < 2);
            newposition.x = (xneg ? (from.x - x) : (from.x + x));
            newposition.z = (zneg ? (from.z - z) : (from.z + z));
            return newposition;
            i++;
        }
        return newposition;
    }

How are we meant to format the code in this forum? I clicked “code editor” but it’s still not indented.

There is also an example in the scripting API :

for inserting code : click the “Insert button”, the one between “Media” and “Drafts” …

1 Like

Thanks, I have re-edited my post.

1 Like