#Triggers detection in Unity

1 messages · Page 1 of 1 (latest)

vocal bough
#

try OnTriggerStay instead of OnTriggerEnter. I had the same problem, and the issue is that OnTriggerEnter only happens "Once". So once you are already inside the second trigger, it won't happen again until you first exit, then re-enter it. OnTriggerStay will fix that problem, as right after interacting with the first object it will detect that something is still within

#

actually, it depends on what you do with the objects? if you don't remove them you might want to use OverlapShere (I'm sure there's a 2D version of it). But anyways that function returns an array of colliders, which you then can cycle through and activate all at once

#

The OnTriggerStay will work though, if it first checks if the object has been interacted with or not.

soft axle
#

Thanks for your support, let me try it

verbal spire
#

This might give an issue where the two colliders constantly overwrite the value of the data type (IInteractive).

soft axle
#

So what do you suggest ?

vocal bough
#

You can avoid that with bools I suppose. "isSelected".. or return if you already have a value

#

if(_interactive_Obj != null) return;//If I already have a value, don't search for more

verbal spire
soft axle
vocal bough
#

By default I think it's 50 times per second

#

I don't think there's a way that don't require to be called every frame, or every physics steps. Except: since you are using the Update() anyways, I suggest only calling the search and interact once when you press "x"

#
Pseudo code:
void Update(){
if(press x){
  SeacrhForObjects();
  if(found object)
      Interact();
}
}
```This way, at least you won't do the whole searching every frame. This requires you to use the Overlap method <<---
soft axle
#

well i think that i'll give it a try. Thanks guys. Really appreciate your assistance.

vocal bough
verbal spire
vocal bough
#

Does that work if you are within 2 colliders though? the way I interpret it is that : You interact with the first object, and if you try to press "x" again, nothing will happen because the _interacttive_object is either still the previous object or it hasn't changed to the new object

verbal spire
vocal bough
#

Yes, but would it work with OP's requirement?

soft axle
# verbal spire

i tried your way and it's working now, i guess i need more specific criteria when exit trigger to make it right. Again thanks for your help.