如何解决当玩家死亡后重新启动游戏时,Collider2d 不工作
我已经检查了游戏内重启按钮,它就像一个魅力,玩家仍然会被障碍物碰撞而受到伤害,但是当玩家死亡并按下重启按钮时,玩家不会与任何障碍物碰撞,
所以我认为这是因为我的“无效死亡”,但我只禁用了一些,当我重新启动游戏时它仍然有效,只有玩家不会与任何障碍物相撞,只是......帮帮我!
public void Die() {
//Disable Score
ScoreCounter.SetActive(false);
//Animation
AN.Play("Death");
//Stop Coroutine
StopCoroutine("Invulnerability");
//Load Restart Menu
PanelRestart.SetActive(true);
//Disable Object Function
GetComponent<CapsuleCollider2D>().enabled = false;
this.enabled = false;
}
这是我的暂停按钮脚本,我在游戏中使用了暂停按钮然后重新启动,以及当玩家死亡然后重新启动
public void RestartButton() {
SceneManager.LoadScene(1);
}
解决方法
当我重新启动游戏时,只有玩家不会与任何 障碍
很可能不是您的碰撞问题,而是您的 StopCoroutine()
调用。因为它目前不会停止玩家的 "Invulnerability"
,因此一旦他碰撞他就不会死亡。
要解决您需要像这样调整 StopCoroutine()
调用的问题,您可以:
- 将
IEnumerator
方法保留为私有成员。 - 将开始的
Coroutine
保留为私人成员。
IEnumerator 示例:
// Keep the executing script as a private variable.
// This is needed to stop it with StopCoroutine.
private IEnumerator coroutine;
void Start() {
coroutine = ExampleIEnumerator();
// Start the given Coroutine.
StartCoroutine(coroutine);
// Stop the given Coroutine.
StopCoroutine(coroutine);
}
private IEnumerator ExampleIEnumerator() {
yield return new WaitForSeconds(1f);
}
协程示例:
// Keep the executed coroutine as a private variable.
// This is needed to stop it with StopCoroutine.
private Coroutine coroutine;
void Start() {
// Start the given Coroutine.
coroutine = StartCoroutine(ExampleIEnumerator());
// Stop the given Coroutine.
StopCoroutine(coroutine);
}
private IEnumerator ExampleIEnumerator() {
yield return new WaitForSeconds(1f);
}
此外,您还需要在死后禁用 CapsuleCollider2D
后重新启用它。
GetComponent<CapsuleCollider2D>().enabled = true;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。