[英]Unity 2D - How to play death animation prefab
我已經用精靈表創建了一個帶有動畫的預制件,我想在玩家死亡時播放它。 我通過在場景中拖動它來檢查預制件是否正常工作,並且它正在循環播放精靈表的每一幀。
現在我想在播放器死亡時播放這個預制件,並在它結束后將其銷毀,但到目前為止我只能將它放置在播放器死亡的地方,並且它永遠停留在那里。 發生這種情況時也會出現一些錯誤。
這是死亡劇本:
public class DmgByCollisionEnemy : MonoBehaviour {
public GameObject deathAnimation;
void Die() {
deathAnimation = (GameObject) Instantiate(deathAnimation, transform.position, transform.rotation);
//Destroy(deathAnimation);
Destroy(gameObject);
}
}
我通過在Unity界面中拖動預制件來設置deathAnimation。
我在Die()
方法觸發時得到的錯誤是
UnassignedReferenceException: The variable deathAnimation of DmgByCollisionEnemy has not been assigned.
You probably need to assign the deathAnimation variable of the DmgByCollisionEnemy script in the inspector.
那我怎么能這樣做呢?
您可以嘗試將簡單的銷毀腳本添加到您的死亡動畫對象中,該對象會在一段時間內銷毀對象或在動畫中觸發它( Unity手冊:使用動畫事件 )。 當您實例化對象時,它將出現在所需的位置,無論“主”對象如何,它都將被破壞。
像這樣銷毀腳本:
void DestroyMyObject()
{
Destroy(gameObject);
}
腳本運行時間:
void Start()
{
Invoke ("DestroyMyObject", 1f);
}
void DestroyMyObject()
{
Destroy(gameObject);
}
產卵腳本:
using UnityEngine;
using System.Collections;
public class SpawnExtra : MonoBehaviour {
public GameObject deathAnimation;
public static SpawnExtra instance;
void Start ()
{
instance = this;
}
public void SpawnDeathAnimation(Vector3 position)
{
Instantiate (deathAnimation, position, Quaternion.identity);
}
}
當你想要產生這樣的附加對象時,你可以使用它:
SpawnExtra.instance.SpawnDeathAnimation (transform.position);
現在你必須添加gameobject,例如ExtrasController,在其上添加腳本,你可以產生你想要的任何東西。 請記住在檢查器中拖放動畫預制件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.