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

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.

Related

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.

Unity3D Collider Passing Through Other Colliders

The Problem: Mesh colliders on some rigidbody objects are passing through colliders on other objects.
Things I have tried:
With the assumption A is a GameObject with a RigidBody attached and B is a normal GameObject with a collider.
Give A a convex mesh collider
Give A a non-convex mesh collider
Give B a convex mesh collider
Give B a non-convex mesh collider
give B a box collider
give B a convex mesh and box collider
give B a non-convex mesh and box collider
Adjusting the weight of the rigidbody
I have tried all of these in all combinations of A and B.
In addition,
Colliders are not marked as triggers
All objects are on the default layer (0)
Checking isKinematic; doing this seemed to make gravity stop affecting the object, so I have left it as false.
Constraints: I want A to use a mesh collider since most of the objects involved are moderately complex, and fitting other colliders to them would take a while.
Weird Behaviour: I have some objects with both rigidbody and convex mesh collider where the collision is working fine with a non-convex mesh collider. This is inconsistent with other gameobjects. The objects have all of the same settings.
I am using unity version 2019.3.11f1 if that is relevant.
The object being used are from this package. Specifically, the filing cabinet with rigidbodies on the drawers works fine. The desk, office chair, pen, and open laptop all fall through the "floor" (a cube with all of the above colliders tested on it).
Do you have 'isKinematic' checked on the objects with rigidbodies that are going through other colliders? If so uncheck it so that external forces affect it.
edit you also need to click convex on the mesh colliders if they are colliding with other mesh colliders, Convex Mesh Colliders are limited to 255 triangles, are the objects that are not passing through have more than 255 triangles in geometry?
I am Assuming you have set your Collision Detection to Discrete In your gameObject's RigidBody, if so, please make sure to select Collision Detection to Continuous In your gameObject's RigidBody.
Reason Why this is not working
You are trying to use collision that has (speed > your Computer's frame speed) so, the frame is not catching the collider properly some time catches and some time fails to catch.
You are moving your GameObjects with tranform.translate or position etc. If so then make sure to use Rigidbody Related function for Positioning, rotating.
Solution Image For First Problem
I'm assuming
-you game object which contain the mesh component is very small ( bellow 0.7 size(x,y,z) so the game engine is little difficult to detect it)
and also
for the colliding objects at-least one rigid-body component must be attach to one object
This seems to be a common big problem, and I have been struggling with the same issue for days, and have finally fixed it, so am feeling like I should highlight one important thing:
move Rigidbody via MovePosition, not via just changing its position field directly, nor via changing position of GameObject holding it.
Changing position via "wrong" ways will "teleport" your rigidbody, physics won't fully work on it in that case.

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.

Unity3D: Letting a BoxCollider2D collide with the floor 'earlier'?

Pre-info:
I'm making a 2D game in Unity which behaves like Castle Crashers, where the player can move around forwards and backwards like in a sidescroller, but also up and down, kind of like a topdown game - but it's still a 'sidescroller'.
In Unity I'm using Rigidbody2Ds and Boxcollider2Ds for physics.
However, when wanting to simulate things like dropping items, creating gibs or any other object that needs to fall to the 'floor', this gets tricky.
The objects that need to fall to the floor don't know where the floor is, so they'll fall forever.
Question
Can Boxcollider2Ds be set to collide with an individual infinite x-axis?
Object A should collide with the red axis and Object B should collide with the blue axis.
Is this possible?
You could use layers. And in project settings -> Physics2DSettings set them not to collide with each other. There is a hard limit of 32 layers and first 8 are used by system (you can still use them for this) this leaves you with 24 discreet layers - change layer of your objects when they change their position on Y axis. The gameplay might feel awful.
Use 3D physics. tilt your camera 45 degrees on X axis, set projection to ortho, and draw 2D sprites on top of invisible 3D physics objects - then you will have real 2D plane to walk and jump on.
Don't use box2d at all: write your own - simple physics library, you need it only for jumping and falling, right ?