Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Issues with 2D Collision, regarding layers and casts.

Discussion in '2D' started by Lyker, Oct 3, 2015.

  1. Lyker

    Lyker

    Joined:
    Feb 27, 2013
    Posts:
    60
    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: https://gyazo.com/e45978dc6921e95d601e0fb9802ea74a

    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: http://pastebin.com/g0H9SLY0

    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? :confused:
    Screenshot (Physics2D Settings included): https://gyazo.com/f3f290f659996ca0568258a502c629e5

    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: http://pastebin.com/yP1GZFVr
    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. :D
     
  2. _jacks

    _jacks

    Joined:
    Nov 27, 2014
    Posts:
    27
    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()
     
  3. Lyker

    Lyker

    Joined:
    Feb 27, 2013
    Posts:
    60
    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. :)
     
  4. Lyker

    Lyker

    Joined:
    Feb 27, 2013
    Posts:
    60
    I fixed it! Here are the changes I made:

    1. Instead of using IgnoreLayerCollision() on line 34, I changed this method by using Platform Effector 2D for the same result.
    2. 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: http://pastebin.com/aiFfeaZQ
     
    theANMATOR2b likes this.