- Home /
How do i delay the first attack after getting Line Of Sight with an enemy?
Basically i want my Unit to have a delay when switching Targets, or when first attacking an enemy, imy implementation already has a cooldown in between attacks but i want it to have a delay as if getting ready to attack a new enemy.
void Update()
{
Collider[] inRange = Physics.OverlapSphere(transform.position, 50f);
int i = 0;
while (i < inRange.Length)
{
if (inRange[i].tag != transform.tag && inRange[i].tag != "LEVEL")
{
RaycastHit LineofSight;
if (Physics.Raycast(transform.position, inRange[i].transform.position- transform.position, out LineofSight, range))
{
if (LineofSight.transform.tag != gameObject.tag && LineofSight.transform.tag != "LEVEL" && (Vector3.Magnitude(inRange[i].transform.position - transform.position) <= MinDistance))
{
MinDistance = Vector3.Magnitude(inRange[i].transform.position - transform.position);
Target = LineofSight.transform.gameObject;
canSpotEnemy = true;
hasTarget = true;
}
}
}
i++;
}
if (hasTarget)
{
Attack(Target);
hasTarget = false;
Target = null;
MinDistance = Mathf.Infinity;
}
}
void Attack(GameObject target)
{
Health health = target.GetComponent<Health>();
if (health != null && Time.time-attackTime+attackRampup > 0 )
{
attackTime = Time.time + attackCooldown;
health.TakeDamage(attackDamage);
Debug.Log(target.name + transform.gameObject.name);
Debug.DrawRay(transform.position, target.transform.position - transform.position, Color.green);
}
}
Answer by Le-Capitaine · Jan 24, 2020 at 02:27 PM
You could add a public value for how long to wait upon spotting a target, then use the same system as cooldown using that value. If you want the two to stack, you can simply add it to the current cooldown value. If you're using a coroutine, you can declare a Coroutine instance, assign your coroutine to it, then stop and restart it with the relevant time.
I ended up going for a different aproach(question was under moderation for a couple days), that ultimately worked, i trigered an attack animation and used the animation to trigger the attack function.
Your answer
Follow this Question
Related Questions
Different attacks using same keybindings? 2 Answers
Timed Attack System 1 Answer
How can I make an event happen every 2 seconds in the OnTriggerStay void? 2 Answers
How do I fix an issue with my enemy instantly dying? 1 Answer
Enemy AI problems 2 Answers