简体   繁体   English

当敌人与 Player-Unity 碰撞时加载场景

[英]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 的新手(距离我上次使用它已经有一段时间了),所以这个清单可能不完整,但仍然值得一试:

  • Put a 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:如果它不打印任何东西:

  • Check if you are ussing a collider in both the player and the enemies.检查您是否在玩家和敌人中都使用了对撞机。
  • The player's collider should have the "Is trigger" checkbox selected, so it triggers a OncollitionEnter event.玩家的对撞机应该选中“Is trigger”复选框,因此它会触发 OncollitionEnter 事件。

If it prints the message:如果它打印消息:

  • Check if there is an error message under the printer message, may be that it can't find the scene.检查打印机信息下方是否有错误信息,可能是找不到场景。
  • Be sure that you typed correctly the name/tag of the enemies (I suppose the script is on your player)确保您正确输入了敌人的名称/标签(我想脚本在您的播放器上)

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.

相关问题 玩家碰撞按钮后统一加载下一个场景 - Loading the next scene in unity after the player collides with the button 当我的播放器撞到门时,有没有办法加载场景? - Is there a way of loading up a scene when my player collides with a door? 当玩家与物体碰撞时增加滑块。 统一 - Increase slider when player collides with object. Unity Unity NavMeshAgent 与玩家碰撞时开始向 Go 不同方向 - Unity NavMeshAgent Starts To Go a Different Direction When Collides With Player 当敌人与不同的 object 发生碰撞时,我怎样才能让玩家失去健康? - How can I make it so that the player loses health when an enemy collides with a different object? 我怎样才能做到这一点,当我的玩家与敌人碰撞时,它会禁用移动输入,直到玩家接触地面? - How can I make it so when my player collides with an enemy, it disables movement input until player touches the ground? 更改场景时更改播放器 Position Unity - Change Player Position when a scene was change Unity 播放器与具有特定标签的对象碰撞时的 Unity3D 播放声音 - Unity3D playing sound when Player collides with an object with a specific tag Unity中如何让敌人以动量跟随玩家? - How to make an enemy follow the player with momentum in Unity? 在 Unity3D 中让敌人跟随玩家 - Have an enemy follow the player in Unity3D
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM