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

Sprite walking behind objects whilst still having collision?

Discussion in '2D' started by TheRoyalSax, Nov 10, 2016.

  1. TheRoyalSax

    TheRoyalSax

    Joined:
    Nov 10, 2016
    Posts:
    2
    I will try my absolute best to describe the issue, because I haven't been able to find a solution. Unfortunately, I am still a bit new with unity, and to be honest, I am not entirely familiar with C# as a programming language either. So, whenever the player walks behind an object, in this case the object is a bed, I want the player to be able to go about knee-high or so below it (half of their legs will be invisible, as they are behind the bed), yet if the walk in front of the bed, they will be able to stand higher than the bed, providing more depth. To be more clear, I want the user to be able to have half of his legs standing behind the bed, making them invisible, and giving the area more depth. Yet they will not be able to walk in too deep. Then, if they attempt to walk up towards it from the bottom, I want the top half of their body to go above the bed, whilst the bottom half stops when it collides with the bed, so that the player can't walk straight over the bed. A normal mid-body collider would not work however. How would I achieve this effect?
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Why would the mid-body collider not work? I imagine that if the bed had a collider along the middle to bottom, and the player had a collider from his feet to his knees, that would work appropriately if you handled the sorting order based on Y position.
     
  3. TheRoyalSax

    TheRoyalSax

    Joined:
    Nov 10, 2016
    Posts:
    2
    Because then he is on a static layer. So he would always be above or below the object, whilst I want him below it when he walks down from above, but above it when he walks from below, upwards.
     
  4. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    That is what I meant by "sorting order based on Y position". Without using 3D space, you need to use sorting order to move a sprite in front or behind another object. So whenever your player's Y position is above the bed, sort the bed in front. When the player's Y position is below the bed, sort the bed behind. The bed's pivot in the center of the bed, the Player's pivot at the bottom-center of the feet.