there are two objects (player and enemy, each on its own layer, colliders and rigged2D hanging, Unity standard physics used, 2D game). In the settings, the collision matrix has disabled collisions between these layers so that objects can pass through each other. But as is usually the case in 2D games, when a player touches an enemy, the player needs to take damage and fly away from the enemy. Next, a short-term invulnerability is turned on and the character can easily pass between such enemies (that is why collisions are disabled). Since collisions are disabled in the collision matrix, events such as OnCollisionEnter, OnTriggerEnter do not work. In my head there is an idea to use Physics2D.OverlapCapsule (since the colliders are capsular on objects) in the update or in the fcsedapdey, but I don’t know how it will affect the performance. In Unity is new, so please do not throw too much slippers =) If there are any ways to make it easier, I will be glad to hear them. Thanks in advance =)
2 answers
Colliders have the ability to behave like triggers. Overview of colliders In this mode, they do not create obstacles, but they still cause the events of the OnTriggerEnter physics and so on.
- But if I make a collider trigger, then because my enemy will simply fall through the floor? Or I do not understand something? - Mopnex
- True, we need a more flexible structure of the enemy. The enemy is an empty object, there are 2 sub-objects in it, Odin is responsible for the work of physics (being on the floor). The second is damage. From the advantages of this approach, the damage component can be one for many places: enemies of the trap and so on. - Egor Bashinsky
- Thank! A very flexible option will work out, I think what we need! - Mopnex
- Always welcome, if the answer is right, mark it. - Egor Bashinsky
|
The easiest solution in terms of performance would be to use Raycast . When producing a raycast, you can specify a mask for the physical layers to which the beam will react. In your case, they will probably have to be thrown away somewhat in different directions (but this will not greatly affect the performance).
- I know about reykasts, I know that it is a very cheap operation in terms of performance, but it is still interesting to know, is Physics2D.Overlap much harder? - Mopnex
- You can estimate purely mathematically. It is much simpler to calculate the intersection of a ray (raycast) and a collaidra (from the point of view of mathematical actions) than the intersection of two geometric figures (for example, two capsules) - vmchar
|