Search Unity

2D - box collider 2d no longer recognises the layer?

Discussion in '2D' started by TheOnlyTruth, Jun 21, 2017.

  1. TheOnlyTruth

    TheOnlyTruth

    Joined:
    Aug 1, 2016
    Posts:
    32
    EDIT: The problem seems to occur when i add object tag "Platform" and the only thing that uses Platform tag is this:
    Code (CSharp):
    1. public class PlatfromsMotor : MonoBehaviour {
    2.  
    3.     public float platformMovementSpeed = 0.5f;
    4.     public GameObject[] activePlatforms;
    5.     public static int activePlatformsInt;
    6.  
    7.     void Update ()
    8.     {
    9.         activePlatforms = GameObject.FindGameObjectsWithTag("Platform"); //TODO: Move this so that it does not update every frame;
    10.  
    11.         for (int i = 0; i < activePlatforms.Length; i++)
    12.             {
    13.             GameObject platformToMove = activePlatforms [i];
    14.             platformToMove.transform.Translate (Vector2.left * Time.deltaTime * platformMovementSpeed);
    15.             }
    16.         activePlatformsInt = activePlatforms.Length;
    17.     }
    18.  
    19.  
    20. }

    Hello :) I am creating a little project, learning about object pooling and creating various things for my endless 2D runner.

    The problem that i face is a bit hard to explain, but ive been working for 4 hours trying to figure out WTF is going on and ... well, i can't. So i'm asking for youir help.


    First off, my player jumping script:
    Code (CSharp):
    1. public class PlayerController : MonoBehaviour {
    2.  
    3.     public float jumpForce = 3;
    4.     Rigidbody2D myrigidbody;
    5.     public LayerMask groundLayersList;
    6.  
    7.     [SerializeField]
    8.     private bool grounded;
    9.  
    10.     public GameObject myBottom;
    11.     [SerializeField]
    12.     private Collider2D myBottomCollider;
    13.  
    14.  
    15.     // Use this for initialization
    16.     void Start () {
    17.      
    18.     myBottomCollider = myBottom.GetComponent<Collider2D>();
    19.     myrigidbody = GetComponent<Rigidbody2D> ();
    20.      
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update ()
    25.     {
    26.         grounded = Physics2D.IsTouchingLayers(myBottomCollider, groundLayersList);
    27.  
    28.         if (Input.GetKeyDown ("space"))
    29.         {
    30.             Jump ();
    31.         }
    32.  
    33.     }
    34.  
    35.     private void Jump ()
    36.     {
    37.         if (grounded)
    38.         {
    39.         myrigidbody.AddForce (transform.up * jumpForce);
    40.         }
    41.     }
    42.  
    43. }

    Now, the tricky part. When i create an object, and give it:
    layer "Ground"
    adding and enabling 2D box collider

    everything works fine. Player can jump, and gets checked if grounded when colliding. So this part is working fine.

    But when i try to create platforms with sprites its seems like "bottompartofplayer" or "myBottom" never colides with 2D sprites. And im pulling my haird trying to figure out why.

    Screenshot: upload_2017-6-21_19-52-26.png

    And yes, every tile is made form a sprite, but sprites have box colliders AND does have a layer:
    upload_2017-6-21_19-53-18.png

    But the player is sitll not grounded:
    upload_2017-6-21_19-53-52.png

    Thanks for the help in advance :/
     
    Last edited: Jun 21, 2017
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Maybe this will help the issue reveal itself.

    In the Edit>ProjectSettings>Physics2D config, you can turn on the setting to show contact points as a little scene gizmo. That way you know 100% what is detecting physical contact. You may want to check "Always Show Colliders" as well.

     
  3. TheOnlyTruth

    TheOnlyTruth

    Joined:
    Aug 1, 2016
    Posts:
    32
    Hm, what i have done is:
    upload_2017-6-21_20-43-41.png

    Im 100% positive that the collider is touching the platform :/ (and the paltforms box collider)
     

    Attached Files:

  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    the "Bottom" object, it's also on the same layer as the player? I'm assuming it's a child object with a collider?
     
  5. TheOnlyTruth

    TheOnlyTruth

    Joined:
    Aug 1, 2016
    Posts:
    32
    yeah, its a child of the player object. As i said - its fine when coliding with simple cube with 2D box collider... its the sprites that mess it up somehow.

    EDIT: Example. Empty game object with box colider 2D. Layer set to ground. Nothing else - works perfectly, player gets grounded. However, when i set the box colider on sprite, its no longer the case.

    EDIT2: it seems that the problems occurs when i add tag "platforms". The tag is used by script i provided at the top of the thread, it simply finds all platforms and moves them. This seems to cause the problem. I still cant understand why o_O

    upload_2017-6-21_21-46-40.png
     
    Last edited: Jun 21, 2017
  6. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    If the platforms are tagged, but you comment out the part that moves them, do you still have collision issues?

    In any case, I would suggest keeping the environment still and moving the player instead.

    Unfortunately I don't have any other ideas without being able to look at the project in front of me. If you want, you can package your project and send me a link in a private message. I can't promise when I'll have time to go through it, but in the meantime maybe someone else here has some suggestions.
     
  7. TheOnlyTruth

    TheOnlyTruth

    Joined:
    Aug 1, 2016
    Posts:
    32
    commenting out that actually helped. Platforms now spawn, but are no longer moving - the player is able to jump on them.

    //platformToMove.transform.Translate (Vector2.left * Time.deltaTime * platformMovementSpeed);

    This is so... weird? I honestly have no clue why is this happening o_O Do you have any idea whats causing the problem (or rather why moving platforms cause the problem)?

    Also, regarding:
    Why in particular would you suggest that? Because i've read some guides here and there, and some of them actaully suggests moving platforms rather than the player, so that you basicaly always have the player at around 0,0 coords. I have little to no experience when it comes to coding/unity, so im just trusting the internet/people :D If you could explain me the benefits of moving player, i'd be very glad to learn :3
     
  8. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    The reason I would suggest moving the player only is because it's a choice between constantly updating potentially hundreds of objects, versus moving less than ten objects. If the camera is following the player every frame, the effect will be identical. The player being around (0,0) doesn't really have any advantages.

    Every time you want to add some environmental things, you need to make sure that they move at the same rate as the rest of the level. That makes physics act weird (as you have discovered), and makes programming anything else that moves differently more complicated.

    For instance, if you wanted a ball to sit on a platform with physics, it would roll off because the platform is moving underneath it. Or if that platform is a moving platform, then you would need to take into consideration the global movement rate, in addition to the platform's local movement to get it to look and behave right.

    I theorize that your original issue was due to the platform not moving using physics, it essentially 'teleported' each frame a small distance, which messed up the physics simulation because the positions were changing out of sync with the physics update (FixedUpdate).
     
    TheOnlyTruth likes this.
  9. TheOnlyTruth

    TheOnlyTruth

    Joined:
    Aug 1, 2016
    Posts:
    32

    Thanks. I changed Update() to FixedUpdate() and its now working. Wow.

    So i guess i'll have to rethink the system a lil bit and will move the player instead.

    You've been a great help, thank you!
     
    LiterallyJeff likes this.