Navigation point generator

Hello, im a noob trying to code a method that generates points equally distanced from each other contained in a Box Collider. The code below spawns all the points in a single position a not even in the Box Collider.

Thanks for the help

public class Grid : MonoBehaviour
{
    public BoxCollider navArea;          
    public GameObject navPointPrefab;   
    [SerializeField] int numberOfPoints = 5;     
    int spaceBtwPoints;
    
    private void OnValidate()
    {
        GenerateNavigationPoints();
    }

    private void GenerateNavigationPoints()
    {
        int origineX = (int)(navArea.center.x - navArea.size.x / 2);
        int origineZ = (int)(navArea.center.z - navArea.size.z / 2);
        
        spaceBtwPoints = (int)(navArea.size.x / numberOfPoints);

        if (navArea != null && navPointPrefab != null)
        {
            for (int i = 0; i < numberOfPoints; i++)
            {
                for(int j = 0; j < numberOfPoints; j++)
                {
                    Vector3 position = new(origineX, 0 ,origineZ);

                    Instantiate(navPointPrefab, position, Quaternion.identity);
                    origineX =+ spaceBtwPoints;
                }
                origineZ =+ spaceBtwPoints;
            }
        }
    }
}
1 Like

If your navArea size is too small (less units than numberOfPoints, or less than 1 unit) than your spaceBtwPoints (or origineX and origineZ) would become 0 after conversion to int. Because float to int conversion removes the part of the number that goes after the point.

float someFloat = 0.7f;
int someInt = (int)someFloat;               // 0

No need to convert those variables to int. Try to use float as is.