简体   繁体   English

Unity-两种不同类型的对撞机之间的检测不起作用

[英]Unity - Detection between two different types of colliders is not working

I have two different type colliders in a gameobject : a sphere collider for a trigger and a capsule collider for a hit raycast. 我在游戏对象中有两种不同类型的对撞机:用于触发器的球形对撞机和用于命中gameobject的胶囊对撞机。 I have this code for the OnTriggerEnter but nothing happens when the player enters in the sphere trigger. 我有用于OnTriggerEnter代码,但是当玩家进入球形触发器时什么也没有发生。 What am I doing wrong? 我究竟做错了什么?

public GameObject canvasTesoroFoto;
public GameObject canvasTesoroDatos;

private GameObject Player;
public SphereCollider sCollider;
public CapsuleCollider cCollider;

void Start()
{
    Player = GameObject.FindGameObjectWithTag("Player");
    sCollider = GetComponent<SphereCollider> ();
    cCollider = GetComponent<CapsuleCollider> ();

}

void OnTriggerEnter(Collider other)
{
    if (other == sCollider) {
        if (other.gameObject.tag == "Player") {
            canvasTesoroFoto.SetActive (true);
            canvasTesoroDatos.SetActive (true);
        }
    } 
    if (other == cCollider) {
        Debug.Log ("HitCollider");
        return;
    }
}

To make it work you will need: 要使其工作,您将需要:

  • In the obstacle: The Sphere collider with the is trigger checked 障碍中:带有的Sphere碰撞器已被触发检查

In the player: 在播放器中:

  • A collider 对撞机
  • A rigidbody 刚体

Besides, if the scrip is in the obstacle, the collider other will refer to the player NOT to the OBSTACLE. 此外,如果在障碍物上打滑,则对撞other将指玩家而不是障碍物。 So you should do directly like this: 因此,您应该直接这样做:

void OnTriggerEnter(Collider other)
{
        if (other.gameObject.tag == "Player") {
            canvasTesoroFoto.SetActive (true);
            canvasTesoroDatos.SetActive (true);
        }
}

Based on your comment. 根据您的评论。 In that case you should consider add the trigger in the collider of the player, to detect the sphere of the obstacle, and leave the trigger for the capsule. 在这种情况下,您应该考虑将触发器添加到玩家的对撞机中,以检测障碍物的球体,并将触发器留给胶囊。

And in case you need that both colliders in the obstacle are triggered, what you can do is to create a sphere gameobject as child of the obstacle, make it transparent and add there the script and sphere collider. 并且如果您需要触发障碍物中的两个碰撞器,则可以做的是创建一个球形游戏对象作为障碍的子代,使其透明并在其中添加脚本和球形碰撞器。 Then keep the collider in the obstacle itself. 然后将对撞机放在障碍物本身中。 Then you can exchange information between script in the sphere and the obstacle if necessary. 然后,您可以根据需要在球体中的脚本和障碍物之间交换信息。

But as far as I know, there is not way to distinguish which of the colliders in the gameobject triggered OnTriggerEnter() if both has got checked "is trigger" 但是据我所知,如果两个对象都已选中“触发”,则无法区分游戏对象中触发了OnTriggerEnter()哪些对撞机

Edit: based on your comments, I imagine a situation where the GameObejct of the colliders is some kind of enemy which may be hit by raycast and at the same time can crash with the Player (for example to take life from him). 编辑:根据您的评论,我想像这样一种情况,对撞机的GameObejct是某种敌人,可能被射线投射击中,并且同时可能与玩家崩溃(例如夺走玩家的生命)。

The best approach is to make is trigger the collider of the Player, to detect the sphere collider of the enemy in a script attached to the Player. 最好的方法是触发播放器的对撞机,以检测附着在播放器上的脚本中的敌人的球形对撞机。 Then uncheck is trigger the sphere collider in the enemy. 然后取消勾选将触发敌人中的球形碰撞器。 And just leave is trigger the capsule collider to detect raycast. 而离开则触发胶囊对撞机检测射线。

In this tutorial you have a nice example of this: 在本教程中,您有一个很好的例子:

https://unity3d.com/learn/tutorials/s/survival-shooter-tutorial https://unity3d.com/learn/tutorials/s/survival-shooter-tutorial

确保您的Player游戏对象也附加了对撞机

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

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