0
\$\begingroup\$

My character dying animation works and when the character lies on the ground, text appears saying "Wasted". I added an object and it respawns.

The main problem is that although he respawns, the character still plays the death animation on the ground after respawning in the new location, as shown in the animation below:

enter image description here

using UnityEngine; using DG.Tweening; using UnityEngine.UI; using UnityEngine.SceneManagement; public class Health : MonoBehaviour { public float curHealth; public float maxHealth; public bool enemyDied = false; private Animator anim; public Slider healthBar; private ThirdPersonCharacter thirdPerson; private void Start() { curHealth = maxHealth; healthBar.minValue = 0; healthBar.value = curHealth; healthBar.maxValue = maxHealth; anim = GetComponent<Animator>(); thirdPerson = GetComponent<ThirdPersonCharacter>(); } void Update() { float hit = anim.GetFloat("hit"); if (hit > 0) { hit -= Time.deltaTime * 3; anim.SetFloat("hit", hit); } if (curHealth < 1) { Physics.SyncTransforms(); anim.SetBool("death", true); } if (Input.GetKeyUp(KeyCode.Space)) { SendDamage(Random.Range(10, 20)); } } public void SendDamage(float damageValue) { curHealth -= damageValue; healthBar.value = curHealth; if (curHealth <= 0) { enemyDied = true; } Invoke(nameof(Resurrect), 2f); anim.SetFloat("hit", 1); } public void Resurrect() { SpriteRenderer blackout = Camera.main.transform.GetComponentInChildren<SpriteRenderer>(); Color orjinalRenkWasted = blackout.color; Color clrWhitealpha0 = new Color(1, 1, 1, 1); Color clr = new Color(0, 0, 0, 1); blackout.transform.GetChild(0).GetComponent<SpriteRenderer>().DOColor(clr, .5f).SetDelay(2f).OnStart(() => { blackout.DOColor(clrWhitealpha0, .5f).SetDelay(.5f).OnComplete(() => { Invoke(nameof(BringEverythingBack), 2f); blackout.transform.GetChild(0).GetComponent<SpriteRenderer>().DOColor(new Color(0, 0, 0, 0), .5f).SetDelay(2.5f).OnStart(() => { blackout.DOColor(orjinalRenkWasted, .25f); }); }); }); } void BringEverythingBack() { transform.root.position = GameObject.FindGameObjectWithTag("Respawn").transform.position; } 
\$\endgroup\$
4
  • 1
    \$\begingroup\$ I see anim.SetBool("death", true); - do you ever set this to "false" anywhere? Do you have a transition out of your death animation back into your regular alive animations? Do you ever reset curhealth so you don't immediately die again? \$\endgroup\$ Commented Oct 13, 2022 at 0:24
  • \$\begingroup\$ Thanks for the answer to my question. No I didn't set anim.SetBool.false but now I wrote anim.Setbool.false somewhere but same situation did not change. \$\endgroup\$ Commented Oct 13, 2022 at 0:42
  • \$\begingroup\$ Please edit your question to show your updated code. Don't forget to answer the other parts of my comment: do you have transitions out of the death state? Do you set curHealth back to a value at or above 1? Be sure the answers to this are visible in your question. \$\endgroup\$ Commented Oct 13, 2022 at 1:10
  • \$\begingroup\$ I've reverted your chains of edits to this question, because they did not add useful information. You were asked to edit the question to show your revised code (including anim.SetBool("death", false), but you did not do that, instead deleting useful parts of the question. This is considered vandalism and an abuse of the edit feature to bump a post without clarifying it. A pattern of abuse like this can result in restrictions to your account, so please follow instructions and do not make meaningless edits in the future. \$\endgroup\$ Commented Oct 19, 2022 at 19:45

1 Answer 1

1
\$\begingroup\$

In your BringEverythingBack() method, you should add code to do the following things:

  • Force the state machine of the animator back to the default state of your animation controller with the anim.Play(stateName) method. You could also do this with transitions as DMGregory suggested in a comment on the question. But I would recommend to just use Play in this case. One reason is because later you might want to use the same method to respawn the character while they are in a completely different animation state. So you would have to create transitions from every possible state back to the default state. Also, the reason you usually use transitions is because they allow you to gradually blend from one animation to another. When the player respawns, you usually don't want to do that. You want them to appear in the default pose immediately. And that's exactly the behavior you get with Play.
  • Clear the die-flag on the animation controller by setting anim.SetBool("Die", false);. Otherwise the animation controller will probably transition right back into the death-animation.
  • Refill the character's health with curHealth = maxHealth, so they don't immediately die again.
\$\endgroup\$
2
  • \$\begingroup\$ I fixed the health issue but there is a small problem. I added health health bar Text, but when the character dies, health text - decreases to minuses. \$\endgroup\$ Commented Oct 27, 2022 at 20:33
  • 1
    \$\begingroup\$ @YusufhanArslan which seems to be a seperat question. You can accept this as correct if it solves the issues from your question and open a new question with your new problem. Dont forget to include enough code to reproduce your new error/ question \$\endgroup\$ Commented Oct 28, 2022 at 7:32

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.