SOLVED: Why does Ground layer affect enemy?

I wanted a set of enemies that move vertically through the air and changed direction at a predetermined point, I managed to get an enemy moving vertically and changed OnTriggerExit2D to OnTriggerEnter2D.

I made some game objects with box colliders to work as invisible wall - though I wonder if there’s a way to literally box them in to a specific area rather than place multiple walls around them…

The enemy just passed straight through them, after a lot of messing about (and somewhat out of desperation, really not thinking it would work) I changed the layer of my invisible wall to ‘Ground’ suddenly it worked, but there’s no mention of the ground layer in the enemy script I really don’t understand why that worked??

I then added an EnemyWall layer and changed it to that, this also worked, but if layer is set to default it doesn’t work, so I guess maybe I just don’t have a good enough understanding of how layers work?

Hi Dominic,

How do you move the enemy?

The same method Rick used, just swapped around the axis

Does that mean you manipulate the transform.position directly? If so, it’s no surprise that the collision does not seem to work because your code is overriding the values calculated by the physics simulation.

This is the original code made by Rick to make the enemy walk along the platform and turn back when it reaches the edge:

[SerializeField] float moveSpeed = 1f;
Rigidbody2D myRigidBody;

// Start is called before the first frame update
void Start()
{
    myRigidBody = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update()
{
    if (IsFacingRight())
    {
        myRigidBody.velocity = new Vector2(moveSpeed, 0f);
    }
    else
    {
        myRigidBody.velocity = new Vector2(-moveSpeed, 0f);
    }
}

bool IsFacingRight()
{
    return transform.localScale.x > 0;
}

private void OnTriggerExit2D (Collider2D collision)
{
    transform.localScale = new Vector2(-(Mathf.Sign(myRigidBody.velocity.x)), 1f);
}

Here’s my reworking of the code to make the enemy float up and down:

[SerializeField] float moveSpeed = 1f;
Rigidbody2D myRigidBody;

// Start is called before the first frame update
void Start()
{
    myRigidBody = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void Update()
{
    if (IsFacingUp())
    {
        myRigidBody.velocity = new Vector2(0f, moveSpeed);
    }
    else
    {
        myRigidBody.velocity = new Vector2(0f, -moveSpeed);
    }
}

bool IsFacingUp()
{
    return transform.localScale.y > 0;
}

private void OnTriggerEnter2D (Collider2D collision)
{
  
    transform.localScale = new Vector2(1f, -(Mathf.Sign(myRigidBody.velocity.y)));
}

It does work, but the game object I made as an invisible wall with a Box Collider 2D only sets the enemy back in the opposite direction if I change the layer of the invisible wall to one I’ve added myself.

Go to the Physics 2D settings and check the collision detection matrix. Maybe the invisible wall was/is on a layer that does not interact with the enemy’s layer.

Ah, mystery solved! Thank you! I was looking at the problem from completely the wrong angle.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms