Search Unity

Forcing a character to fall through a platform

Discussion in '2D' started by nunesbarbosa, Jan 11, 2014.

  1. nunesbarbosa

    nunesbarbosa

    Joined:
    Jul 17, 2012
    Posts:
    103
    Hi. I'm using Unity 4.3 with Physics 2D and I want to make the character fall down a platform when I press the down key, something like most platform games when you press down and jump.

    My character have two colliders (circle and box) just like the Unity platform demo I can't figure how to make him fall.. I already tried the following without success:

    1. Enabling the platform collider IsTrigger but everything falls along with the player;
    2. Enabling IsTrigger on the character collider: I can't because I can only access one of the colliders;
    3. Force moving the position.y of the character: It messes with the gravity Physics. It's a dirty solution;
    4. Moving the character to a different position on the Z axis don't work either because it makes no difference to Unity 2D engine.

    I searched a lot on the forums, but I can't find a simple solution and this is driving me crazy! Can anyone help me?
     
  2. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    You could temporarily disable the collider on either the player or the platform, making sure yougivethe player enough time to fall.
     
  3. nunesbarbosa

    nunesbarbosa

    Joined:
    Jul 17, 2012
    Posts:
    103
    That's attempt #1 and #2.

    If I disable the collider on the platform everything falls along with the player like nearby enemies (which also uses Physics2D).

    I can disable only one collider on the player because I have two colliders (circle and box) and Unity doesn't give me an option to select an array of colliders from MonoBehavior. :(
     
  4. msbranin

    msbranin

    Joined:
    Jan 19, 2009
    Posts:
    104
    Could you put the 2 player colliders in an empty game object attached to the player character then just disable the parent obeject of the colliders temporarily?
     
  5. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    How to access both colliders on one object:

    Code (csharp):
    1.  
    2. BoxCollider2D boxCol = GetComponent<BoxCollider2D>();
    3. CircleCollider2D cirCol = GetComponent<CircleCollider2D)();
    4.  
    Once you have a reference to both you can disable them both.
     
  6. nunesbarbosa

    nunesbarbosa

    Joined:
    Jul 17, 2012
    Posts:
    103
    Yes, that worked! I was treating colliders as monobehavior attributes insted of script components.. If anyone stumble at this post, here is my solution:

    Code (csharp):
    1.     void Start () {
    2.         boxCollider = GetComponent<BoxCollider2D>();
    3.         circleCollider = GetComponent<CircleCollider2D>();
    4.     }
    5.  
    6.     void Update() {
    7.         down = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Hole"));
    8.  
    9.         // Disables the player collider temporarily
    10.         if(Input.GetKey("s")  down) {
    11.             StartCoroutine("Fall");
    12.         }
    13.     }
    14.  
    15.     // Makes the character fall by disabling then enabling the collider
    16.     private IEnumerator Fall() {
    17.         boxCollider.isTrigger = true;
    18.         circleCollider.isTrigger = true;
    19.         yield return new WaitForSeconds(0.2f);
    20.         boxCollider.isTrigger = false;
    21.         circleCollider.isTrigger = false;
    22.     }
     
  7. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    Is it 2d? If it is, use plane you can go threw him like invisible wall.
     
  8. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    Thanks for posting your success and how you ended up using it. This is something that many people can use in their own projects.
     
  9. dilmerval

    dilmerval

    Joined:
    Jun 15, 2013
    Posts:
    232
    Thanks for posting it, I'm working in a game right now and will more likely use your method :), could you explain to me why the need of this line?

    Code (csharp):
    1.  
    2.  
    3. down = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Hole"));
    4.  
    5.  
     
  10. dilmerval

    dilmerval

    Joined:
    Jun 15, 2013
    Posts:
    232
  11. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    It's chexking to see that his player is standing on an object that is on the hole layer. He has probably has a gameobject attached to his player that is directly below him that acts as a "ground checker".
     
  12. dilmerval

    dilmerval

    Joined:
    Jun 15, 2013
    Posts:
    232
    Awesome @nunesbarbosa just added it to my game looks and works great thanks for sharing...
     
  13. dilmerval

    dilmerval

    Joined:
    Jun 15, 2013
    Posts:
    232
    @nunesbarbosa in case you need it, I came up with another script to be able to jump through platforms :)

    Code (csharp):
    1.  
    2. Physics2D.IgnoreLayerCollision(LayerMask.NameToLayer("Player"),
    3.                                        LayerMask.NameToLayer("AirFloor"),
    4.                                        !groundHit || rigidbody2D.velocity.y > 0
    5.                                        );
    6.  
    7.  
     
    aminovsky likes this.