- Home /
Question by
dree213 · Dec 16, 2019 at 08:51 PM ·
animationcollisionspritevisual studio
how can I program so that every time an object loses life an animation appears?
have this code. I want that when the life of gem decrease appear an sprite animation by is side.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GemControl : MonoBehaviour
{
private int lifes = 10;
public PlayerControl control;
public Image lifeBar;
public GameObject Prop;
public void SetLifes(int _value)
{
lifes -= _value;
if (lifes <= 0)
{
control.GameOver();
}
else if (lifes > 10) lifes = 10;
lifeBar.fillAmount = (float)lifes / 10;
}
}
thank you
Comment
Answer by Wolfgang_Brabaender · Dec 17, 2019 at 09:00 AM
Hi,
You will need an animator and an animation for that. Look at the docs how to use them. Once you have them in place, you could work with a trigger parameter (inside the animator).
The method couls then look sth. like that, assuming you have the Trigger "LifeLoss" in your Animator:
Animator _animator;
public void SetLifes(int _value){
if(_value > 0){
this._animator.SetTrigger("LifeLoss");
}
//other behaviour
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612220235im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Trigger Animation doesn't work, please help me. 1 Answer
Strange 2D sprite behavior 0 Answers
2D animation collision 1 Answer
2D sprite animation issue 0 Answers
Compile error with animator 1 Answer