如何使 StartCoroutine 统一工作?
How can I make the StartCoroutine working in unity?
我是 c# 的新手,我正在尝试制作一个平台,当玩家站在平台上时它会掉下来。我使用了 StartCoroutine,所以平台会在五秒后掉下来,但由于某种原因我的协程无法正常工作。
这是代码:
public GameObject player;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
rb.useGravity = false;
//rb.isKinematic = false;
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "player")
{
StartCoroutine(ObjectFall());
}
}
IEnumerator ObjectFall()
{
yield return new WaitForSeconds(5f);
Debug.Log("Its working");
this.rb.useGravity = true;
//this.rb.isKinematic = true;
}
}
如果函数没有参数,则不需要使用 IEnumerable。您可以使用 Invoke("ObjectFall", 5) 并将您的 IEnumerable 变为 void 而不会产生。它可能不是问题的根源,但请尝试一下。您游戏中的比例可能是错误的,这就是质量 1000 起作用的原因。尝试为整个场景固定比例。记住1个单位=1米或多或少
我是 c# 的新手,我正在尝试制作一个平台,当玩家站在平台上时它会掉下来。我使用了 StartCoroutine,所以平台会在五秒后掉下来,但由于某种原因我的协程无法正常工作。
这是代码:
public GameObject player;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
rb.useGravity = false;
//rb.isKinematic = false;
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "player")
{
StartCoroutine(ObjectFall());
}
}
IEnumerator ObjectFall()
{
yield return new WaitForSeconds(5f);
Debug.Log("Its working");
this.rb.useGravity = true;
//this.rb.isKinematic = true;
}
}
如果函数没有参数,则不需要使用 IEnumerable。您可以使用 Invoke("ObjectFall", 5) 并将您的 IEnumerable 变为 void 而不会产生。它可能不是问题的根源,但请尝试一下。您游戏中的比例可能是错误的,这就是质量 1000 起作用的原因。尝试为整个场景固定比例。记住1个单位=1米或多或少