[Solved] OnTriggerEnter Colliders

Hi there,

I’m pretty new to Unity and C#, and started to follow a video tutorial series about making an arcade style vertical shooter. I have some experience with PHP, but I never programmed some kind of software, so maybe somebody could give me a hint how to accomplish my task:

I want to script a homing missile. The missile should fly straight upwards, until an enemy enters its first collider (a sphere) and the missile starts to chase that enemy until the second collider is entered (a box with the size of the missile).

I started with onTriggerEnter, but I won’t find the commands I need. Basically I want to get the collider that has evoked the onTriggerEnter hook, so I can do some if checks.

Thank you for your answers,

Thomas

function OnTriggerEnter(other : Collider) {
  //your code here
}

“other” is the collider you hit. as an example, the collider’s game object would be accessed by other.gameObject

So if I have two colliders (homingCollider and missileCollider) attached to my missile prefab, as well as my missile script, I just can do stuff like:

    void OnTriggerEnter(Collider hitCollider)
    {
        
     if (hitCollider == homingCollider) 
        {
             //Do something
        }

     if (hitCollider == missileCollider) 
        {
             //Do some other stuff
        }

    }

missileCollider and homingCollider are public variables, that I filled with the inspector with the proper collider components.

Thanks again for your quick help =).

i’d rather compare the tags of the colliders, but yes, that’s roughly how it would work

Doesn’t seem to work unfortunately. I have:

Missile Prefab

  • missileCollider (box collider with same size as missile)
  • homingCollider (bigger sphere collider)

Asteroid Prefab

  • standard sphere collider

When I use the following lines in the script that is attached to the missile prefab, I just get the names of the asteroids collider in my log - but instead I would like to know which of the missiles colliders were involved in the collision.

    void OnTriggerEnter(Collider hitCollider)
    {
        Debug.Log(hitCollider.name);
    }

How can I achieve that?

Edit: About the tags - As far as I know, tags are attached to a gameObject, not onto a component. So they won’t help me to find out if one or the other collider has been hit. Am I right?

hitCollider.gameObject.tag should work

Any other suggestions? Maybe I missunderstand something about the tag solution, but to me it seems not to work that way. Again, I have the following two game objects (but the second one is more or less irrelevant for that problem):

Missile Prefab
2 attached colliders

  • missileCollider (checks if the missile has hit an asteroid)
  • homingCollider (checks if an asteroid is in lockon range)

Asteroid Prefab

  • standard sphere collider

Inside the script of the missile, first I want to check if something is in lockon range of it. If true, the homingCollider is no longer important, I want to check for collisions with the missile collider. Unfortunately I don’t know how to find out if the collision has hit the homing or the missileCollider.

Any idea?

Thanks, Thomas

i can post the code i’m using in my project, but it’s written in javascript. it works perfectly though

function OnTriggerExit(other : Collider) {
	if (other.CompareTag("floor"))
	{
		//custom stuff
	}
}

Sorry, there seems to be an error in communication :smile:. Tags won’t help me, because every game object can only have one single tag. But I have one game object with 2 colliders applied, so I don’t see how tags could be any help? I want to check which collider of the object has triggered the OnTriggerEnter event :? but inside the script that is applied to the object, not in the script that is applied to the other object that is involved in the collision.

ok you wrote it 2 times but i didn’t read carefully, sorry about that.
i’d simply add an empty game object to the missile as a child and attach the homingCollider to that object instead of the missile. afaik, 2 colliders on the same object are treated as a compound collider instead of 2 different colliders, but i don’t have unity installed on this system to test stuff.

No need to feel sorry, your patience is greatly appreciated :). In the meantime I came up with the same approach you suggested, and it works wonderfull - nevertheless, thanks for your time and thoughts!