How do i make so when the enemy comes within a attack radius its damaging the target there is 2 different targets there is a building and there is a player so how do i make it attack the target there is within attack radius this is the script
void Update()
{
TargetFinder();
if (currentTargetDistance < attackRadius)
Attack();
if (myTarget == null)
currentTargetDistance = searchRadius;
else
Move(myTarget.transform);
if (CurrentHealth < DeathHealth)
{
print("Enemy Has died!!!");
Destroy(GroundEnemy);
}
print("Enemy took some damage");
}
void TargetFinder()
{
foreach (var x in Targets)
{
float distance = (transform.position - x.transform.position).magnitude;
if (distance < currentTargetDistance)
{
myTarget = x;
displayText.text = "My target: " + x + "
Distance to Target: " + distance; //skal fjernes nå det er helt klart
currentTargetDistance = distance;
}
}
}
void Move(Transform t)
{
transform.LookAt(t);
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
}
void Attack()
{
Targets.Remove(myTarget);
Destroy(myTarget);
myTarget = null;
currentTargetDistance = searchRadius;
}