void Raycast_SeeEnemy()
{
RaycastHit2D[] hits = GetAroundHits(gameObject.transform.position, 16, enemy, SeeDistance);
if (hits[0].collider != null)
{
target = hits[0].transform.gameObject;
SeeEnemy = true;
}
else if (hits[0].collider == null)
SeeEnemy = false;
if (transform.position.x > target.transform.position.x && horizontalMove)
Flip();
else if (transform.position.x < target.transform.position.x && !horizontalMove)
Flip();
}
private RaycastHit2D[] GetAroundHits(Vector2 origin, int count, LayerMask mask, float distance = Mathf.Infinity)
{
float angleStep = 360f / count;
var hits = new RaycastHit2D[count];
for (int i = 0; i < count; i++)
{
float angle = i * angleStep;
float radian = angle * Mathf.Deg2Rad;
Vector2 direction = new Vector2(Mathf.Cos(radian), Mathf.Sin(radian));
hits[i] = Physics2D.Raycast(origin, direction, distance, mask);
}
return hits;
}