Hi guys, still very new at this, making a hack and slash 2d game. So right now I have the slashing to work like this:
RaycastHit2D hit = Physics2D.Raycast (position, transform.position);
if (hit.collider.gameObject.tag == "Ogre")
{
Debug.Log ("You made a hit!");
dmg.Hit ();
}
And this is in the enemy class:
public void Hit()
{
enemy_cur -= dmg;
}
So when the mouse goes over the enemy collider, the current health of the enemy drops by 0.05f. This works fine, but the issue that I can’t currently wrap my head around is that because it detects collision and is constantly updating if i just leave the mouse over the monster, its health will drop without stopping. How do I prevent that? How can I limit one slash to one collide, thus lowering the enemy’s health once, until another slash comes in. I’ve tried experimenting with a timer cooldown, but it makes the game less smooth to play as there’s now a waiting period before the next attack.
Any help would be appreciated. Thanks guys.
EDIT: Would having an if check for entering and exiting the collision be a sufficient solution? I’m now trying to implement this but I seem to be having trouble adding it into the hit.collider if.
Provide more details about your class (Collision functions, ...)
– Hellium