#How top detect collisions from the side of a tilemap

1 messages · Page 1 of 1 (latest)

rapid lion
#

So I'm trying to make a platformer, and I want my enemies to go back upon touching the walls, which are part of a tilemap. Unfoprtunately, they just keep walking into the wall, and they only detect it very inconsistently.

#

'''void OnCollisionEnter2D(Collision2D col)
{
Debug.Log("Collision Enemy:" + col.gameObject.tag);
if (col.gameObject.CompareTag("Ground"))
{

        Vector3 dir = (col.gameObject.transform.position - gameObject.transform.position).normalized;

        if (dir.x > 0)
        {
            // hit right
            bIsGoingRight = !bIsGoingRight;
            transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
        }
        else if (dir.x < 0)
        {
            // hit left
            bIsGoingRight = !bIsGoingRight;
            transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
        }
    }
'''
granite haven
#

!code

tawny domeBOT
#
Posting code

📃 Large Code Blocks
Large code blocks should be posted as links to services like:
https://gdl.space/, https://paste.ofcode.org/, https://hatebin.com/
https://paste.myst.rs/, https://hastebin.com/

📃 Inline Code
Surround code with three backquotes. Not quotation marks.
To get C# formatting the first line should only contain cs or csharp.
Add a comment with a line number if there is an error message.
```cs
// Your code here
```
Do not share screenshots of code unless requested.

dapper root
rapid lion
#

i know, i'm trying to fix it

granite haven
#

I would not use OnCollision either

#

too inconsistent

#

Raycast is much better