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

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?

Related

It is impossible to implement accurate collisions in 3D SpriteRenderers? Is Billboard incompatible with rigidbodies?

I've been having difficulty implementing enemies with a billboarding system for a while:
For a new project I'm reusing some animated sprites from an old static camera shooter game I made. It consisted of shooting projectiles and hitting enemies advancing towards your position. The enemies were capsules which had an animated plane in front of them.
I wanted to go a step further and make a raycast system that would detect if the impact on a plane is on the alpha part of its texture. I achieved it using a SpriteRenderer and a plane that matched the size of the sprite which receives the impact of the Raycast.
The problem comes with the movement of the enemies. If I use Rigidbodies this conflicts with the plane which detects the impact, since I have to use a non-convex Mesh Collider for RaycastHit.textureCoord (With Convex colliders it doesn't return the correct position). I need Rigidbody based movement for the enemies to push each other when colliding.
I tried using Transform Movement, since the error does not occur, but this allows enemies to pass through each other and occupy the same space.
I started investigating navMesh but I'm not sure if it is possible to avoid collisions.
I started to place a group of trigger colliders on the enemy mimicking the shape of the sprite. But this makes it difficult for me to detect collisions when enemies do certain animations (Some enemies jump diagonally or their animations are a bit jittery).
I would like to keep the impact detection system for the sprite, but maybe there is another way to check the texture at the impact location.
I think my options are:
With rigidbody based movement: separate the animation and hit detection system from the enemy movement. And make the billboarding effect teleport to the object constantly.
With Transform movement: Detect if they are overlapping and move them in opposite direction to the overlapping to simulate the collision.
With motion based navMesh: I'm not sure if it will be useful to me, since the characters jump and I also believe that a Mesh Agent ship cannot simultaneously be an obstacle. I understand that the navMesh cannot be generated in runtime.
Leaving texture based detection and using the trigger colliders group: Animate the colliders to also follow the animations. Or simplify everything to a large collider, losing precision on impact.
-Find an alternative method to detect the impact coordinates in the texture, avoiding using RaycastHit.textureCoord and therefore Mesh Collider. (Although I would like to keep the impact detection system as it is, maybe there is a similar approach for the same result).
The reason I have not yet delved into these solutions is because I would like to keep the system simple. I'm trying to avoid separating the enemy into 2 gameobjects, simulating collisions or animating collider positions. I honestly don't know which way to go in order to move forward with the project.

Unity physics scenes copy all colliders from main scene

I am trying to use Unity's physic scenes to perform some simulations outside of the main scene. That being said, I need all of the colliders from the main scene in the physics scenes. What is the easiest way to create a physics scene with all of the existing colliders from another scene? Do I have to loop through everything, use SceneManager.MoveGameObjectToScene(...) for every object then move them back at the end of the simulation? This doesn't seem scalable.
EDIT
I have a multiplayer game, when I get responses from the server I am taking their velocity and the servers position, then applying all local movement since that packet was sent via Physics2D.Simulate() to have the velocities correctly applied to the position. Now I need to do the same for the projectiles so I would need the map colliders as well as the player's. I was reading the best thing to do for physics sims is use physic scenes. If I call Physics2D.Simulate() in the main scene for more than 1 set of data (player movement vs projectiles) places I got a lot of screen jitter. Moving these simulations into separate scenes removes the jitter.
The only alternative I see is re-implementing Physics2D.Simulate() myself which seems like a bad (and complicated) idea.
Thanks.
I ended up only needing the collider and rigid body so I created an empty object in the physics scene with these 2 objects. Every time I needed to run the simulation, I would update the rigid body with the current players rigid body details then run the sim and apply the output velocity from the sim to the players rigid body again.

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.

Untiy3D: Detecting a collision without using the physics engine?

I have a nice trail renderer which moves steadly towards some other GameObjects and I need to know when the renderer "gets" to one of these GameObjects.
I've been looking around and it seems that, leaving aside the code controlling the collision itself, I need to:
1) Create a Collider (BoxCollider, for instance).
2) Attach to it a RigidBody, because moving the transform of the collider is not the best thing in terms of performance.
3) Apply a force to the RigidBody to make it move towards the objectives.
Overall, it seems like an overly complex setup to make if one object has just collided with another one, as I don't really it to do anything with physics (The ray won't bounce, nor come back, its all controlled by code).
Am I missing something or this is the only approach to this situation available on Unity?

Collision detection between characters in Unity3D

I am working on 3D fighting game and I am using Unity3D as my game engine. I have a scene, two characters who work very fine and I can control it with my keyboard. But now the problem is in the collision detection.
I have used capsule colliders on my characters and also rigidbody gravity. Now when both these players collide the collision can be detected easily but due to the collision my character will fall down and is not able to stand, just like a capsule. I want to create some realistic effects just like other games have. Is there way to do this?
I would recommend you use a Mesh Collider, but that does not work for animated models.
Here are some suggestions:
Collision for Animated Characters
Note: The mesh collider does not animate. This means that if you want accurate 1:1 collisions for
animated characters, it will need a series of colliders parented to
each joint roughly the same shape as the character.
Another way to do
this is to make a ragdoll out of the character and turn off (or
remove) the rigidbody components. If you just want generic collisions
for humanoid characters, you may use a Character Controller (below).
You have to use primitive colliers because two mesh colliers can never collide with each other.
If you want to make realistic interactions... you should use animations and IK in Unity... its the best way...
For more collision related issues check this : https://youtu.be/Bg73o9JH53c