- Home /
Getting sprites to stop going through one another?
I'm trying to get the enemies in my 2D game to stop going through each other. Their "Is Trigger" is checked in order for them to harm the player. I've tried unchecking the box, which does cause the enemies to run into each other and not through each other, but it also does the same to the player and prevents the player's script for taking damage to work. Is there a way to make the enemies not pass through each other yet still be "Is Trigger", maybe in the C# script on the enemy? Thanks.
Answer by meat5000 · Apr 15, 2016 at 05:00 PM
There's a sudden obsession with everyone using Triggers as Physics Colliders. This is nonsense practice. Triggers are area-switches nothing more.
Marking isTrigger takes away the physics barrier of the collider. This is why it can be used as an area trigger. For collisions, remove isTrigger and use OnCollision routines instead of OnTrigger.
Its all in the manual.
Could everyone just stop trying to use Triggers for Physics Collisions please?
I mean literally, the question under this one in the moderation queue was the same damn thing. Seems that someone has released a tutorial which is spreading bad info.
I'm sorry, I'm in a high school game design class trying to make my end of the year project. I haven't been using Unity for long.
I have tried unchecking "Is Trigger" and using OnCollision ins$$anonymous$$d, however when I try this no collision is detected. The player still takes no damage from hitting the enemy.
You are only following half the advice.
http://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html
I simplified 'OnCollision' but didnt consider that you wouldnt follow. Colliders come with three routines which work when isTrigger is checked.
OnTriggerEnter
OnTriggerStay
OnTriggerExit
They come with three routines which work when isTrigger is NOT checked
OnCollisionEnter
OnCollisionStay
OnCollisionExit
Its all easily searchable in the docs and has examples in the Scripting Reference.
I DID follow your advice, mainly because I tried all of those before even posting this question. The thing is that no matter what I put in, OnCollisionEnter, OnCollisionStay, or OnCollision Exit, no collision is ever detected between the enemy and the player when the enemy's "IsTrigger" is unchecked.
Any collision requires 2 colliders and 1 non-kinemtic rigidbody.
Given that you're talking about a 2d game, you should probably be using Rigidbody2Ds, Collider2Ds, and OnCollisionEnter2D()...
I've BEEN using two colliders and non kinematic rigidbodies. I've ALSO tried every kind of collision code I can think of. OnCollisionEnter2D, OnTriggerEnter2D, etc. I've unchecked "IsTrigger" on the enemies and it doesn't work. I put in code for unity to output the words "Player Hit" when collision with the enemy occurs, and so far it has yet to give the message. Thus I'm assu$$anonymous$$g that the player and enemy are hitting one another, but not passing into one another even by one pixel. So the OnCollisionEnter2D doesn't work since their hit boxes never cross. I need a way to get the player to detect collision with the enemies in this way.
If the callback is not fired the collision has not occurred.
One non-trigger collider on each object and one non-kinematic rigidbody is required on either object. The collisions will occur even without using any OnCollision functions at all. Realise this routine is a bonus and is not a requirement.
If this basic setup is not working there are several things that could cause this.
1) $$anonymous$$oving objects with transform.Translate or modifying .position (causes teleportation).
2) $$anonymous$$oving objects too fast for the collision to register (yes this even happens on Cont. Dynamic)
3) Something is awry in the Collision $$anonymous$$atrix (Physics $$anonymous$$anager)
As tanoshimi says, $$anonymous$$ake sure the colliders and rigidbodys are the 2D variants!
Please post your collision code and a screenshot of your inspector settings showing the components attached to your objects. Have you got the function signature for OnCollisionEnter2D correct?
Here is the player's collision code, which it so far only uses when it's hit by the enemies.
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Enemy" && !invulnerable)
{
print("HIT");
invulnerable = true;
HP--;
Invoke("NotInvulnerable", invulnerableTime);
}
if(other.gameObject.tag == "BossShot" && !invulnerable)
{
invulnerable = true;
HP -= 2;
Invoke("NotInvulnerable", invulnerableTime);
}
}
Here is the code the enemies use for colliding with the player's laser shots, which works just fine no matter what.
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Projectile")
{
HP--;
}
}
This is a screenshot of the player's rigidbody and box collider.
And here is a screenshot of the enemy's rigidbody and box collider.