- Home /
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");
}
}
}
Answer by Danao · Oct 13, 2014 at 03:29 PM
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;
}
Answer by hatake3 · Sep 21, 2014 at 02:24 AM
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 also have gone through the process from the beginning once again to see if there was any change, but I'm getting the same result.
Thanks for the update hatake3. Sorry, I see I did miss the ending brackets in my original cut/paste. The health/damage here is from a tutorial and somehow it was working (at least to damage the player). Your code is much more efficient though and I replaced my code with yours. However, for some reason the animation still isn't playing despite it looking like everything should be in place! I'll edit my original post and replace the code as is now. Let me know if you have any other ideas of what to look at. Your help is greatly appreciated :)
Is it the pixelnest tutorial? :P Anyway, not sure why your animation is not working. From your original explanation, I couldn't see anything wrong with it... $$anonymous$$aybe upload a picture of the animator and the inspector?