学习目标:
实现人物死亡动画+代码
参考视频:秦无邪OvO的个人空间_哔哩哔哩_Bilibili秦无邪OvO,独立游戏开发者/美术/编曲;秦无邪OvO的主页、动态、视频、专栏、频道、收藏、订阅等。哔哩哔哩Bilibili,你感兴趣的视频都在B站。 https://space.bilibili.com/335835274?from=search&seid=4530647365121956206&spm_id_from=333.337.0.0CSDN https://mp.csdn.net/mp_blog/creation/editor/122708406
学习内容:
最主要的还是要先把动画做好,第一次做动画没什么经验,画布比较小是64*32,建议做人物画布建立大一点,做好后测试一下能不能运行  
代码部分:
在PlayerHealth的C#脚本中,当health <= 0的时候,调用动画的名字叫die的trigger,播放动画后延迟调用销毁对象。
PlayerHealth:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerHealth : MonoBehaviour { ? ? public int health; ? ? public float blinkTime; ? ? public int blinks; ? ? public float dieTime;
? ? private Renderer myRender; ? ? private Animator myAnim; ? ? void Start() ? ? { ? ? ? ? myRender = GetComponent<Renderer>(); ? ? ? ? myAnim = GetComponent<Animator>(); ? ? }
? ? // Update is called once per frame ? ? void Update() ? ? { ? ? ? ?? ? ? }
? ? public void DamagePlayer(int damage) ? ? { ? ? ? ? health -= damage; ? ? ? ? if(health <= 0) ? ? ? ? { ? ? ? ? ? ? myAnim.SetTrigger("Die"); ? ? ? ? ? ? Invoke("KillPlayer", dieTime); ? ? ? ? } ? ? ? ? BlinkPlayer(blinks,blinkTime); ? ? }
? ? void KillPlayer() ? ? { ? ? ? ? Destroy(gameObject); ? ? } ? ? void BlinkPlayer(int numBlinks,float seconds) ? ? { ? ? ? ? StartCoroutine(DoBlinks(numBlinks, seconds)); ? ? } ? ? IEnumerator DoBlinks(int numBlinks, float seconds) ? ? { ? ? ? ? for (int i = 0; i < numBlinks * 2; i++) ? ? ? ? { ? ? ? ? ? ? myRender.enabled = !myRender.enabled; ? ? ? ? ? ? yield return new WaitForSeconds(seconds); ? ? ? ? } ? ? ? ? myRender.enabled = true; ? ? } } ?
学习产出:

?播放成坤
|