There is Box Collider 2d, how do you know which side it touched one trigger or another? For example, if he touched on horseback or with the right side, something happened about a certain action.
1 answer
1) Method. The most ancient.
As can be seen from the figure, a square player object will say that it collides with its upper part if its Y coordinate is greater than the lowest Y coordinate of the enemy (or any other object). If the lowest Y coordinate is greater than the highest Y of the opponent, collide below. If the leftmost coordinate in X is less than the opponent’s rightmost X - a collision on the left. If the rightmost coordinate on X is greater than the leftmost X of the opponent, collide on the right.
Those. do the method that will calculate all this and return as a result of the check from which side it hit / entered the trigger.
Memo . It should be remembered that the center in a unit is usually in the center (and not at the top left, as per the standard). Therefore, the calculation of the extreme right coordinate on X will be approximately as follows: the праваяСторона = координатыЦентраИгрока.x + ширинаИгрока/2 . X праваяСторона = координатыЦентраИгрока.x + ширинаИгрока/2 . Those. It is necessary to push off from the center and from the width of the player.
2) Method. - Use Physics2D . It at least includes several methods.
2.1) OverlapCircle .
OverlapCircle(Vector2 point, float radius, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity) checks whether an object with a radius of radius intersects a surface whose layer is layerMask .
We place on the object from each side one EmptyObject , which, just, will indicate, and from which side something happened. for example
IsCollidingRight = Physics2D.OverlapCircle(rightObject.transform.position, 0.5f, enemyMask); Where rightObject.transform.position is the same EmptyObject that is located on the player’s right. enemyMask is a LayerMask , i.e. layer with which to compare the intersection. In this case, it can be declared as a variable in the class: public LayerMask enemyMask; and is selected in the drop-down list in the inspector.
Either announcement:
public int enemyLayer; public int enemyMask; And initialize it like this:
enemyLayer = LayerMask.NameToLayer("Enemy"); enemyMask = 1 << enemyLayer; IsCollidingRight = Physics2D.OverlapCircle(rightObject.transform.position, 0.5f, enemyMask); //IsCollidingRight = Physics2D.OverlapCircle(rightObject.transform.position, 0.5f, 1 << enemyLayer); taste
2.2) LineCast - draws a line from the object in the specified direction and checks for intersection with the object of a particular layer, i.e. You can also use a mask.
EmptyObject one (or several) EmptyObject on each side of the EmptyObject (a little farther than the sides of the object !! ), which, in fact, will indicate which side something happened.
In the script we check for intersection, for example
IsCollidingRight = Physics2D.Linecast(transform.position, rightObject.transform.position, 1 << LayerMask.NameToLayer("Enemy")); The line will be drawn from the player to the object, on the side, and if something of a certain layer falls on this line, then it will be reported
2.3) RayCast - Directs the beam from the object in the specified direction and checks for intersection with the object of a particular layer, i.e. You can also use a mask.
Almost like Linecast , only EmptyObject ( EmptyObject ) are now located on the object itself on each side one (or several), which, just, will indicate which side something happened.
IsCollidingRight = Physics2D.Raycast(rightObject.transform.position, Vector2.right, 1.5f, 1 << LayerMask.NameToLayer("Enemy")); IsCollidingUp = Physics2D.Raycast(rightObject.transform.position, Vector2.up, 1.5f, 1 << LayerMask.NameToLayer("Enemy")); ... So, how to work with the rays: start up a few, attach them right on the object or generate it in code (for example, entering into the List and work with the list) - it's up to you to decide.



