unity - how two objects with rigidbody can pass through each other? - unity3d

(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.

Related

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.

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.

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.

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?

How to get a wind effect afecting multiple meshes in Unity3D

I have a very tall, and thin, tower of cubes one on top of the other, each with a rigidbody attached.
I'm trying to build it higher and higher and have a wind blowing at random heights to try to blow it up and demolish it. It seems simple, but I can't do it properly.
I need to do something as the wind zones for tree, and I cannot attach the forces to the cubes since I need something to instantiate randomly at certain times or places that "pushes" the cubes to make them fall.
I've tried with a particle system, but I was unable to make the particles as collider objects (is this even possible?).
Any suggestion?
Interactive cloth might be your choice.
To add an Interactive Cloth in the scene, click menu
GameObject -> Create Other -> Cloth.
Add new anchor GameObjects (Menu GameObject > Create Empty Object)
For each game object, add a collider component (Menu Component >
Physics > Box Collider)
For each collider, specify the center and size of the collider
For each collider, specify the 3D location of collider, at which the
cloth remains static
Make sure the collider overlaps/intersects with the cloth, otherwise, it won’t work.
For your case, if you wish to programmatically specify the anchor position (since it is random), you might wish to refer to this tutorial as well.