简体   繁体   English

Unity 3D 游戏对象上的碰撞不起作用

[英]Unity 3D Collision on gameobject not working

I've made a code where once you collide on a gameobject, it will go to a different scene, the gameobject is a bed, I've applied a tag named Bed, I've applied a rigidbody, I've applied the code to the player where once you collide the code will activate, but for some appearent reason, it still won't work,我制作了一个代码,一旦你在游戏对象上碰撞,它就会 go 到不同的场景,游戏对象是一张床,我应用了一个名为 Bed 的标签,我应用了一个刚体,我应用了代码到玩家,一旦你碰撞代码就会激活,但由于某种原因,它仍然无法工作,

here is the code I've applied to the player:这是我应用于播放器的代码:

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.SceneManagement;

public class PlayerCollision : MonoBehaviour
{
    
  void OnCollisionEnter (Collision Collisionbad)
       {
        
          if (Collisionbad.collider.tag == "Bed")
           { 
                  Debug.Log("Roll Credits");
                  SceneManager.LoadScene("EndingScreen");
           }
        }
}

Your issue is most likely that you are checking the tag of a collider as opposed to a gameObject您的问题很可能是您正在检查collider的标签而不是gameObject

if(Collisionbad.gameObject.tag == "Bed"))

Also, consider using CompareTag as it is more efficient此外,请考虑使用CompareTag ,因为它更有效

if(Collisionbad.gameObject.CompareTag("Bed"))

Add BoxColliders to both objects which collides Add this script to the object which collides with Bed In settings check layers for object 1 and 2 thath they cann collide And your script must work将 BoxCollider 添加到两个发生碰撞的对象 将此脚本添加到与床发生碰撞的 object 在设置中检查 object 1 和 2 的层,它们不能发生碰撞并且您的脚本必须有效

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM