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

Issue with Physics2D.IgnoreCollision

Discussion in 'Physics' started by AdamCNorton, Dec 10, 2014.

  1. AdamCNorton

    AdamCNorton

    Joined:
    Apr 16, 2013
    Posts:
    57
    I'm trying to make some simple platforms. You jump through them from the bottom, but then land on top. I have read several threads on this, but none seem to have the answer. To me, the simplest way is to check my characters "ground check" at this feet against the height of the platform. If his feet are lower, collisions are ignored. If his feet are equal to or higher, than they are not ignored. However, this code is not working.

    The way it is written right now, collisions are never ignored, even though my Debug text does show it's going to that part of the statement. If fact, the Debug text toggles between true and false just as expected, which confuses me even more why this is not working. If I comment out the else/if, then collisions are suddenly always ignored. Does anyone see why? I've been pulling my hair out over this one. Not sure what else to do.

    This script is on my Player object. Then I have another script on my platform that simply assigns the platformColliders[] variables when the player hits a trigger I have surrounding the platform.

    Any assistance would be appreciated.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerOneWayCollision : MonoBehaviour {
    5.  
    6.     public GameObject player;
    7.     public GameObject groundCheck;
    8.     public GameObject platform;
    9.     public PlayerLoco playerLoco;
    10.     public PlatformOneWay platformOneWay;
    11.     public Collider2D cirCollider;
    12.     public Collider2D boxCollider;
    13.  
    14.     void Awake()
    15.     {
    16.         player = gameObject;
    17.         playerLoco = gameObject.GetComponent<PlayerLoco>();
    18.     }
    19.  
    20.     void FixedUpdate ()
    21.     {
    22.         if(platform != null)
    23.         {
    24.             if(groundCheck.transform.position.y < platform.transform.position.y)
    25.             {
    26.                 Physics2D.IgnoreCollision(platformOneWay.platformColliders[0], boxCollider, true);
    27.                 Physics2D.IgnoreCollision(platformOneWay.platformColliders[1], boxCollider, true);
    28.                 Physics2D.IgnoreCollision(platformOneWay.platformColliders[0], cirCollider, true);
    29.                 Physics2D.IgnoreCollision(platformOneWay.platformColliders[1], cirCollider, true);
    30.                 Debug.Log ("Ignoring: TRUE");
    31.             }
    32.             else if(groundCheck.transform.position.y >= platform.transform.position.y)
    33.             {
    34.                 Physics2D.IgnoreCollision(platformOneWay.platformColliders[0], boxCollider, false);
    35.                 Physics2D.IgnoreCollision(platformOneWay.platformColliders[1], boxCollider, false);
    36.                 Physics2D.IgnoreCollision(platformOneWay.platformColliders[0], cirCollider, false);
    37.                 Physics2D.IgnoreCollision(platformOneWay.platformColliders[1], cirCollider, false);
    38.                 Debug.Log("Ignoring: FALSE");
    39.             }
    40.         }
    41.     }
    42.  
    43.     public void OnTriggerEnter2D(Collider2D other)
    44.     {
    45.         if(other.tag == "Platform")
    46.         {
    47.             platform = other.gameObject;
    48.             platformOneWay = platform.GetComponent<PlatformOneWay>();
    49.             Debug.Log ("You've Entered Platform");
    50.         }
    51.     }
    52. }
    53.  
     
  2. bittarello

    bittarello

    Joined:
    Aug 8, 2013
    Posts:
    8
    Hello,

    I know that this thread is old but I was stuck in this same problem yesterday. I've tried the same approach and sometimes it worked.

    This problem brought me here, and for future devs that will have the same problem, I'm posting my solution for Unity 5.2. There is a component named "Platform Effect 2D". You add it with a 2D collider with "Used By Effector" enabled.

    That's it! You can set the mobile rigidbody objects to "Continuos" and put kinematic ridigbodys on platforms to ensure that physics will work always.

    I hope it will help somebody.

    Font:
    http://docs.unity3d.com/Manual/class-PlatformEffector2D.html