How to seperate the collision detection in unity? - unity3d

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

Related

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

How do I make GameObjects use physics to interact with each other using Vuforia?

I'm using Vuforia for an AR school project and am trying to make a Roll a Ball game. However, the ball keeps falling through the ground despite the fact that both the ground and the ball have colliders. How can I the ball interact with the ground?
In general you always need at least one Rigidbody in order to get collisions via the physics engine.
A Rigidbody
is the main component that enables physical behaviour for a GameObject
What you would probably use for that is a kinematic Rigidbody on your moved object.
This is a GameObject with a Collider and a kinematic Rigidbody attached (ie, the IsKinematic property of the Rigidbody is enabled). You can move a kinematic rigidbody object from a script by modifying its Transform Component
but it will not respond to collisions and forces like a non-kinematic rigidbody. Kinematic rigidbodies should be used for colliders that can be moved or disabled/enabled occasionally but that should otherwise behave like static colliders. An example of this is a sliding door that should normally act as an immovable physical obstacle but can be opened when necessary. Unlike a static collider, a moving kinematic rigidbody will apply friction to other objects and will “wake up” other rigidbodies when they make contact.
Even when immobile, kinematic rigidbody colliders have different behavior to static colliders. For example, if the collider is set to as a trigger then you also need to add a rigidbody to it in order to receive trigger events in your script. If you don’t want the trigger to fall under gravity or otherwise be affected by physics then you can set the IsKinematic property on its rigidbody.
For detailed information also refer to Unity Manual - Colliders and in particular the sections Collider Interactions and Collision action matrix

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

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.

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.