TLDR: Physics.OverlapSphere does not detect dynamically generated mesh/collider in webGL build.
I am messing around with boids and have a fun little simulation running in unity. In the editor everything works, however, when I create a webGL build only select parts of the boid behavior works. The boid behavior that is independent of other boids, like obstacle avoidance, works. The boid behavior that is dependent on neighboring boids does not. I’ve figured out that this is because the boids aren’t able to detect eachother.
Each boid generates a list of visible targets (neighboring boids) as follows
Collider[] targetsInViewRadius = Physics.OverlapSphere(transform.position, viewRadius, boidMask);
This works in the editor, but in the build targetsInViewRadius is never populated. I believe I have narrowed the issue down to Physics.OverlapSphere not picking up the dynamically generated mesh/collider I am creating for each boid. Plus I’ve never tried mesh generation before so my best guess is that the mistake is there.
I’ve looked at a couple posts with similar issues that stemmed from mesh.isReadable being false. But from Unity - Scripting API: Mesh.isReadable, t “A dynamic Mesh created from script always returns true” which I doubled checked. I’m still pretty new to unity and have no clue what the issue is or where to look next so I’ve included my mesh generation bellow. Any help would be greatly appreciated! Thanks!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ConeGenerator : MonoBehaviour
{
public float lengthToRadiusRatio;
public float scale;
public int vertexCount;
public Material coneMaterial;
public LayerMask targetMask;
Mesh coneMesh;
MeshFilter coneMeshFilter;
MeshRenderer coneRenderer;
void Awake()
{
//create child to hold mesh
GameObject coneVizualizer = new GameObject("ConeVizualizer");
coneVizualizer.transform.parent = this.transform;
coneVizualizer.transform.localPosition = Vector3.back * .001f;//offset form Vector3.zero so boid is rendered on top of its fov. uses sphere collider so this is okay
coneVizualizer.transform.localRotation = Quaternion.identity;
//create mesh
coneMeshFilter = coneVizualizer.AddComponent<MeshFilter>();
coneRenderer = coneVizualizer.AddComponent<MeshRenderer>();
coneMesh = new Mesh();
coneMesh.name = "coneMesh";
coneMeshFilter.mesh = coneMesh;
coneRenderer.material = coneMaterial;
//set color
Color color = coneRenderer.material.color;
float colorIntensity = (color.r + color.g + color.b) / 3f;
float factor = colorIntensity + Random.Range(colorIntensity * .7f, colorIntensity * 1.7f);
coneRenderer.material.color = new Color(color.r, color.g * factor, color.b, color.a);
GenerateConeMesh();
coneVizualizer.AddComponent<MeshCollider>();
//thank you https://answers.unity.com/questions/1288179/layer-layermask-which-is-set-in-inspector.html
coneVizualizer.layer = (int) Mathf.Log(targetMask.value, 2);
}
void GenerateConeMesh()
{
coneMesh.Clear();
coneMesh.vertices = new Vector3[]
{
Vector3.up * scale,
(Vector3.right * 1/lengthToRadiusRatio - Vector3.up) * scale,
(Vector3.left * 1/lengthToRadiusRatio - Vector3.up) * scale
};
coneMesh.triangles = new int[]
{
0, 1, 2
};
coneMesh.RecalculateNormals();
}
}