克隆不使用动画师/启用/禁用功能



我正试图让我的敌人在与冰弹接触时冻结。除了动画师不播放动画和脚本没有被启用/禁用之外,一切都不会出错。我尝试过script=GetComponent((;并且anim=gameObject.GetComponent((;该部分也是如此。对于这一集,我尝试了脚本。启用了脚本。详述等等。这些都不起作用。我认为这可能与get组件有关,但与IDK有关。这些都在实例化的预制件上

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public float health;
public float freezeTime;
public Animator anim;
public Enemy script;
// Start is called before the first frame update
IEnumerator Wait(){
yield return new WaitForSeconds (freezeTime);
Debug.Log("waiting over");
}
void Start()
{
anim = gameObject.GetComponent<Animator>();
script = gameObject.GetComponent<Enemy>();
}
// Update is called once per frame
void Update()
{
if(health <= 0){
Dead();
}   
}
void OnTriggerEnter2D(Collider2D other){
if(other.CompareTag("Harmful")){
Destroy(other.gameObject);
TakeDamage();
}
if(other.CompareTag("Freezing")){
anim.SetBool("IsFrozen", true); 
Destroy(other.gameObject);
gameObject.GetComponent<Enemy>().enabled = false;
TakeDamage();
Debug.Log("waiting started");
StartCoroutine(Wait());
anim.SetBool("IsFrozen", false);
gameObject.GetComponent<Enemy>().enabled = true;
}
}
void TakeDamage(){
health -= 1f;
}
void Dead(){
Destroy(gameObject);
}

}

问题实际上不是动画师或脚本设置为false,而是因为我启动了一个等待函数,但它启动了等待函数,并立即运行了应该在等待函数之后运行的代码。太长,读不下去了等待,但代码仍然运行。如果你想要完整的工作代码,它就在这里。间距有点奇怪,因为stackoverflow btw:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyHealth : MonoBehaviour
{
public float health;
public float freezeTime;
public Animator anim;
public Enemy script;
public Rigidbody2D rb;
public bool WaitOver;
// Start is called before the first frame update
IEnumerator Wait(){
yield return new WaitForSeconds (freezeTime);
WaitOver = true;
Debug.Log("waiting over");
}
void Start()
{
anim = gameObject.GetComponent<Animator>();
script = gameObject.GetComponent<Enemy>();
rb = gameObject.GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if(health <= 0){
Dead();
}   
if (WaitOver == true){

anim.SetBool("IsFrozen", false);
gameObject.GetComponent<Enemy>().enabled = true;
rb.isKinematic = false;
}
}
void OnTriggerEnter2D(Collider2D other){
if(other.CompareTag("Harmful")){
Destroy(other.gameObject);
TakeDamage();
}
if(other.CompareTag("Freezing")){
WaitOver = false; 
anim.SetBool("IsFrozen", true); 
rb.isKinematic = true;
Destroy(other.gameObject);
GetComponent<Enemy>().enabled = false;
TakeDamage();
Debug.Log("waiting started");
StartCoroutine(Wait());

}
}
void TakeDamage(){
health -= 1f;
}
void Dead(){
Destroy(gameObject);
}

}

最新更新