- Home /
Trigger or Colission not work if inside volume
Hello, there!
I found that OnTriggerEnter/Stay/Exit and OnColissionEnter/Stay/Exit not work when object1 instantiated inside collider of object2. When its colliders not intersect on spawn moment all works great. Easy to understand why :)
The only way to check if one object still near to spawned point is Vector3.SqrMagnitude(pos1,pos2). Not bad, but this need Update() :(
Any other way to solve this problem with triggers?
For now I create several colliders on spawn point, when new object appear and start move it will intersect with one of those volumes. But ... if object has great size, it intersects with all colliders at once and this approach not work universally :)
Answer by TonyLi · Aug 01, 2013 at 02:52 PM
Use OnTriggerStay or OnCollisionStay. This is called every fixed update that the colliders are in contact. At least one needs a [non-kinematic - or kinematic] rigidbody. OnTriggerStay is less efficient than OnTriggerEnter, of course, because it runs every fixed update instead of just once.
Thank you for answer, you right in theory, but on practice things little complicated. System will work ONLY if object, which emitted in trigger volume, has Rigidbody.You cannot add trigger AND rigidbody to emitter, and wait while emitted object with collider produce any callback, it will not. This situation very not optimal, it demand to produce crowd objects with rigidbody. Am I clear?
I've done this in practice and it works fine. $$anonymous$$y triggers were static colliders. The spawned object was mobile and had a kinematic rigidbody. Sorry, I mis-wrote when I said "non-kinematic rigidbody" earlier. Give it a try.
" spawned object was mobile and had a kinematic rigidbody" - yes, this is the only way. You cannot add rigid to static collider, you need to spawn many objects with rigidbody, which in my situation not needed for any other reason in scene. This is the thing which I said as not optimal earlier.
So neither object ever moves? If either object moves, it shouldn't be a static collider (or it should have a rigidbody).
Emitter is static and emitted objects is navmesh agents which immediately start move to its targets. For now I solve problem with simple check the distance and start spawn next object only when distance is greater than some value.
Answer by Jamora · Aug 02, 2013 at 08:32 PM
You could possibly use the Bounds of your colliders. Whenever you need to check if it's inside your triggers, you just use collider.bounds.Intersects(otherBound)
You will only need two objects with colliders. No rigidbodies are needed if that is a problem for any reason.
Thank you, Jamora, this approach also require Update(), but looks more accurate than my distance checking! I'll keep your idea in memory and try it when I will need more accurate results )
Your answer
Follow this Question
Related Questions
Trigger Spawning? 1 Answer
Do triggers not fire if spawned colliding? 1 Answer
onCollisionEnter2d spawn - unity 4.6 0 Answers