Unity2D:播放hearts图像UI动画

时间:2018-08-01 22:17:28

标签: c# unity3d animation

我一直在遵循YouTube上的本教程,内容涉及如何为播放器创建播放器健康系统UI;我创建了一个脚本,该脚本控制心形图像组件以在玩家被射击时(当curenthealth下降时)更改源图像。我想做的是向播放器的心脏图像UI添加动画,每次播放器失去一颗心时,我都希望播放动画。但是,我不确定该怎么做?有人可以帮我吗,谢谢StackOverflow社区!

这是我的HUD脚本:

public Sprite[] HeartSprites;
public Image HeartUI;
public PlayerHealth playerHealth;
public bool playerIsActive = false;


void Update () {
    if (playerIsActive == true) {
        if (playerHealth != null) {
            HeartUI.sprite = HeartSprites [playerHealth.curHealth];
        }
    }
}

public void ActivatePlayerHealth()
{
    playerIsActive = true;
}

这是我玩家的健康脚本:

public int curHealth;
public int maxHealth = 1;

void Start () {
    curHealth = maxHealth;
    death = false;
    if (PlayerPrefs.HasKey ("Lives")) {
        curHealth = PlayerPrefs.GetInt ("Lives");
        curHealth = 1;
    }
}

void Update () {
    if (curHealth <= 0) {
        gm.Die ();
    }
}

public void Damage(int dmg)
{
    curHealth -= dmg;
    if(curHealth <0)
        curHealth = 0;
}

0 个答案:

没有答案