Having trouble with melee attack script
Here's the aforementioned script:
public float attackDuration = 0.3F;
public bool attacking;
[HideInInspector]
void Start()
{
}
void Update()
{
if (Input.GetButtonDown("Attack"))
{
attacking = true;
}
EnableDamage();
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag =="Enemy")
{
if (col.tag == "Enemy")
{
col.SendMessage("LevaDano", 1, SendMessageOptions.DontRequireReceiver);
}
}
}
void EnableDamage()
{
if (attacking == true) return;
attacking = true;
StartCoroutine("DisableDamage");
}
IEnumerator DisableDamage()
{
yield return new WaitForSeconds(attackDuration);
attacking = false;
}
The main problem is that i can't get attacking
back to false
no matter what. How to proceed? Also, feel free to warn me about any other bugs.
Answer by Cynikal · Nov 03, 2016 at 11:41 PM
You can't get it back to false because you're consistently setting it to true, and never back to false.
So, On your Update()
You have: If Attack, Set attacking to True.
You also have it running "EnableDamage" on every frame.
If Atacking is true you break out of it. Which, it will be always. DisableDamage will never trigger.
Try this:
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag =="Enemy")
{
if (col.tag == "Enemy")
{
col.SendMessage("LevaDano", 1, SendMessageOptions.DontRequireReceiver);
}
}
attacking = false; //Will ALWAYS set to false even if they dont hit their desired target.
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612100010im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Calculate BoxCollider2D based on the actual player sprite 2 Answers
How to fix knockback in 2D melee combat system 0 Answers
Unity 2D: Glitchy Collision/Triggers Problem 0 Answers
Problem with android collisions and fixed time 0 Answers
Bounds and extents of a tilemap collider across differing y axes, Tilemap collider bounds 0 Answers