Trigger Collider question

I’m using MoveToward to move my Player along a series of gameObjects stored in an array and laid out along a ‘path’ (not like in pathfinding).

All the gameObjects are at the same Y height and some of them have Trigger Colliders on them. The Player travels along from the transform.position to the target.position at the same Y height as the Trigger Colliders.

If the Player has to hypothetically pass 4 gameObjects to get to the target gameObject, how can I stop the preceding 3 gameObject Trigger Colliders from triggering an event? I only want to trigger the event of the target gameObject, if there is one. I know that using MoveToward it might not actually pass through all of them to get to the target gameObject, but I have to assume it might.

Thank you for any help

You can just do something like this…

 void OnTriggerEnter(Collider col)
{
    if (col.gameObject.name != theNameWeWant) return;
    else DoThings();
}

Lane, thanks for the help, and I can see that code helping me out somewhere else, but looking at how I think that’s supposed to work, makes me have to explain a bit more to clearly understand my problem.

The gameObject the Player can land on is randomly chosen by a dice roll. I won’t be looking for a predetermined Collider name. All the gameObjects in the array are of the same name, just some have Colliders that trigger effects based on the Tag of that specific gameObject, if it even has a Tag.

Could I check to see if the gameObject matches the index number of the array element before triggering the Collider? I feel the Trigger Colliders will trigger the event if the gameObject collides with it, regardless if it’s the 1st or last one, or all of them along the way.

I don’t really see why you need to filter anything if you already know what is going to happen and are expecting it. Is this a timing thing? If you’re moving the character to x position, can’t you just yield until the move is complete?

You can of course use the index if it is known. If you’re absolutely moving the player to a position then you should be able to identify that trigger with that position, and return if your expected trigger collider is not it.

I’m very new at all of this. If I can yield until I get to the target gameObject, that would be great. I’ll have to research that.

I am going to have 2 dice randomly choose numbers, total them up, and have the player move to the gameObject whose element number matches the total of the dice (+2 to make up for element 0). So a roll of 6 would take me to the 6th gameObject in the array. Then it would check for a Trigger Collider and Tag.

I was just worried I would trigger events along the way to getting to the target gameObject as it passed through the first 5 in the above example.

If you’re using Triggers then it will, which is why you would use a return if the some information that you obtain from the Collider passed by the OnTriggerEnter(Collider col) method didn’t match the expected values. That information might be a check to see if that collider is the same collider in your Triggers array at that rolled index, or whatever.

1 Like

Ahhh, I see where you’re going with that. I’ll have to work on that. Thanks! :slight_smile:

1 Like