Angelsm
Angelsm

Reputation: 339

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. I have this code for the OnTriggerEnter but nothing happens when the player enters in the sphere trigger. 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;
    }
}

Upvotes: 0

Views: 1313

Answers (2)

Ignacio Alorre
Ignacio Alorre

Reputation: 7605

To make it work you will need:

  • In the obstacle: The Sphere collider with the is trigger checked

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. 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"

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).

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

Upvotes: 1

Denvery
Denvery

Reputation: 21

Make sure that your game object for Player also has attached collider

Upvotes: 0

Related Questions