Home > Blockchain >  Unity how to check what object triggered a trigger
Unity how to check what object triggered a trigger

Time:11-17

I am in the middle of creating a simple 3d platformer in unity and i'm trying to make it so if you are in a certain radius and you hit the "f" key the enemy will disappear. the way i'm checking to make sure the enemy is close enough to be attacked is to have trigger sphere and when its triggered and you press "f" the enemy will be removed. i'm currently trying to see what collided with the trigger but i cant figure it out. here is my current code

using UnityEngine;

public class PlayerCombat : MonoBehaviour
{
 public GameObject Enemy1;
 public GameObject Enemy2;
 public GameObject Enemy3;
 public GameObject Enemy4;

   

    // Update is called once per frame
void OnTriggerEnter (Trigger triggerInfo) 
    {
        if (triggerInfo.collider.tag == "Enemy" & Input.GetKey("f"))
        {
            if (triggerInfo.collider.name == Enemy)
            {
                Enemy1.SetActive(false);
            }
        }
    }  
}

CodePudding user response:

First, I'm pretty sure OnTriggerEnter takes a Collider as its parameter, no? https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html

You can get the name of the collided object like so

    //Upon collision with another GameObject, 
    private void OnTriggerEnter(Collider other)
    {
        Debug.Log($"collided with {other.gameObject.name}");
        
        //you can check for specific components too
        MyComponent myComponent = other.GetComponent<MyComponent>();
        if (myComponent != null) {
            // do something if it has that component on it!
        }

    }

CodePudding user response:

The signature is and has always been OnTriggerEnter(Collider) otherwise that message method will not be recognized by Unity and not get called at all!

And then you already have the according Collider ... what else do you need?

public class PlayerCombat : MonoBehaviour
{
    // There is no need to know the enemy references beforehand at all
    // you will get all required references from the Collider parameter of OnTriggerEnter itself

    // I personally would however expose these two settings to the Inspector to be more flexible
    // this way you can adjust these two settings within Unity without having to touch your code
    public string listenToTag = "Enemy";
    public KeyCode listenToKey = KeyCode.F;

    // The signature has to be this otherwise the message method is never invoked at all
    private void OnTriggerEnter (Collider other) 
    {
        // Rather use "CompareTag" instead of `==` since the latter will silently fail
        // for typos and nonexistent tags while "CompareTag" shows an error which is good for your debugging
        // And in general you want to use the logical "&&" instead of the bitwise operator "&" for bools
        if (other.CompareTag(listenToTag) && Input.GetKey(listenToKey))
        {
            // simply set the object you collide with inactive
            other.gameObject.SetActive(false);
        }
    }
}  

Finally just make sure that all your enemy instances actually have the tag Enemy

  • Related