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

Landing on ground only (2D)

Discussion in 'Scripting' started by Numberslayer, Jun 15, 2014.

  1. Numberslayer

    Numberslayer

    Joined:
    Jun 14, 2014
    Posts:
    7
    Is there some way to check collisions between two specific 2D box colliders? I have a character made of multiple box colliders. One of the colliders is a small rectangle at the character's feet that is set as a trigger. I want my character to be in 1 of 2 states. Either a falling state or a grounded state.

    Problem is I'm using OnCollisionEnter2D() to switch from falling to grounded. It woks fine when I just jump up and down but if my character hits his head on a platform or if he touches the side of a wall while in the air then he switches from falling to grounded. I only want him to go to the grounded state when his feet touch the ground (When the small box collider in touches a ground collider). Is there some way to id different box colliders and check for them perhaps?
     
  2. ElvisAlistar

    ElvisAlistar

    Unity Technologies

    Joined:
    Oct 2, 2013
    Posts:
    226
  3. Numberslayer

    Numberslayer

    Joined:
    Jun 14, 2014
    Posts:
    7
    Oh that sounds promising. I will try that thanks. How exactly do you put a collider on different layers? My "player" gameobject has multiple colliders for multiple purposes. Can I even sort them in layers if they are within the same gameobject?
     
  4. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    Now, what i would suggest you to do is, draw a little circle at your feet, and whenever your feet touches the ground, that little circle will touch it as well, so that way you can easier call if im touching the ground or not.

    Code (csharp):
    1.  
    2. // This is the circle, add an empty game object and put it at your characters feet.
    3.     public Transform groundCheck;
    4.  
    5. // Your LayerMask, just to say what is the ground. First you have to add a layer to an object, that will be your ground, and set that layer to be you whatIsGround layer.
    6.     public LayerMask whatIsGround;
    7.  
    8. // We are checking wether we are grounded or not.
    9.     bool grounded = false;
    10.  
    11. // Its basicly a radius of the circle. I would suggest you to use about 0.2.
    12.     float groundRadius = 0.2f;
    13.  
    14.  
    15. Fixed Update()
    16. {
    17.  
    18.         grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
    19.  
    20. if(grounded)
    21. {
    22. //Do your stuff here:)
    23. }
    24. }

    Hope it helped! :)
     
  5. Numberslayer

    Numberslayer

    Joined:
    Jun 14, 2014
    Posts:
    7
    I actually already had a circle on my character's feet. So that's a good call. I didn't know you can distinguish between boxes and circles. I will try this out thanks.
     
  6. Numberslayer

    Numberslayer

    Joined:
    Jun 14, 2014
    Posts:
    7
    Omg it worked. Thank you so much guys. I have been trying to figure this out for so long.
     
  7. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297

    Glad it helped! :))