Trying to use IsTouchingLayers for directionswap

I’m trying to use IsTouchingLayers to initiate a directionswap instead of using colliders, in order to use what we learned in the playermovementscript.
However the boxcollider does not seem to recognize touching the groundlayer at all.

Any ideas on how to fix this?

Thanks in advance!

Hi Bastimoo,

Are you referring to the enemy’s box collider? Is to, how is it supposed to touch the ground while it is floating above the ground?

when it moves horizontally, it eventually runs into the ground (below the mushroom), at which point it should touch the ground layer. However that does not seem to happen, it simply keeps on going through the ground and “floats” through. If I add a debug.log, it doesn’t get fired either, so it doesn’t recognize the groundlayer.

If the “Touching ground” message never appears in your console, we can assume that the if-block never got called. That’s good to know.

Did you double check the spelling of the layer in Unity? Maybe it is spelt "Ground " with a space character.

Sorry, I forgot to reply. Yes, I doublechecked spelling on the layer as well as in script.

What are the settings in the Enemy’s Rigidbody2D component?

Is the enemy moving? If so, I’m wondering what your idea is. The PolygonCollider2D is already touching the ground layer. Even if the box collider touches the ground layer at some point, nothing in the execution of the current code will change.

How many times does “Touching ground” appear in your console before the enemy reaches the wall?

I’ve channeled my artistic abilities to hopefully make my idea a bit clearer. :smiley:
The red circled collider shall touch the “ground” layer of the wall on the right. While the enemy “feetcollider” touches the ground layer already, the top collider does not, thus my idea was that when the top collider touches the wall to the right, it may turn around.

Is the “ground” game object assigned to the “Ground” layer? Please share a screenshot of what you see. Also share a screenshot of your entire console window while your game is running. I’d like to see if “Touching ground” appears there and if the message filters are enabled/disabled.

Also test the “Ground” layer with myRigidBody. See here. If it does not detect the “Ground” layer either, the PolygonCollider2D very likely did not find it either. The Rigidbody2D acts as a compound collider meaning it is affected by both the PolygonCollider2D and the BodyCollider2D.

An if-statment with a simple Debug.Log is sufficient.

The definition of IsTouchingLayers:

Checks whether any of the collider(s) attached to this rigidbody are touching any colliders on the specified layerMask or not.

It might be that your two colliders do detect the ground collider but not the “Ground” mask.

Good evening!
Since I changed the code and game according to the video, I had to reproduce the error including the collider.
Great news: It works now! Not so great news: Every time I touch the groundlayer, the Enemy moves left and right RAPIDLY for about a second, causing about 50-100 groundlayer touches (according to Debug.Log and rapid animations - it looks fun!), until it turns to the right direction and wanders off again.

So the touchgroundlayer is fixed now, however it now touches 50-100 times - new problem - any idea for a new solution?

Thanks in advance!

First of all, good job on fixing the issue with the ground layer. :slight_smile:

How many times does “Touching ground” appear in your console? The background is not assigned to the “Ground” layer, is it?

Or does your idea work and the if-block gets executed when the box collider touches the edge? If so, pause the game and click the button on the right-handed side of the Play button to flip through each frame. I have an idea what the problem might be but maybe you’ll be able to figure it out yourself. Let me know if you noticed something interesting while checking the situation frame by frame.

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

Privacy & Terms