playMaker

Author Topic: Is there a way to know if a trigger is NOT touching anything?  (Read 4285 times)

PolyMad

  • Hero Member
  • *****
  • Posts: 545
Ye I know this will sound strange but I find myself probably in a particular condition.

The three trigger options ENTER, STAY and EXIT don't seem to cut what I need to do, because I need exactly the opposite: I need to know when the trigger is NOT touching anything.
Using EXIT is out of question because when the character leaves the previous block it calls an EXIT.
So I tried to use STAY, as the character is walking on different blocks, when it leaves the previous block, no matter the trigger is already on another block, the STAY function will be turned off when it leaves the old block.

I really can't understand how I can catch when the trigger is out of every collider without a function that will report me this value.

PolyMad

  • Hero Member
  • *****
  • Posts: 545
Re: Is there a way to know if a trigger is NOT touching anything?
« Reply #1 on: May 25, 2016, 11:17:14 AM »
I solved it, but was quite heavy and annoying. I had to introduce a variable to let the routine know how many blocks the trigger is laying on. It's 1 or 2, but I had to add it anyway. If the trigger lays on 0 blocks then I activate the reverse function.

All of this could be avoided with a simple value "null" in case the trigger is not touching anything. I think this is missing, don't know if in PlayMaker or Unity itself.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Is there a way to know if a trigger is NOT touching anything?
« Reply #2 on: May 26, 2016, 02:55:11 AM »
Hi,

 It's a Unity thing. The only ways I know are :

- Have a dedicated fsm listening to  ENTER and EXIT, and have a counter, if the counter is 0, then nothing is inside the trigger, else there is

- Have the same dedicated Fsm but keeping track of all insiders using an array ( 1.8 or with arrayMaker)

- use a script that does the same thing basically :) https://alastaira.wordpress.com/2013/12/12/keeping-track-of-targets-in-a-trigger/

Bye,

 Jean

PolyMad

  • Hero Member
  • *****
  • Posts: 545
Re: Is there a way to know if a trigger is NOT touching anything?
« Reply #3 on: May 26, 2016, 03:10:23 AM »
Quote
- Have a dedicated fsm listening to  ENTER and EXIT, and have a counter, if the counter is 0, then nothing is inside the trigger

That's EXACTLY how I did it!
Glad I managed to understand industry standard in half an hour! :D

However, this is a really stupid lack of a function in my opinion.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Is there a way to know if a trigger is NOT touching anything?
« Reply #4 on: May 26, 2016, 03:18:06 AM »
Hi,

 I agree. but I also think I know why they do not implement it, neither the physics engine nor unity provides this: because it would be an overhead.

instead of providing a feature takes memory and scripting, which means less performances all around, they leave this up to the developer IF needed, and WHEN needed.

Imagine huge amounts of triggers, all having to keep a boolean flag, execute codes, for nothing... I think it make sense to spend that little extra time fo deve to do this only IF and WHEN needed.

 Does that make sense? on top of that, this could be a good template to provide on the Ecosystem for when needed, drop this on a GameObject and you'll have an fsm you can query using for example custom actions from the ecosystem like FsmBoolCheck

https://600.snipt.net/fsmbooltest/

Bye,

 Jean

PolyMad

  • Hero Member
  • *****
  • Posts: 545
Re: Is there a way to know if a trigger is NOT touching anything?
« Reply #5 on: May 26, 2016, 04:37:55 AM »
Mhhh yes I understand what you mean.

But here's the hack I think, I'll test it later: when you spawn the trigger, if it doesn't collide with anything, the collision string will remain empty!
So I will try to replace the collision count method with a deactivation/reactivation of the trigger object every 0.1 seconds or so.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Is there a way to know if a trigger is NOT touching anything?
« Reply #6 on: May 26, 2016, 07:23:28 AM »
Hi,

 You are right, spawned trigger do no scan for their environment for "already" colliding objects. You need to spawn colliders in a clean space.

Bye,

 Jean

PolyMad

  • Hero Member
  • *****
  • Posts: 545
Re: Is there a way to know if a trigger is NOT touching anything?
« Reply #7 on: May 26, 2016, 07:53:41 AM »
Uh? I think you are wrong here: when you spawn a collider it reports the objects on which it is colliding, or at least my enemy is working like that: it spawns with the trigger already colliding with a block, and it's reported. So that's why spawning it in the open should work: because it would not report any collider!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Is there a way to know if a trigger is NOT touching anything?
« Reply #8 on: June 07, 2016, 03:55:02 AM »
Hi,

I tested it and indeed you don't get a collision.

Repro case:

1: have a cube with a rigidbody, not moving, listening on OnCOLLISIONENTER
2: instantiate a prefab that interesects the cube with its collider, you won't get called ONCOLLISIONENTER

and that is because nothing is moving. If one of your object moves it's ok you'll get a collision enter.

so watch out for this, it can become tricky to debug, depending on your situation.

 Bye,

 Jean

PolyMad

  • Hero Member
  • *****
  • Posts: 545
Re: Is there a way to know if a trigger is NOT touching anything?
« Reply #9 on: June 07, 2016, 05:26:29 AM »
Thank you Jean, very kind from you :)

SamH

  • Playmaker Newbie
  • *
  • Posts: 41
Re: Is there a way to know if a trigger is NOT touching anything?
« Reply #10 on: January 13, 2020, 03:09:17 PM »
If I can just add an extra note to this thread- I completely understand that a trigger that is searching every frame for an object not colliding with it would be very expensive. But it would be incredible if there was a send event option added to 'Trigger Event' if the 'on stay' was false. In this way after a single frame it would detect the 'no collision' and immediately send an event. If there is a collision it would remain 'on stay' with current functionality. This way you could have a trigger which on state enter, immediately checks if an object is or isn't currently colliding. I would have so much use for this!

There are definitely work arounds, like described above. I also replicate this behavior by adding a wait action after the Trigger Event, which waits 0.1 seconds and then fires an event. So if the 'on trigger stay' doesn't fire, the wait does instead. But it would be great to have this as a neat option in the existing 'on trigger stay' action. Hopefully suggestion is useful to other people! Thanks
« Last Edit: January 13, 2020, 06:12:02 PM by SamH »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Is there a way to know if a trigger is NOT touching anything?
« Reply #11 on: January 14, 2020, 01:07:31 AM »
Hi,

yep, it's not implemented very much because it is very performance hungry just to have this possibly that's probably unused for most of the usage of this action.

 The way to do this is quite simple with an fsm template.

 Watch all Enter and Exit collisions and triggers, and keep a counter, add one on enter, substract 1 on exit and then raise a bool to true when this counter is not 0

then you can reuse this template very easily and even probably fire a global event to the local branch ( send global event to self including children), when the bool value switches states, so other fsm simply catch these global events.

 
Bye,

 Jean