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. Dismiss Notice

Platform Effector 2D triggers OnCollisionEnter2D (should it?)

Discussion in 'Physics' started by joaomarcelo9393, Jun 14, 2015.

  1. joaomarcelo9393

    joaomarcelo9393

    Joined:
    Oct 5, 2014
    Posts:
    2
    I am using Edge Collider 2D with Platform Effector 2D. It works fine, the character does not collide with the platform when coming from under it, but that DOES call OnCollisionEnter2D on the character.

    Is it supposed to work like this?
    It would be better if it did not call OnCollisionEnter2D, wouldn't it?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    That's by design. You still may want to know if you touch the colliders or enter triggers no matter what the effector behaviour that may be associated with it is. Don't forget, they're still stock colliders, they just get additional behaviour.

    If you want to somehow distinguish them from other colliders on the same GameObject that are not used by the effector then simply check that property before processing it.
     
  3. joaomarcelo9393

    joaomarcelo9393

    Joined:
    Oct 5, 2014
    Posts:
    2
    Ok, thank you for the info.

    I am making an endless runner game. As the player character is jumping and OnCollisionEnter2D is triggered in its script, I change animation state from Jumping to Walking again.

    But if the player jumps from below a platform that has an Edge Collider 2D with Platform Effector 2D and crosses it, OnCollisionEnter2D is called and the animation changes mid-air.

    As a workaround I added a third condition: if rigidbody2D.velocity.y <= 0, so I know the character is already falling from the jump.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    Actually, looks like I completely missed your point (for some unknown reason), sorry for that.

    So you're talking about not wanting callbacks when the contact is disabled.

    When you get the contact (Collision2D) simply check the 'enabled' property: http://docs.unity3d.com/ScriptReference/Collision2D-enabled.html

    This will be false when the one-way is in-effect i.e. the object is passing through the platform and true under all other circumstances.

    Hope this helps.
     
    Eristen, jd3, d2clon and 2 others like this.
  5. Twenmod

    Twenmod

    Joined:
    Apr 27, 2018
    Posts:
    2
    I also have the same problem i am making a 2d top down shooter and i want to make cover where you can only shoot trough one way but the bullet detects collision and destroys itself (like it hit a normal wall) i tried to fix this by checking if collision2d.enabled is true but it still detected collision here is my script


    private void OnTriggerEnter2D(Collider2D collision)
    {
    if (collision.enabled = true)
    {
    if (collision.tag != "Player")
    {
    Instantiate(Hitwallparticles, transform.position, Quaternion.Euler(90, 0, 0));
    Destroy(gameObject);
    }
    if (collision.tag == "Player")
    {
    Instantiate(Hitplayerparticles, transform.position, Quaternion.Euler(90, 0, 0));
    Destroy(gameObject);
    }
    }
    }
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,507
    You are trying to assign true to the enabled field (assignment operator "=") and not check if it's true (equality operator ("==").
     
  7. d2clon

    d2clon

    Joined:
    Aug 19, 2017
    Posts:
    19
    Code (CSharp):
    1. collision.enabled == true
    Made the trick.. thanks!