How can I make a gameObject with Boxcollider2d and the Boxcollider2D effects to gameObject A but moving through gameObject B - unity3d

I'm new in unity and i'm trying to buid a 2d game and I want to know how to create a gameobject with boxcollider2d and the boxcollider2D just work when the game hits gameObject A but do not work when interacting to gameobject B.

Assign your objects to different Layers.
By doing this you can either set them to collide with each other, or to ignore any collisions between them. So make an A layer for all your A objects, and a B layer for all your B objects. (Of course you can name it whatever you like) Go to
Edit > Project Settings > Physics
where you can edit the layer collision matrix, to enable or disable collisions between layer elements.

Related

Collision Layer Matrix and Parent/Child object relationships

I come here after some testing, and because after some googling, I was incapable of finding a straight answer.
Suppose I have a player character with a tag set to Player and a layer set to Humanoid. I also have a bunch of NPC performing patrol, wander, and other movement behaviors. Their tags are set to NPC and their layers to Humanoid. Since this is a 2D game using 2D physics, I used the Project Settings collision matrix to make it so that Humanoids do not collide with each other.
In other words, the NPCs and the player characters can pass through each other, while respecting gravity, force, impulse, etc... This is working fine. My problem arises when: I want some child GameObject of NPC which is in layer Default and has a CircleCollider2D of type Trigger and I want to detect that collision in order to perform some actions (e.g.: show dialogue, stop or switch AI Behaviour of the parent object, or others).
From my testing, this seems not to be working, but I might be doing something wrong. So my question is:
If a GameObject A ignores collisions against layer X, will collisions against a GameObject C in layer Y be ignored if C is a child of a GameObject P in layer X?
private void OnCollisionEnter2D(Collision2D other) {
if (other.gameObject.CompareTag("Player")) {
Debug.Log("Collided with player");
}
if (!eventConsumed && other.gameObject.CompareTag("Player")) {
var draw = Random.Range(0.001f, 1.0f);
if (draw > 1.0f - eventChance) {
dialogueEvent.Play();
dialogueEvent = null;
}
eventChance = Mathf.Clamp(eventChance * 1.33f, 0.0f, 1.0f);
}
}
In order to detect whether or not a physics collider that is set as a trigger has collided. You would need to use the OnTriggerEnter2D function instead of the OnCollisionEnter2D function.

Character falling through floor unity

I had this working just fine until I decided I wanted to change how I organized the colliders just a few minutes ago to avoid having multiple colliders on the same object.
I have a "Player" object with a boxCollider2d with isTrigger=false and an empty child called "triggers" which itself has 2 children, each with a single boxCollider2d and both with isTrigger=true. These triggers are used for detecting when the player is grounded or next to a wall but are not supposed to affect the physics.
I also have a "floor" object which has a sprite and an empty child called "colliders" which has a single child called "ground" with a single boxCollider2d (isTrigger=false) and the layer of the object set to "Ground".
My understanding is that since my player has a non-trigger collider (and rigidbody) and the floor has a non-trigger collider (at least its child does), that the character shouldn't pass through the floor. Also, it's starting from a height above the ground so it's not an intersection problem.
My best guess is that somehow the child colliders of the player being triggers are turning the main collider into a trigger or that for some reason the floor collider being in a child is screwing with it, but I don't know enough about how these colliders work to really know.
The child trigger colliders should not cause it to mess up like that.
Check Project Settings -> Physics 2D -> Layer Collision Matrix. Are your objects not able to collide?

Does a VR object need a Rigidbody for a collider?

In Unity3D, you need rigidbodies on GameObjects that use colliders, as they use physics. You don't need rigidbodies on static GameObjects because they don't use physics, though you still need to have at least one in the calculation.
My situation is this: I want the game to detect putting your head through a wall with colliders. I'm just curious, as the collider on the head won't use physics, does it still need a rigidbody as it's moving, just not with physics?
My question is: Does a VR object need a rigidbody? (and yes it set it to kinematic)
Colliders → Collision action matrix
=> It depends.
Some of the combinations only cause one of the two objects to be affected by the collision, but the general rule is that physics will not be applied to an object that doesn’t have a Rigidbody component attached.
You said
as the collider on the head won't use physics
But note that in general Collision = Physics.
So yes, at least one of the objects involved in a collision needs to be a (kinematic) Rigidbody.
As you can see in the matrix above e.g. for a collision with a static object the event (like OnCollisionEnter) will be invoked only on the object with a (kinematic o not) Rigidbody but not on the static object.
static here means it has no Rigidbody component attached even if it is moved by code or in other words: As soon as something moves in your scene it should have a (kinematic) Rigidbody component!
You can add colliders to a GameObject without a Rigidbody component to create floors, walls and other motionless elements of a Scene. These are referred to as static colliders. At the opposite, colliders on a GameObject that has a Rigidbody are known as dynamic colliders.

Unity - handle OnCollision on child's Rigidbody

I have a GameObject, constructed from bunch of pills, where each pill is basically constructed from two spheres and a cylinder.
So the GameObject hierarchy looks something like this:
Player (Empty GameObject)
Pill (Empty Game Object)
Sphere (3D sphere)
Cylinder (3D cylinder)
Sphere (3D sphere)
Hopefully the picture will give you guys the visual illustration of what the GameObject looks like:
Now, my goal is to have the entire GameObject - Player as a Rigidbody, while I would like to detect collision on Pill level. Hence I added Rigidbody to Player and Capsule collider to each Pill. However such configuration does not work - the Pill does not receive OnCollisionEnter event. I found suggestion solution, however it does not work for me either - if I set Capsule collider property Is Trigger, then Player does not interact with other Rigid bodies. The only solution I found so far is to add Rigidbody to each Pill, but I am concerned about performance is such scenario.
To sum up - my question is - can I have the above configuration - parent rigid body, child collider work and accept OnCollision events? Of course if I can have parent rigid body and a child with Is Trigger set will also work in case the Player keeps rigid body physics behavior.
I don't know why you want to detect collision on Pill Level but retaining the same configuration one thing you can access is the Collision ContactPoint.

Unity 2D collisions - how to setup objects that collides among themselves

I am starting with Unity 5 and I am straggling a bit with its 2D collisions. Looking a bit into it I found that there are three types of objects that can be defined:
- Static: Just a 2D collider (2D collision box component for example).
- Dynamic: 2D collider + 2D rigid body.
- Kinematic: 2D collider + 2D rigid body set to kinematic.
And as far as I know they collide this way:
Static: Only collides with dynamics.
Dynamic: Only collides with statics and kinematics.
Kinematic: Only collides with dynamics.
I am trying to make a simple Space Invaders and I am struggling to define the collision types of the different elements (aliens, player, alien bullets and player bullets).
I imagine I can set objects to dynamic and disable the gravity to match the correct collision types.
But my question is, for example I want to make a simple game with a few enemies of the same type (instantiating a prefab), and I want those enemies to detect collision with each other. How I am supposed to setup the enemy collision properties to achieve that?
Many thanks in advance!
Personally I would set the enemies in the same layer with each other, and make sure they are colliding with each other. You can set the layer of the prefab in the inspector, where it is located underneath the name, as you can see here:
Once you click on the layer, there is an option to add new layers as well, at the complete bottom.
Then I would set the Layer Collision Matrix through menu Edit - Project Settings - Physics 2D to match the layers that need or need not to collide with each other, like so:
In this example the Enemy layer won't collide with any other layer than itself.