Analyzing waypoints by cover and visibility

When dealing with military games, especially FPS, we need to define a waypoint value, by its capacity, to be a good cover point with the maximum visibility for shooting or reaching other enemies visually. This recipe helps us compute a waypoint's value given these parameters.

Getting ready

We need to create a function for checking whether a position is in the same room as others:

public bool IsInSameRoom(Vector3 from, Vector3 location, string tagWall = "Wall")
{
    RaycastHit[] hits;
    Vector3 direction = location - from;
    float rayLength = direction.magnitude;
    direction.Normalize();
    Ray ray = new Ray(from, direction);
    hits = Physics.RaycastAll(ray, rayLength);
    foreach (RaycastHit h in hits)
    {
        string tagObj = h.collider.gameObject.tag;
        if (tagObj.Equals(tagWall))
            return false;
    }
    return true;
}

How to do it…

We will create the function that computes the quality of the waypoint:

  1. Define the function with its parameters:
    public static float GetCoverQuality(
            Vector3 location,
            int iterations,
            Vector3 characterSize,
            float radius,
            float randomRadius,
            float deltaAngle)
    {
        // next steps
    }
  2. Initialize the variable for handling the degrees of rotation, possible hits received, and valid visibility:
    float theta = 0f;
    int hits = 0;
    int valid = 0;
  3. Start the main loop for the iterations to be computed on this waypoint and return the computed value:
    for (int i = 0; i < iterations; i++)
    {
        // next steps
    }
    return (float)(hits / valid);
  4. Create a random position near the waypoint's origin to see if the waypoint is easily reachable:
    Vector3 from = location;
    float randomBinomial = Random.Range(-1f, 1f);
    from.x += radius * Mathf.Cos(theta) + randomBinomial * randomRadius;
    from.y += Random.value * 2f * randomRadius;
    from.z += radius * Mathf.Sin(theta) + randomBinomial * randomRadius;
  5. Check whether the random position is in the same room:
    if (!IsInSameRoom(from, location))
        continue;
    valid++;
  6. If the random position in the same room, then:
    Vector3 to = location;
    to.x += Random.Range(-1f, 1f) * characterSize.x;
    to.y += Random.value * characterSize.y;
    to.z += Random.Range(-1f, 1f) * characterSize.z;
  7. Cast a ray to the visibility value to check whether:
    Vector3 direction = to - location;
    float distance = direction.magnitude;
    direction.Normalize();
    Ray ray = new Ray(location, direction);
    if (Physics.Raycast(ray, distance))
        hits++;
    theta = Mathf.Deg2Rad * deltaAngle;

How it works…

We create a number of iterations and then start putting random numbers around the waypoint to verify that it is reachable and hittable. After that, we compute a coefficient to determine its quality.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.47.166