- Home /
How do I use an enemy's OnTriggerEnter to collide with stationary player?
I have a player ship and many enemy ships (prefab). I want the ship to collide with enemies (ship takes damage, enemies are destroyed), but I don't want enemies to collide with enemies (don't want them to destroy each other).
Here's how I have tried to do this. Ship and enemy prefab have Colliders, ship has a Rigidbody. Ship is the Trigger. I use OnTriggerEnter in the ship's script.
Problem: if the ship is stationary, enemies pass right through it.
How do I get enemies to ignore each other, but collide with the the stationary ship? If I need to use Triggers then I will. If I shouldn't use Triggers, please tell me that also.
I really have no idea what you are trying to do, I dont know if its 3D or 2D etc, but either way if you want the ships to ignore eachother. Put a raycast infront of them and use a type of path finding, then transform them to 'player ship' and also use a OnCollisionEnter.
http://docs.unity3d.com/Documentation/ScriptReference/Collider.OnCollisionEnter.html
I hope this helps, other then that I am lost on what you are trying to get at.
Answer by aldonaletto · Jan 24, 2013 at 04:16 AM
Simply add kinematic rigidbodies to the ship and the enemies: trigger events will be generated no matter if the ship is static or moving.
@aldonaletto: Thank you for your answer. I made the ship kinematic and now the enemies collide when the ship is stationary. After your suggestion, I read about non-kinematic collisions and found out they "fall asleep" when stationary. That's why the enemies pass through my sitting ship.
I suspect that the reason is different: apparently, the collision system only tracks rigidbodies (kinematic or not), thus it can detect when a rigidbody hits a collider, but not the other way around. As a rule of thumb, any moving collider should have a kinematic rigidbody for the collision system to work. Non-kinematic rigidbodies "go to bed" when not moving in order to reduce the CPU load, but they wake up when another rigidbody collides with them.
@aldonaletto: I didn't think it would work to put a Rigidbody on the enemy prefab too, but it did. It's working exactly how I want it to. Now I just need to understand collisions and triggers better. Obrigado, Aldo!
I want the exa$$anonymous$$er in an area to perceive that there are no enemies around, but when he detects the player, he immediately opens the door, even if there is an enemy inside, how can I fix it?
Answer by cdrandin · Jan 24, 2013 at 03:48 AM
This should help you http://docs.unity3d.com/Documentation/ScriptReference/Physics.IgnoreCollision.html
@cdrandin: Thank you, that looks very useful. I need to go try it out.
Your answer
Follow this Question
Related Questions
Character controller collider? 0 Answers
OnTriggerEnter2D only one time 1 Answer
how to make a bullet that'll be like a missile. 2 Answers
OnTriggerEnter with exact spot of collision 2 Answers
onCollisionEnter vs onTriggerEnter 1 Answer