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

EDIT: The problem seems to occur when i add object tag “Platform” and the only thing that uses Platform tag is this:
Code that uses “Platforms” tag

public class PlatfromsMotor : MonoBehaviour {

    public float platformMovementSpeed = 0.5f;
    public GameObject[] activePlatforms;
    public static int activePlatformsInt;

    void Update ()
    {
        activePlatforms = GameObject.FindGameObjectsWithTag("Platform"); //TODO: Move this so that it does not update every frame;

        for (int i = 0; i < activePlatforms.Length; i++)
            {
            GameObject platformToMove = activePlatforms [i];
            platformToMove.transform.Translate (Vector2.left * Time.deltaTime * platformMovementSpeed);
            }
        activePlatformsInt = activePlatforms.Length;
    }


}

Hello :slight_smile: 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:
Player Jumping Script

public class PlayerController : MonoBehaviour {

    public float jumpForce = 3;
    Rigidbody2D myrigidbody;
    public LayerMask groundLayersList;

    [SerializeField]
    private bool grounded;

    public GameObject myBottom;
    [SerializeField]
    private Collider2D myBottomCollider;


    // Use this for initialization
    void Start () {
     
    myBottomCollider = myBottom.GetComponent<Collider2D>();
    myrigidbody = GetComponent<Rigidbody2D> ();
     
    }
 
    // Update is called once per frame
    void Update ()
    {
        grounded = Physics2D.IsTouchingLayers(myBottomCollider, groundLayersList);

        if (Input.GetKeyDown ("space"))
        {
            Jump ();
        }

    }

    private void Jump ()
    {
        if (grounded)
        {
        myrigidbody.AddForce (transform.up * jumpForce);
        }
    }

}

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:

And yes, every tile is made form a sprite, but sprites have box colliders AND does have a layer:

But the player is sitll not grounded:

Thanks for the help in advance :confused:

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.

Hm, what i have done is:

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

the “Bottom” object, it’s also on the same layer as the player? I’m assuming it’s a child object with a collider?

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

3116658--235755--upload_2017-6-21_21-46-40.png

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.

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 :smile: If you could explain me the benefits of moving player, i’d be very glad to learn :3

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).

1 Like

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!

1 Like