- Home /
This question was
closed Feb 19 at 09:29 PM by
mav1704h for the following reason:
Problem is not reproducible or outdated
Player respawn not working like intended
My "PlayerDeath" animation has a AnimationEvent that starts PlayerDeath(), all works fine, but the animation plays twice for some reason. So two deathGraves get created and the character health is 1/4 of its max value and not 1/2. I am new to programming, so maybe I missed something. Help would be very appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CharacterCombat : MonoBehaviour
{
public int maxHealthPlayer = 200;
public int currentHealthPlayer;
public HealthBar2 healthBar2;
public Animator animator;
public GameObject deathGrave;
public Transform characterObject;
int notHealthy;
void Start()
{
currentHealthPlayer = maxHealthPlayer;
healthBar2.SetMaxHealth(currentHealthPlayer);
notHealthy = maxHealthPlayer/2;
}
void Update()
{
if (currentHealthPlayer <= 0)
{
DeathAnimation();
Debug.Log("CharacterDeath");
}
}
public void TakeDamageP(int damageP)
{
currentHealthPlayer -= damageP;
healthBar2.SetHealth(currentHealthPlayer);
animator.SetTrigger("GotHit");
}
public void PlayerDeath()
{
Instantiate(deathGrave, characterObject.position, characterObject.rotation);
currentHealthPlayer = notHealthy;
notHealthy = currentHealthPlayer/2;
healthBar2.SetHealth(currentHealthPlayer);
}
void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.tag == "WorldTrap")
{
TakeDamageP(20);
}
}
void DeathAnimation()
{
animator.SetTrigger("PlayerDeath");
}
}
Comment
Answer by SellPet · Feb 14 at 12:54 PM
Im too lazy to check whole script man, but i would recommend adding bool, add it to if statement where you call the death method, and set it to true whenever you die. so death happens only once. same with health. then reset if you respawn