Hi,
this is my code,
if(targetFood == null)
{
Collider2D[] allFood = Physics2D.OverlapCircleAll(antPosition, viewRadius, 7);
if(allFood.Length > 0)
{
Debug.Log("Find Food");
//Select one food Randomly
Transform food = allFood[Random.Range(0, allFood.Length)].transform;
//Find Dir to Food
Vector2 dirToFood = (food.position - transform.position).normalized;
//Search is it in ant's view angle (Simulating that ant smell that food)
if(Vector2.Angle(transform.position,dirToFood) < viewAngle / 2)
{
food.gameObject.layer = takenFoodLayer;
}
targetFood = food;
}
}
I have foods in my scene and ants randomly move until they find food. I only need to detect foods. I make “Food” layer for foods. and it’s layer 7. but it’s not debug when food in it range. but when I remove 7, it work. but it work for every layers.
have a good day, thanks.