- Home /
Instantiate a collider already colliding with an object not working
Hi once again.
I'm trying to create some sort of melee attack collision detection. I thought it would be simple and i only had to instantiate a collider, check for some collision and be delete it afterwards. I'm using OnTriggerStay but sometimes the collider is created already touching something but if the object which is colliding with is not moving the trigger never happens...
what i'm doing wrong here? is there any simpler way of achieving what i want?
Answer by thaiscorpion · Nov 26, 2012 at 05:27 PM
Is there a reason you are creating and destroying the trigger? It might be better to maintain it and check if a enemy is inside the trigger when you attack. You could make a simple Bool, isInRange, for each enemy and on trigger enter change to true and on leave change to false, then you only have to check if the enemys isInRange variable and done.
Hope this helped.
the reason i'm destroying it is because i don't really need it after the collision is detected and the attack handled, still thats not the problem, the problem is, even if i don't destroy it the triggers never detects the collision with the enemy if/when the enemy is not moving, the collider borns colliding with the enemy and if this enemy doesn't move the collider never triggers anything
Seems like my answer is just what you want then, dont destroy it so it is already instantiated when you need it just use it to check if an enemy is inside it or not.
uhm, that might be a fine solution for me actually, but i would still like to know how why the trigger is not triggered when this case happens, for now i guess your solution might be ideal
the reason it doesnt trigger is because the detection is only calculated when the object moves. Or if you using on enter and leave if its already inside it wont set of that trigger. Once the object moves it executes the detection code and then realises that it has to trigger the event.
Answer by WillB · May 02, 2013 at 04:34 PM
If anyone else is reading this, thaiscorpion's last comment is correct.
I was instantiating a large BoxCollider (that isTrigger) in front of my player that calls functions on all colliders inside of it, then destroys itself after WaitForSeconds(0.1). It wasn't working, until I added a kinematic rigidbody (lol?) to the instantiated BoxCollider.
Took 3 hours to figure that out; hope I saved you some time. :)
Your answer
Follow this Question
Related Questions
Using shadows as Triggers? 1 Answer
How to restart scene with trigger? 1 Answer
Collider won't collide with a trigger 3 Answers
How to design an Archer Board Scoring System 1 Answer
Fire Collision detection 1 Answer