Here’s my Code
void RemoveMovingEntities(Vector2Int gridPos)
{
//---------------------------Setup Spherecast-----------------------------
//https://docs.unity3d.com/ScriptReference/Physics.SphereCast.html
//Setup Layermask - https://docs.unity3d.com/Manual/layermask-set.html
int layer = 8; //The Layer "MovingEntities"
int layerMask = (1 << layer); //Apparently this works. This is a weird corner of the API, Unity manual itself says to do this.
//Setup Spherecast
//RaycastHit API - https://docs.unity3d.com/ScriptReference/RaycastHit.html
RaycastHit hit;
Vector3 origin = tilemapManager.GetWorldPosition3(gridPos, -10f);
MonoBehaviour.print(origin);
float radius = 0.5f;
Vector3 direction = Vector3.forward;
float maxDistance = 15f;
QueryTriggerInteraction triggerSetting = QueryTriggerInteraction.Collide;
//Spherecast
if(Physics.SphereCast(origin, radius, direction, out hit, maxDistance, layerMask, triggerSetting))
{
Object.Destroy(hit.transform.gameObject);
}
}
When i use the remove tool and click on a tile, this function is called (have confirmed this via Debug.Log). I want it to find and destroy any moving entities on said tile.
Things i’ve checked:
-The Entities i want to remove are indeed on layer 8.
-The origin is correct, for example if a slime is on tile centred 0.5, 0.5, it will be 0.5, 0.5, -10.
-Turning up the radius to laughably high values (20) does nothing, as does the max distance (100) (the entities are at z=0, confirmed)
- Trying Vector3.back for the direction doesn’t help.
- The code inside the if statement is not reached.
one thing i noticed in the API is that “SphereCast will not detect colliders for which the sphere overlaps the collider.” I have no idea what this means, so if anyone could clarify what this means as well that’d be great, it may or may not be related to the issue.