Triggering a Player Hit Animation

Hi. My hit/taking damage animation is not working. Here is the code I’m trying to use:

	void OnCollisionEnter2D(Collision2D collision)
	{
		if (collision.gameObject.tag == "Enemy") // If colliding with enemy, execute this block
		{
			HealthScript playerHealth = this.GetComponent<HealthScript> ();
			playerHealth.Damage (1);
			animator.SetTrigger ("Hit");
		}
	}
}

bool playerHit = false;
if (playerHit && collision.gameObject.tag == “Enemy”){
animator.SetTrigger (“Hit”);

You are setting playerHit to false, so the following if-statement will never execute.

Do you need the playerHit variable? If this script is on your player, then the fact that the OnCollisionEnter2D method is executing is enough to know that your player got “hit” by something. It seems counter-intuitive to set playerHit to false there. You should be fine by removing the

 bool playerHit = false;

line, and changing the if-statement to

if (collision.gameObject.tag == "Enemy")
   animator.SetTrigger ("Hit");

Another simple fix is to just set ‘playerHit’ to true:

bool playerHit = true;

It would make more sense to set it to true rather than false, but the variable is still redundant if you’re not using it someplace else

UPDATE:

That can’t be your entire method, because you’re missing a couple of ending brackets. I’m going to assume this is how your code is structured (and I added comments):

void OnCollisionEnter2D(Collision2D collision){
			bool damagePlayer = false;

			// Collision with enemy
			EnemyScript enemy = collision.gameObject.GetComponent<EnemyScript> ();
			 /* You shouldn't do this, because if the gameobject you're colliding with doesn't have
			 * the EnemyScript, this line will create a runtime exception. Since you're checking for
			 * null in the following if-statement, I think it works, but it's not the best way to do it */

			if (enemy != null) {

				// Damage the player
				 /* Your method runs line by line from top to bottom. Since you are setting damagePlayer
				 * to false at the start of the method (and you don't change it after that), the following
				 * if-statement will never be true, so that whole block will never execute */
				if (damagePlayer) // This is always false {
					HealthScript playerHealth = this.GetComponent<HealthScript> (); // This will never execute

					if (playerHealth != null)	// Nor will this
						playerHealth.Damage (1);// Nor that
				}
				
				if (collision.gameObject.tag == "Enemy") {
					animator.SetTrigger ("Hit");
				}
			}
}

Wont the following method suffice?

void OnCollisionEnter2D(Collision2D collision)
		{
			if (collision.gameObject.tag == "Enemy") // If colliding with enemy, execute this block
			{
				HealthScript playerHealth = this.GetComponent<HealthScript> ();
				playerHealth.Damage (1);
				animator.SetTrigger ("Hit");
			}
		}

I realize there is a bit more here, but this is what ended up working for me (in case it can help anyone in the future):

 void OnCollisionEnter2D(Collision2D collision)
    	{
    		if (!invul && collision.gameObject.tag == "Enemy") { // If colliding with enemy, execute this block
    						HealthScript playerHealth = this.GetComponent<HealthScript> (); //get player health script
    						playerHealth.Damage (1); //damage player
    						invul = true; //player is invulnerable
    						StartCoroutine (InvulWait ());
    		
    		}
    	}
    	private IEnumerator InvulWait() {
    		animator.SetTrigger ("Hit"); //play hit animation
    		yield return new WaitForSeconds (2); //invul time
    		invul = false;
    		}