Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to detect a stairs top in a 2D view?

Discussion in '2D' started by videoanime, Oct 24, 2014.

  1. videoanime

    videoanime

    Joined:
    Sep 28, 2014
    Posts:
    42


    Well, I show you a video where I'm practicing some animation issues, you can see what I did, but let me explain to you my problem.

    I can't detect the end of the stairs, as you can see, if I keep the up key the character goes up and the going up animation persists, what I want to do is that when the character reaches the top, he suddenly stops.

    I marked the "is trigger" on my stairs in order to I could pass through it and I created a variable that tells me when my character is touching the "ground" or not, so, if I touch the stairs and I am touching the "ground", I can go up, the problem is that at the end, the variable that tells me I'm touching the stairs is active and the variable that tells me I'm touching the "ground" is inactive.

    I tried to resolve this with ontriggerenter2D, ontriggerexit2D, oncollisionenter2D or oncollisionexit2D but I can't.

    I was thinking of detecting the height of the object but this seems to me an unpractical method, I don't know, because imagine that there were a lot of objects to detect and to interact, measuring the dimensions to each one would be awkward.

    Can you give me some tips? How this works in 2D games like Metroid, Castlevania and so on?
     
  2. imaginaryhuman

    imaginaryhuman

    Joined:
    Mar 21, 2010
    Posts:
    5,834
    add another set of trigger boxes floating `above` each stair step, which the player much be `inside of` in order to walk up... then omit such boxes at the top?
     
    videoanime likes this.
  3. Rokzero

    Rokzero

    Joined:
    May 19, 2014
    Posts:
    5
    http://higherorderfun.com/blog/2012/05/20/the-guide-to-implementing-2d-platformers/ You can find there some info about stairs too. It`s really simple.
     
    videoanime likes this.
  4. GarBenjamin

    GarBenjamin

    Joined:
    Dec 26, 2013
    Posts:
    7,441
    In the games you listed they were almost certainly using a map of tiles.
    By examining the map cell beneath (or in front) of the player's feet they could easily determine if stairs were there. When on stairs they could either handle it as part of general movement or they could have made it a state (AscendingStairs or DescendingStairs). Personally, I'd enter a new state so only the checks applicable to this specific state need to be performed. If AscendingStairs and player reverses direction state changes to DescendingStairs and vice-versa. And so forth.
     
    videoanime likes this.
  5. videoanime

    videoanime

    Joined:
    Sep 28, 2014
    Posts:
    42
    Ohhhh I hadn't seen that way, thank you guys, I'll try doing something with the ideas you gave to me.