【Unity2D】实现人物死亡动画+代码
學習目標:
實現人物死亡動畫+代碼
參考視頻:秦無邪OvO的個人空間_嗶哩嗶哩_Bilibili秦無邪OvO,獨立游戲開發者/美術/編曲;秦無邪OvO的主頁、動態、視頻、專欄、頻道、收藏、訂閱等。嗶哩嗶哩Bilibili,你感興趣的視頻都在B站。https://space.bilibili.com/335835274?from=search&seid=4530647365121956206&spm_id_from=333.337.0.0CSDNhttps://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;
? ? }
}
?
學習產出:
?播放成坤
總結
以上是生活随笔為你收集整理的【Unity2D】实现人物死亡动画+代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android 光晕动画,充电动画软件下
- 下一篇: 美国对华贸易逆差及其为何如此之高