[英]Load Scene when Enemy Collides with Player-Unity
Unity 2D.统一二维。 I am trying to load EndGameScene when an enemy collides with my player, but I can't seem to get it to work.当敌人与我的玩家发生碰撞时,我试图加载 EndGameScene,但我似乎无法让它工作。
My tags are "Player" and "enemy".我的标签是“玩家”和“敌人”。 This is the current code .这是当前代码。
Code:代码:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class EndGame : MonoBehaviour
{
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "enemy")
{
SceneManager.LoadScene("EndGameScene");
}
}
}
I'm new to Unity (and it's been some time since last time I used it), so this check list may be incomplete, but still worth a try:我是 Unity 的新手(距离我上次使用它已经有一段时间了),所以这个清单可能不完整,但仍然值得一试:
Debug.Log("it collides!")
at the beggining of your OnCollisionEnter function, so you can be sure that it is actually detecting a collition.在 OnCollisionEnter function 的开头放置一个Debug.Log("it collides!")
,这样您就可以确定它实际上是在检测碰撞。If it doesn't print anything:如果它不打印任何东西:
If it prints the message:如果它打印消息:
there's a better way to make the collision: change the name here if (collision.gameObject.name == "enemy")
and make it like this if (collision.gameObject.tag == "Enemy")
and go to the enemys in you scene and add the Enemy tag to them有更好的方法来进行碰撞:在此处更改名称if (collision.gameObject.name == "enemy")
并使其成为if (collision.gameObject.tag == "Enemy")
和 go 中的敌人你在场景中添加 Enemy 标签
It might be because you have not built the scene into the project's build settings (in the unity interface):可能是因为您没有将场景构建到项目的构建设置中(在统一界面中):
Go to the Game Over scene/ open it by going to File>OpenScene>Scenes(folder)>Game Over (scene name) click 'File' (top left corner) click 'Build Settings' option click 'Add Open Scenes button' make sure the scene you want to add is listed and checked click 'Build' (on bottom right) Go 到 Game Over 场景/通过转到 File>OpenScene>Scenes(文件夹)>Game Over(场景名称)打开它点击“文件”(左上角)点击“构建设置”选项点击“添加打开场景按钮”确保您要添加的场景已列出并选中单击“构建”(右下角)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.