Ignore collision in specific circumstances while still using it as a trigger - unity3d

I am making a 2D platformer type of game. In this platformer there are both the player and "enemies". The problem arises because: I need both enemies and players to collide with the ground so they don't fall through it. I need to detect when a player collides with an enemy so i can register damage. I need the player to be able to walk through an enemy. Having colliders on the feet is not an option because the player may interact with it. What is the best way to approach this kind of specific collision detection?

You can use the Layer Collision Matrix (Edit -> ProjectSettings -> Physics2D) to define exactly which layers can collide with which other layers.
So I would simply use two colliders on the player:
one is not a trigger so it can collide with the ground. Put this on a layer playerGround which collides with the ground layer.
the other one is a trigger and can collide with the enemy layer. Name it e.g. playerEnemy or something.
Since each GameObject can only have one layer your colliders would have to sit on different objects e.g. like
player (Rigidbody2D)
|--GroundCollider(layer: playerGround)
|--EnemyCollider(layer:playerEnemy, isTrigger)
The enemy layer can collide with both the ground and the playerEnemy.
This way both can walk on the ground. Player can walk through enemies but you can use OnTriggerEnter to detect the collisions with enemies.

Related

How to seperate the collision detection in unity?

I'm new to unity and I'm making a 2d game. Game has an enemy which has a shield with it. The shield( child) has a shield manager script and a collider with it. When it collides with the player it pushed the player back and destroys itself.
And the Enemy(Parent) has an enemy controller script. It has a collider too. And the enemy contoller has an OnCollisionEnter2D function in it for detecting if the player attacks it. But the parents OnColliderEnter2D function being triggered in both of the colliders.
So shield are being destroyed but also the enemy are also too. The parent detects its child's collider.
How can I seperate them?
Is there a way to find which one of this colliders are colliding with the player?
You can put enemy and shield in different layers. And in layer collision matrix, you can disable collision detection between these two layers.
You can find layer collision matrix in Edit > project settings > physics

Simple problem on Unity 2D with empty object collider

I want to make one empty object that player can pass but enemy can't pass.
I have added collider to the object. so I can see triggering of collider event with object and player, enemy.
But both are passing the object. Only I need player passing.
What I should add?
Navigate to the Unity Physics 2D settings (Edit>project settings) and then scroll down and uncheck the unwanted colliders that u don’t want to collide with each other.
Make sure to give the player, the object collider and the enemy a unique layer.
Uncheck the layer between the player and the object collider layer hence will allow the player only to pass.
You have multiple options.
One of them is using Layers and the Physics2D -> Layer Collision Matrix. Here you can define which layers shall be interacting with each other and which should ignore each other.
See also Layer-based Collision.
So e.g. have three layers
Player
Enemy
Goal
and make the collision matrix to allow collision between Enemy and Goal but ignore collision between Player and Goal. Then make all colliders isTrigger = false.
=> Only the object(s) with the layer Player can pass the object(s) on the layer Goal while the object(s) with layer Enemy will collide with them, thus can not pass them.
This can only be set up beforehand. You can however still make objects dynamically switch layers and thereby even on runtime control which objects can collide with each others.
As alternative you can also make collisions between specific individual colliders be ignored by using Physics2D.IgnoreCollision
Makes the collision detection system ignore all collisions/triggers between collider1 and collider2.
Using this you can dynamically enable and disable collisions between specific colliders.
But if you actually want to be able to have an event be triggered by both player and enemy but the enemy shall not be able to pass the goal then your setup should probably be like
Goal isTrigger = false
Enemy isTrigger = false
Player isTrigger = true
this way the player will pass the goal, the enemy won't.
Then you would use both OnTriggerEnter2D to detect the player collision and OnCollisionEnter2D to detect the enemy collision.

How to have collision detection without physics in Unity?

I am making a simple Unity 2D game. In this there are three gameOjects - 1) The Ground (a long rectangle)2) The player (a square)3) The coin (a circle)Currently i am using colliders on all three. These colliders help the player remain on the Ground and when the player collides with the coin i increment the score.But the problem is that when the player collides with the coin it behaves as if the coin is a physical object and it stops the player.I dont want thisI want that when the player collides with the coin there is no physical interaction between themI tried one thing - switching the colliders in player and coin to isTrigger, but in this case the player and the ground interact in the way they should (the player falls through the ground).What should i do??
Make the interactable objects triggers. That way, you can interract with them, without obstracting your path. To do that, go to the collider Component on the inspector and check the "Is Trigger" checkmark.
Then, go to your scripts and instead of the event OnColliderEnter use the event OnTriggerEnter

Unity 2D how to avoid my player moving over the enemies

I'm doing a platformer and I have colliders and rigid bodies in my hero and my enemies. I have also colliders on my platforms.
Everything works and moves nice, dudes move along the platforms, they jump and they catch each other.
I use the collider in my enemies to discover if the hero touches them and then deal damage to the hero. And when slashing I use "overlapCicle" to discover if the sword touched the enemies.
My problem is that with this setup my hero and my enemies can walk one in the top of others. Additionally if I disable the collider of an enemy (to make it invulnerable for a second after being hit) it will fall through the platform..
What's the best approach to this structure of colliders? I want everybody to walk over platforms. I want enemies colliders to detect the hero touching them and I want my sword (overlapcircle) to find enemies. And I want hero and enemies to be able to walk across each other, specially enemies.. they should not walk one over the others
Seems like you want the enemy and the player to be able to walk through each other, while being able to interact with each other.
In that case, you can create another physics layer for your interactions (attacking or vision detection), and set-up your Physics layers to ensure that the Player and Enemy do not collide, but their interaction layers can collide with the respective characters.
Like so:
Where PlayerTriggers and EnemyTriggers will be physics layer for interaction between the enemy/player.
This ensures that the enemy's vision/attack collider can hit the player, but the enemy itself can't do so. Vice versa.
To access the physics layer menu, go to Edit (Top left) and click on Project Settings.
You can read more about it in Unity's Doc.

Collision detection, without the physics?

I have enemies following a path in my game. Along the path I have turrets shooting at them. To get collision detection between the bullet of the turret and enemy i've added RigidBody2D, and Circle Collider 2D to the enemy, and Circle Collider 2D to the bullet.
When the bullet hits the enemy it shall not push the object, just destroy the bullet and take some of the health.
I feel Rigidbody2D is messing it up, it gives mass, drag and gravity to my enemy... but I dont need it. But it seems I do if I want to have collision detection bewteen two objects?
You need Rigidbody to detect collisions, however you are just triggering some actions (taking damage). That is why you can check isTrigger on your colliders, and then implement 'OnTriggerEnter2D' callback instead of 'OnCollisionEnter2D', which won't activate any physics.
See http://docs.unity3d.com/Documentation/ScriptReference/Collider2D.OnTriggerEnter2D.html for reference.