Simple problem on Unity 2D with empty object collider - unity3d

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.

Related

The problem with the correct use of a rigid body in a 2D game

I decided to make a 2D game for the PC on Unity, where the character moves around the stage. But I have a problem with the correct use of Rigid Body 2D.
In this picture you can see my playing field.
https://i.stack.imgur.com/PBhwx.png
I want my character to move around the entire field and when colliding with objects does not go through them.
My field is Tilemap, but I hung the Box Collider on the Grid. Boxing collider surrounds the grid.
But when I use Rigid Body and turn on the game, the object falls through. And if you remove the Rigid Body, then it just goes through objects.
I tried to use different types of Rigid Body, when using kinematic and static, sprite goes through houses.
The way it works is when you attach a RigidBody2D to an object, you give it physics properties, but it won't do anything useful without a collider. If you have an object with collider attached - every other object with collider attached will collide with it.
In your case the player should have RigidBody2D and BoxCollider2D. The ground should have BoxCollider2D, and basically everything you want the player to collide with should have BoxCollider2D. So the houses should have BoxCollider2D as well.
This setup should allow the player to walk on the ground without falling through it, and collider with houses.
Make sure that the collider's area is correct. You can preview it in the editor and edit it if it's not correct.
Also if you want it to work make sure the camera points down and your game setup is alongside Y axis so if there's a rigid body object falling down it falls down on the ground not before/behind it.

How can i make two game objects are pass through each other in Unity?

I made two game objects, player and enemy.
I hope two game objects collision detect and they are pass through each other.
Thus i put a check mark objects collider's 'is Trigger' and objects's 'rigid body'. So, i expected that objects are pass through each other.
But, player and enemy crashed each other. Why player and enemy crash?
i want two objects pass through each other.
help me please.
A collider marked as a trigger should not cause physical collisions with an incoming Rigidbody (see here). Check and see if one of the player or any objects has a child with a collider that is not marked as a trigger.
Also note that a CharacterController component will be constrained by collisions, as mentioned in the documentation. CharacterControllers do not have a trigger option, so you will have to disable collisions through a different method. The most common methods are using Physics.IgnoreCollision or changing the layers of the objects and disabling collisions between layers in Edit -> Project Settings -> Physics. See here for more details: Layer-based collision detection and How to Ignore Collision between Objects.

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 - how two objects with rigidbody can pass through each other?

(I saw a question like mine, but my case is different)
I explain:
I have a 2d game(side view), where players going around and hit each other with weapons like sword or hammer. I set ground on a collision layer called "Ground" and players In "Player" layer. and the collision of the "Ground" and "Player" layer is checked. the ground has no rigid body but the players have(due to using gravity and etc). I want players don't collide with each other to have a smoother mechanic but I want to detect and receive messages when they pass through each other, for some porpuses that I need.
Now, if I uncheck the "player" collision layer (In Collision2D settings) they can path through but I can't receive any messages due to Inactive collisions. and if I set the player's collider as trigger they pass through ground and fall.
(please correct me if I'm wrong)
so what can I do?
If both objects need to be solid (not trigger volumes) then you need to edit the physics layers.
Change the two objects to be on separate layers (there are 32 layers and only the first five or so have names given by default) in the inspector panel dropdown.
This just happens to be from a project I'm working on, the layers at the top with red marks next to them are the default ones. At the bottom is "edit layers" where you can name and define new layers (up to 32 total).
Go to Edit -> Project Settings -> Physics and change the physics collision mask so that the two layers you assigned your two objects are set to not interact:
Uncheck any pair that should not collide (by default all layers collide with all layers).
Have your player game object have two children objects (one that you could call GroundCollider, on the you could call PlayerCollider)
On the first one, add a collider with onTrigger unchecked. On the second one, add a collider with onTrigger checked
So after playing with what the Toaster said, I find the solution.
The ground collider must be in layer "Ground", the player with a collider with trigger checked in "Player" Layer, and we also need a third layer called for example "GroundCollider" and a child object for the player with a collider with trigger unchecked.
Ok. then in physics 2D settings "player" layer must check collision JUST with itself. and "Ground" layer and "GroundCollider" should check collision just with each other, not with itself or with any other layer.
with these settings, the player collides with ground and two players can check collision with each other and also can pass through each other.

Make two physics objects not collide but do detect collisions in Unity

I have a Unity project in which there is a 2D game world that consists of static colliders to make the geometry solid to the characters that inhabit it. The player is a dynamic collider (with a non-kinematic rigidbody). There's also an enemy character that is also a dynamic collider. Both characters walk over the floor and bump into walls like I'd expect them to.
What I want to achieve is that the player and enemy are not solid to each other, so they can move through each other. I achieved this by putting the enemy and the player on separate layers and setting the collision matrix so that these layers do not collide with each other. The problem I'm having now, however, is that I do want to detect whether or not the enemy and the player ran into each other. I added a trigger collider to the enemy character, it's on the enemy layer which means it doesn't detect collisions with the player.
I thought of making a sub-gameobject for the enemy, put it on the player's layer, add a rigidbody and trigger collider to it and use that to detect collisions between the player and the enemy, but it feels so convoluted that it leaves me wondering if there isn't a more elegant solution for this.
Edit (2020-05-26): Nowadays you can tell the engine to ignore collisions between two given objects. Kudos to Deepscorn for the comment. Will leave the rest of the answer unchanged, and read with that in mind.
Yes, you need to create a child GameObject, with a trigger collider, and put it in a layer that interacts with the player layer.
No, you don't need to add a Rigidbody to the new GameObject, the parent's rigidbody already makes it a dynamic collider, so you will get OnTrigger events.
As a side note, just to keep things organized, if you create a child of the enemy don't put it in the player layer. For example, in the future you might need to disable the player's layer collision with itself. Furthermore, if your player interacts this way with many objects, I'd put a single trigger on the player instead of the enemies, on a separate PlayerTrigger layer, just to keep things simple.
Isn't there a simpler way? Not really. You definitely need non-interaction between the player and enemy colliders, but some kind of interaction between them too. So one of them needs to span two layers, or the whole interaction would be described by a single bool. The physics engine processes lots of information in one go, so you can set all the layers and collisions you want, but during the physics loop you have no further control on what happens. You can't tell the engine to ignore collisions between just two objects. Having only 32 layers, and having them behave in the rigid way they do are actually heavy optimizations. If you are concerned about performance for creating another layer, disable interaction between layers you don't need, like the trigger layer and the floor and walls, or layers that don't even touch.
Your alternative is doing it all by code, which is even less elegant. A single child capsule on the player doesn't sound that bad now, doesn't it?