Issues with 2D Collision, regarding layers and casts.

Hi!

I’m currently working on a personal project, and I’m losing my head regarding collision layers and physics casts. Here is my setup:

I have a 2D character with the following properties: a rigidbody, a circle collider and the ‘Character’ layer.
Screenshot: Screenshot - e45978dc6921e95d601e0fb9802ea74a - Gyazo

I also have a several platforms, all having an edge collider. All of these are currently on the ‘Default’ layer. In my Character script I have an IsGrounded method which uses a CircleCast() to check for the ground.
Code: public bool IsGrounded { get { RaycastHi - Pastebin.com

  1. Here is my first problem. If I change the layer for my floor platform from ‘Default’ to ‘Platforms’, my IsGrounded breaks. It will not return any colliders. This confuses me since my CircleCast() checks on the ‘Platforms’ layer, right? :face_with_spiral_eyes:
    Screenshot (Physics2D Settings included): Screenshot - f3f290f659996ca0568258a502c629e5 - Gyazo

  2. Now comes my second problem. My Character script will also animate and move my character, based on some simple logic. My next step was to make the character climb a ladder and afterwards make him move again. This works… For one ‘Lyker’ instance. If I create another instance of the same ‘Lyker’ prefab, and make them both climb the ladder, one will succeed and one will fail, sometimes getting stuck against the edge collider or just failing through all floors. This depends on the place where I put him.

Video:

My complete Character code can be viewed here: using UnityEngine;using System.Collections;public class Character : MonoBe - Pastebin.com
I hope I’ve explained it well enough. If there’s anything you need to know more, just ask, any help is appreciated.

Also, no hate on the graphics, it was the easiest to find and animate. :smile:

So it looks like the the mask you gave is a zeroed bit field. Try this:

int mask = 1 << LayerMask.NameToLayer(“Platform”);

Obviously update the circlecast too.

You are using Update() to apply physics forces which should be applied in FixedUpdate()

Seems like that fixed, great! How come I have to use a bit shift in the CircleCast but not in the IgnoreLayerCollision?

Aah, yeah, totally overlooked that. Thanks haha!

Even though, I’m still getting that weird behavior regarding my second issue. Here is my current code: http://pastebin.com/EVUR7KVD

Thanks so far. :slight_smile:

I fixed it! Here are the changes I made:

  • Instead of using IgnoreLayerCollision() on line 34, I changed this method by using Platform Effector 2D for the same result.
  • Instead of checking for IsGrounded on line 57, I now check for collision with the ladder trigger using OverlapCircle(), and stop climbing once this collision stops.

Here is my code right now for anyone wondering: using UnityEngine;using System.Collections;public class Character : MonoBe - Pastebin.com

1 Like