playMaker

Author Topic: Triggers getting triggered with wrong Tag [SOLVED]  (Read 998 times)

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 254
Triggers getting triggered with wrong Tag [SOLVED]
« on: November 30, 2022, 10:33:24 AM »
I'm using triggers a lot. But now suddenly I'm confused on how they actually work.

I'm in the understanding that when a rigidbody with collider enters another collider set to trigger, the onTriggerEnter method is called. This way I pick up my coins in my game using the TRIGGER EVENT action. The Player (with rigidbody and collider) hits the coin (with trigger collider).

I'm currently adding a magnet zone to my player to attract coins, this collider obviously can't be just a collider since it would disturb the player's movement, so I set it up as well as a trigger. Hence I now have two triggers interacting with each other, the magnet field and the coin.

According to the Collision action matrix from https://docs.unity3d.com/Manual/CollidersOverview.html this should be working, but the problem is that now that the new trigger collider (the Magnet field) fires as well other onTriggerEnter events on other trigger colliders I have setup for example for ending the level or spawning particles etc.

Why is this happening? My Magnet has it's own tag "PlayerMagnet" while for example the level end trigger has a tag "LevelEnd". Nonetheless, the action TRIGGER EVENT is triggering the event no matter the specified tag.
« Last Edit: December 09, 2022, 11:25:23 AM by Christoph »

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 254
Re: Triggers getting triggered with wrong Tag
« Reply #1 on: November 30, 2022, 01:19:59 PM »
So I was able to resolve this with a new layer that only collides with collectibles. I think I'm confused on which object triggers which trigger. Still not confident to be honest, but at least it's working now.  8)

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Triggers getting triggered with wrong Tag [SOLVED]
« Reply #2 on: December 03, 2022, 01:16:23 AM »
Hi.
Just a question.
Do you have fsm(s) on your coins that have trigger events or do you have it on the player.

I am asking because, if you are handling trigger events on the coins it's bad for performance.

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 254
Re: Triggers getting triggered with wrong Tag [SOLVED]
« Reply #3 on: December 03, 2022, 02:06:25 PM »
I stopped to use FSMs on objects that I use a lot. Like Coins. Use only C# scripts on those. Like for rotating the coin I used a FSM. So I had 30-50 coins in a level all running a FSM. Plus even worse, I place them in level prefabs so I had 50 levels, all with 30-50 coins, duplicating effectively the FSM 2000 times.

So yes, I wanted to avoid having trigger events on the coins. But I'm using those now in C# scripts which I have attached to the coins. Same thing?

Because I haven't seen that it would be affecting performance badly. But I haven't tested on a cheap Android device either...

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Triggers getting triggered with wrong Tag [SOLVED]
« Reply #4 on: December 04, 2022, 12:05:23 AM »
hi.
if you have 100 object listening vs 1 it will make a difference but it does not mean that you will notice.

in our game, before i had trigger events on the shards that fall out off our enemies and could sometimes reach 300+ in a scene where we started to see some performance issues.

When I changed it to the player (which actually uses Overlap action now instead of trigger event)
now the shard do not have any script, only tags Shard/1 shard/10 etc for its value
and have a 'interact' layer
Which erased the performance issue we had

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 254
Re: Triggers getting triggered with wrong Tag [SOLVED]
« Reply #5 on: December 04, 2022, 01:46:52 PM »
hmmm that's interesting. So currently I have a coin pickup script with two onTriggerEnter listeners. One is for the coin to be picked up, the other is for getting into the magnet zone. Both of this colliders(triggers) are on the player. In order to pick the coin up, I see how I can do that directly on the player, respectively on a player manager. But how would I move around the coins attracting each to the player's magnet? That logic I guess has to be on the coin itself, right?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Triggers getting triggered with wrong Tag [SOLVED]
« Reply #6 on: December 05, 2022, 09:49:04 PM »
Hi.
The moving towards player is probably best to have on coin, but to trigger it you should still do it on player (send an event or set a bool for example)

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 254
Re: Triggers getting triggered with wrong Tag [SOLVED]
« Reply #7 on: December 05, 2022, 11:22:08 PM »
ok cool and yes, makes totally sense. Appreciate it djaydino will fix that asap.

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 254
Re: Triggers getting triggered with wrong Tag
« Reply #8 on: December 08, 2022, 12:09:13 AM »
So I did change the logic and moved all trigger events into the player and I'm back to square one with the triggers messing with each other. I just can't make it work.

My player has now two colliders:
1. the player itself
2. the magnet field (set as a trigger)

I have two FSM with a Trigger Event listener each:
1. For the Coin Pickup
2. For the Magnet Zone

Whenever the Magnet Zone (which is way bigger than the player obviously) touches a coin, the coin gets picked up immediately too.

How can I separate the two?


djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Triggers getting triggered with wrong Tag
« Reply #9 on: December 08, 2022, 02:02:30 AM »
Hi.
Yeah you might need to play around with the layermask as the rigidbody tend to combine the children colliders

But for the 'Magnet Zone I would use a overlap box/circle or box/sphere cast Ecosystem
and let it check like every 4 frames or so

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 254
Re: Triggers getting triggered with wrong Tag
« Reply #10 on: December 08, 2022, 08:20:30 AM »
This is so confusing. So each collider/trigger basically needs its own layer?

And why you recommend the overlay cast instead of the collider and trigger on enter?

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 7616
    • jinxtergames
Re: Triggers getting triggered with wrong Tag
« Reply #11 on: December 08, 2022, 09:50:06 AM »
Hi.
collider children of a rigidbody with the same layer seem to trigger the event yes.
so not all collider/trigger need its own layer

overlap is "cheaper" also it does not need a collider and you do not need to do this every frame.

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 254
Re: Triggers getting triggered with wrong Tag
« Reply #12 on: December 08, 2022, 02:41:23 PM »
I will try the overlap later, but I need to understand first the colliders. Haha still can't figure it out.

So I have the player collider in a layer mask called Player.
And I have the player magnet collider in a layer mask called PlayerHelpers.
The coin collider has a layer mask called Collectibles.

All good till here.

Now Player interacts with Coin (it has to pick it up) and PlayerHelpers have to interact with Coin too (it has to activate the Magnet inside the coin). So even with two completely different layer mask, I still have the exact same problem...  ???

Christoph

  • Beta Group
  • Sr. Member
  • *
  • Posts: 254
Re: Triggers getting triggered with wrong Tag
« Reply #13 on: December 09, 2022, 11:25:04 AM »
I think I got it solved with the different layers again... no idea how though, but it's working again. And now I have the trigger on the player only. Nice. Thanks a lot again.