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

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.

Related

Collider2D not detecting collisions at all unity

I am making a 2 player fighting game in unity and am using boxcollider2Ds for attack hitboxes and compositecollider2Ds for defending hitboxes. One time I noticed that the tag assignment for enemy was actually in reverse, so I changed that. but after tweaking a few more things, I noticed that the collision wasn't actually working, so I decided to print out two bools indicating if either condition was met, with the first being if the colliders were touching, and the second being if the move that was hitting you had already hit you. Both were continuously false, even after I did a move, so I soon realized that one of my colliders was in a child object but the class instance was taken from the collider in the parent object. but the collisions still didn't work and the actual collisions weren't working. Then I went to check on the box colliders and composite colliders in the actual unity UI(both being triggers) but it wouldn't detect anything, and it wouldn't even show up in the contacts tab.
for reference here are what colliders are being used
player1
boxcollider2d not a trigger
compositecollider2d: istrigger
defending hitboxes: boxcollider2d, usedbycomposite
defending hitbox 1,2,3 and 4 boxcollider2d, usedbycomposite
attacking hitbox: boxcollider2d, istrigger
can someone let me know what could be preventing it from detecting contacts?
The problem is most likely the Is Trigger bool. When it is enabled, Other objects will be able to pass through it as if it does not have a collider.
Their is also different functions for both collision and trigger. the OnCollision functions are used on non Is trigger colliders while if that bool is true, the function is OnTrigger.

Is it possible to have collision on a child object moves its parent in Unity?

I'm attempting to develop a 2.5D fighting game in Unity, and I've gotten to the point of attempting to make basic collision between players function. I'm attempting to do so via collision boxes like those seen in the video here: (4:01 if the timestamp doesn't work)
https://youtu.be/m5yRLhAx4Ro?t=241
What I have attempted to do in order to replicate this system of collision is to make a collision box prefab with a dynamic rigidbody and box collider. A script instantiates a new one as a child of the player and destroys any existing ones whenever the collision box's size and position need to be changed. However, I've found that this does not work. I can get the players to collide with each other, but when I disable that collision by putting the parents on a layer that doesn't collide with any others, the collision boxes themselves will just phase through each other along with their parent objects, the players.
What I want is a system in which the players will be responsible for their movement, but the child objects (the collision boxes) will function as the players' colliders. If anyone knows a way of doing this, or any other way of achieving this sort of system, I would greatly appreciate the help.
Just add a Fixed Joint on the child object then connect it with the parent rigidbody.

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.

Collision Events in Unity3D

I noticed that OnCollisionEnter2D does not trigger when two objects with Collider2D components make contact. It only triggers when one of them has a RigidBody attached to it. This seems odd to me, because the Unity editor itself says that having a static collider in place of a non moving RigidBody is much better for performance. Then why does Unity not allow two game objects with static colliders trigger collision events when they make contact?
Sure, static colliders are more performant, but as the word indicates: they are static.
Movement requires physics calculation and therefore a rigidbody (which holds information about speed etc.). This is actually stated in the docs for the normal collision here http://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html

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?