public class movement : MonoBehaviour
{
[SerializeField]
GameObject point;
public float damage = 5.0f;
bool isContact = false;
GameObject enemy;
GameObject castle;
bool nextCastle = false;
bool canAttack = true;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void FixedUpdate()
{
float dist = Vector2.Distance(point.transform.position, transform.position);
if (dist > 2.5f)
{
if (isContact && canAttack)
{
Health h = enemy.GetComponent<Health>();
h.health_(damage);
StartCoroutine(damageDelay());
isContact = false;
}
else
{
transform.position = new Vector2(transform.position.x - 0.1f, transform.position.y);
}
}
else if (nextCastle && canAttack)
{
CastleHealth cH = castle.GetComponent<CastleHealth>();
cH.AddHealth(damage);
StartCoroutine(damageDelay());
}
}
void OnTriggerEnter2D(Collider2D collider)
{
Debug.Log("Тут что-то не так!");
if (collider.tag == "Bot")
{
isContact = true;
enemy = collider.gameObject;
}
else if(collider.tag == "Castle")
{
nextCastle = true;
castle = collider.gameObject;
}
}
IEnumerator damageDelay()
{
canAttack = false;
yield return new WaitForSeconds(0.5f);
canAttack = true;
}
}