- Home /
Collision with instantiated non-moving objects.
Alright. Hope it's not a tough one. In my game (2D Platformer), there are melee weapons and ranged weapons. Ranged projectiles collisions and triggers work fine. But I can only get non-trigger melee instances to register a hit with the target instance, if the target is moving into it. In other words, if I spawn the melee hitbox on the npc while it is not doing an attack animation, a trigger enter, exit, nor stay will be called until the game object starts moving. additionally, I have tried to animate the projectile to slightly move passively to solve this, but to no avail.
I feel like a previous version of unity did not do this, because i remember having simple target objects instead of AI that i could simply hit with both melee and ranged attacks and they would show a destructive animation. This was all previously to test simple collisions. I loaded up an old backup where this was possible, and now it no longer works with the current version of unity (5.2.3).
Another thing is, I could ofcourse just turn this trigger into a non-trigger collider, and that makes things simpler for the script to recognize because the colliders just push the npc around, forcing movement for the calls. But I want a trigger. I want to code in specific instructions on hit. Like what if I don't want this knockback effect? I can't do that if it throws the enemy due to physics updates from a non-trigger collider.
Any help?
Answer by wibble82 · Dec 03, 2015 at 10:03 AM
Hi Jade. I've hit this same problem before (both on 4 and 5), and it occurs because the trigger system operates by detecting when objects start/stop penetrating. When you spawn an object penetrating it doesn't seem to trigger, presumably because the rigid body never 'entered' the trigger.
However I believe I fixed it by calling 'RigidBody.MovePosition' on the spawned object and passing it its current position, as this makes the physics engine wake up and see a rigid body that might be doing something. Give that a try anyway - it worked for me a while back.
I would accept this answer for its simplistic solution. The problem however, is that collision detection can occur regardless of rigidbodys existing on a gameobject. So I would not say that they are called exclusively related to the rigidbody.
EDIT: On further testing, it is apparently called multiple times, including the calls for rigidbody component collisioned objects as well. The simple solution is to add a rigidbody and set the gravity scale to zero, and continue to set the position through abstract methods if need be, rather than by force values. If you could include some information in the answer I will accept. Thank you =D
Adding a rigidbody to the instantiated object worked for me.