- Home /
How come my Player, Enemy, Animation & HealthSlider won't correspond to one another?
I am lost... everything makes sense when I read it, but I am not sure why it is not getting the damage from the enemy and decreasing my player's health bar. My hurt animation is going crazy too. Its triggered right at the start. When I set the trigger on "Any status" So does that mean it's triggering because i have an idle animation?
Answer by IronBytes · Mar 28, 2019 at 08:47 AM
In OpossumAttack.cs you are fetching the PlayerHealth on the Opossum Game Object in line 19. Use "Target.GetComponent()" instead to fetch it from the player Game Object.
Also you are calling TakenDamage in the Update function from your PlayerHealth script, so the player takes damage every frame no matter if there is an enemy nearby. You should remove it from the Update loop. (Player Health line 31)
Let me know how it goes as it may not fix all of the problems. Cheers.
That worked. Can you help me with my hurt and screen damage Flash Functions?
I do not have much experience with animations, so I'd need to fork your GitHub project and play around with it a bit. I won't be home over the weekend though so you'll have to wait until next week. $$anonymous$$aybe someone else can jump in in the meanwhile ;)
That's fine I'll try to figure it out and then come back here if it's still messed up.
Good $$anonymous$$orning, so I checked out your project today and the problem with your animation is that the hurting animation is never turned off. It is triggered when the player takes damage and then goes on forever because the boolean "hurting" trigger is never set to false afterwards.
So I made a quick function that resets the animation after a short amount of time, which is called in TakeDmg:
public void TakenDmg(int amount)
{
damage = true;
currentHealth -= amount;
healthSlider.value = currentHealth;
anim.SetBool("Hurting", true);
StartCoroutine(ResetHurtAnimationInSeconds(1));
}
public IEnumerator ResetHurtAnimationInSeconds(float timeUntilReset)
{
yield return new WaitForSeconds(timeUntilReset);
anim.SetBool("Hurting", false);
}
I tried this over the weekend I think its the same idea, but my code did not work. Do I need to make a new function?
public void TakenDmg(int amount)
{
damage = true;
currentHealth -= amount;
healthSlider.value = currentHealth;
anim.SetBool("Hurting", true);
if (!damage)
{
anim.SetBool("Hurting", false);
}
//if (currentHealth <= 0 ) {
// Death();
//}
}
Code is always executed from top to bottom. So in your code when TakenDmg is called 'damage' is first set to 'true' and then you check with if(!damage) whether it is 'false'. This if-statement will never become true so the code inside it is never executed. If you want to trigger "Hurting" manually, then you need to place the code outside the TakenDmg function and call it somewhere else.
You had the right idea though, setting "Hurting" to false.
If you have some time I would suggest you to do a beginner coding tutorial. Space Shooter and Survival Shooter are good options. When you do that, take some time to understand why the code works and how it operates. I think it can help you a lot moving forward with your project.
Your answer
Follow this Question
Related Questions
How To Make Enemy Chase You? 1 Answer
Why enemy animation messes up the player ? 0 Answers
Enemies push player a ridiculous amount. 1 Answer
How to make this GUITexture work. 1 Answer
Animated Object phases through Player 2 Answers