Make two physics objects not colliding, but detect collisions in Unity

I have a Unity project that has a 2D game world that consists of static colliders to make the geometry solid for the characters that inhabit it. The player is a dynamic collider (with a non-kinematic hard drive). There is also the character of the enemy, which is also a dynamic collider. Both characters walk on the floor and jump into the walls, as I expected from them.

What I want to achieve is that the player and the enemy are not continuous with each other, so they can move towards each other. I achieved this by putting the enemy and the player in separate layers and setting the collision matrix so that these layers do not collide with each other. However, the problem that I am facing right now is that I want to determine if the enemy and player have collided with each other. I added a trigger collider to the enemy character, he is at the enemy level, which means that he does not detect a collision with the player.

I thought about creating a sub-game object for the enemy, put it on the player’s layer, add a rigid body to it and launch the collider and use it to detect collisions between the player and the enemy, but it feels so confused that it leaves me wondering, no Is there a more elegant solution for this.

+6
source share
1 answer

Yes, you need to create a GameObject using the trigger collider and place it in a layer that interacts with the player’s layer.

No, you do not need to add a Rigidbody to a new GameObject , the parent hard drive already makes it a dynamic collider, so you will receive OnTrigger events.

As a side note, just to keep things organized, if you are creating an enemy child, do not put it in the player’s layer. For example, in the future you may need to disable the collision of a player’s layer with itself. In addition, if your player interacts with many objects, I would put one trigger per player instead of enemies on a separate PlayerTrigger layer, so that everything was just fine.

Is there an easier way? Not really. You definitely need not to interact between the player and the enemy colliders, but some kind of interaction between them. Therefore, one of them should cover two layers, or the entire interaction will be described by one bool. The physics engine processes a lot of information at a time, so you can set all the layers and collisions you want, but during the physics cycle you no longer control what is happening. You cannot force the engine to ignore collisions between two objects. With only 32 layers, and if they behave tough, they really represent a great optimization. If you are concerned about the performance of creating another layer, turn off the interaction between layers that you don’t need, such as a trigger layer, floor and walls, or layers that don’t even touch.

Your alternative does all this by code, which is even less elegant. One baby capsule on the player doesn’t sound so bad right now, does it?

+3
source

Source: https://habr.com/ru/post/989064/


All Articles