Goober isn't hitting the wall!

Hey all,

Please could someone suggest what else I can check? I’ve been back through the videos several times, but it’s not working for me. I’ve added my enemy (goober), it animates, and moves to the right. But it doesn’t hit the wall and turn around. It just passes into the wall and keeps on moving right until it disappears off screen.

I’ve added the ‘periscope’ box collider. It’s set as a trigger. I’ve checked my code against the Github source and it’s the same. I’ve checked that the enemy is in the right layer (its own) and checked the matrix and it can interact with the layer (‘ground’) that my platform tilemap is in. I’ve tried adding Debug messages to OnTriggerExit2D, and I’ve added an OnTriggerEnter2D with a Debug message as well, neither are triggered. I saw in another thread to try disabling and re-enabling the TilemapCollider2D, which I’ve also done.

Any thoughts gratefully received!

Thanks

Hi Glum,

Move the enemy’s “periscope” collider a bit father to the right, so it does not touch the capsule collider. Otherwise, it might be that the capsule collider interacts with the wall or ground collider too, flipping the enemy again so it crosses the edge like in your screenshot.

Did this fix the issue?


See also:

1 Like

Hi Nina,

Thanks very much for your reply. Sadly this hasn’t worked. I moved the colliders so they’re not touching but little goober still just mindlessly walks through the wall.

I tried moving my bouncy mushroom in front of Goober. The player collides with it, but again Goober goes right through it. I’ve toggled the layers off and on again in project settings and made sure the enemy layer can interact with the ground. I’ve removed the RigidBody2d and Collider components and re-added them, but that still didn’t help. The player can physically interact with the Goober - can push it, or be pushed along by it. (In fact if I stand Player in Goober’s way, Goober pushes player through that same edge of the platform, which is kind of funny.)

So far the only thing that’s ‘fixed’ it is when I changed the body type on Goober’s RigidBody component from Kinematic to Dynamic. When I did that (and after adding freeze rotation, without which it amusingly rolls along) Goober hits the wall and stops moving. So I thought I’d follow that and try using OnCollisionEnter2D in the code to make him reverse. However it doesn’t seem to get triggered. Even if I move Goober up so he drops onto the ground, or if the Player bumps it, it doesn’t seem to call OnCollisionEnter2D.

I’ll keep digging!

Thanks

Could you please share another screenshot of the Goober so I can see the new position of its colliders? And maybe also of the collision detection matrix?

Have you already tried to add a Debug.Log to the method which is supposed to flip the enemy? Maybe it does not get called. A common mistake is a misspelt method name, e.g. OnTriggerEnter2d instead of OnTriggerEnter2D. Figure that out first to avoid wasting your time looking for problems in the wrong place.

You wrote that you had disabled and re-enabled the TilemapCollider2D. Double-check if the collider shape was recalculated correctly. If you see any gaps between the tiles, that could explain the Goober’s behaviour.

Also disable the “ground” game object to see if the wall disappears along with the ground. Maybe it is on the wrong layer.

Hi, thanks again for the advice. I’m still pretty stumped by this!
Here’s a new picture of Goober and his colliders:


It also shows the platforms tilemap with its green line showing the collision boundary that I think is right. The player can’t pass through that edge, only Goober. And if I change Goober to a dynamic not kinematic rigidbody he collides with the wall. (But he doesn’t turn round I guess because the trigger isn’t activated)
This is the collision matrix:
matrix
My naming has been a bit idiosyncratic unfortunately so while the tilemap is called platforms, the layer its on is called ground.
I’m not sure how you disable a layer, but I went through all the tilemaps and un-ticked their renderer components to make them disappear. The elements disappeared as expected - I think the tiles are correct.
I have been adding Debug.Log() to my code as I’ve been trying different things, and not once has it been called, so there’s definitely something there preventing it. When I switched the RigidBody to Dynamic, I also tried using onCollisionEnter2D, but that didn’t seem to get called either. At the risk of making this comment really long, I’ve pasted the code below. I’ve checked it against the Github link in the lesson, and aside from me using different variable names, it looks the same to me.

Again, thank you!

public class EnemyMovement : MonoBehaviour

{

    Rigidbody2D EnemyRigidBody;

    [SerializeField] float EnemySpeed = 1f;

    void Start()

    {

        EnemyRigidBody = GetComponent<Rigidbody2D>();

    }

    void Update()

    {

        EnemyRigidBody.velocity = new Vector2 (EnemySpeed, 0f);

   

    }

    //void onCollisionEnter2D(Collision2D Collision)

    //{

    //    Debug.Log("Collided");

    //}

    void onTriggerEnter2D(BoxCollider2D Collider)

    {

        Debug.Log("Entered trigger");

    }

    void onTriggerExit2D(BoxCollider2D Collider)

    {

        Debug.Log("Exited trigger");

        EnemySpeed = -EnemySpeed;

        FlipEnemyDirection();

    }

    void FlipEnemyDirection()

    {

        transform.localScale = new Vector2 (-(Mathf.Sign(EnemyRigidBody.velocity.x)), 1f);

    }

}

Yes, that’s what I meant. If you are sure that all tiles are on the correct layer (here: “Ground”) and if the collider shape looks correct, which it does, we can exclude (for now) that there is a problem with the tilemap.

The issue is indeed in your code. C# is case-sensitive. onCollisionEnter2D is not Unity’s method and won’t get called during a collision event. OnCollisionEnter2D with a capital O is Unity’s method. Fix the typos, then your code should hopefully run as expected. Remember you can also look at the lecture code changes via the link in the Resources of each lecture. :slight_smile:

image

Oh boy do I feel stupid now! Yes you are entirely correct. I’ve capitalised the O and it’s working now. I can’t believe I didn’t spot that. I even went back and specifically checked the ‘D’ in ‘2D’ was capital because that caught me out before. In my vague defence, my recent experience is all in languages that are not case-sensitive…

Thanks very much for you help! Problem solved. :slightly_smiling_face:

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

Privacy & Terms