3
\$\begingroup\$

I'm newer to Unity so bear with me. I'm using Unity2D and so far I have my character up and walking around, colliding properly with his surroundings, and I've implemented an effective "isometric sorting" script which swaps the order of sprites based on their y coordinate. What I'm trying to do now is create a slope that exists in the middle of the stage or front (bottom of screen). I'll illustrate what I'm trying to do as explaining it is bound to be confusing.

Here is what I'm hoping to achieve.

Right now, I have the ability to move the player up the slope, and in front of the slope (using the isometric sorting script), but it seems maybe impossible to also be able to have the player walk behind the slope.

Here's my idea for how this could be accomplished, but I certainly would need some direction.

enter image description here

Basically, I'm thinking if I can detect that the player has crossed the green line in the image, I could use that information to flip a switch ON which would lower the slopes sorting priority (thus lowering it beneath the players feet), then I would need it to deactivate when the Player exits the slope.

Here is the Isometric Rendering script:

    using UnityEngine;

    [ExecuteInEditMode]
    public class IsometricSpriteRenderer : MonoBehaviour {

    void Update ()
    {
         GetComponent<Renderer>().sortingOrder = (int)(transform.position.y *       -1000);
    }
}

Thank you to anyone who bothers to take the time to help me! I really appreciate it!

\$\endgroup\$
  • 1
    \$\begingroup\$ Why not just add a "z" value to each item in your isometric game? Higher y gets priority in sorting. The same y yields sorting based on the highest z. \$\endgroup\$ – Coburn Jan 14 '16 at 2:33
  • \$\begingroup\$ Thanks for the response, DMGregory. I'm going to see if I can come up with a way to make that work. \$\endgroup\$ – Matthew Jan 14 '16 at 3:47
1
\$\begingroup\$

It looks like a wrong approach. Check this tutorial: https://unity3d.com/learn/tutorials/modules/beginner/2d/sorting-layers

In order to sort your stuff properly and automatically you can use depth (z) and/or sorting layers within objects at the same depth and/or order in layer within objects in the same sorting layer. It's probably reasonable to have objects representing fixed features of the game world (like the slope) use fixed depth/layer/order and have objects representing moveable features of the game world (like the characters) use variable depth/layer/order.

Keep colliders for actual collisions.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.