Comments and answers for "How to calculate sorting order around diagonal objects"
http://answers.unity.com/questions/1136534/how-to-calculate-sorting-order-around-diagonal-obj.html
The latest comments and answers for the question "How to calculate sorting order around diagonal objects"Comment by EulogyForTheFallen on EulogyForTheFallen's comment
http://answers.unity.com/comments/1597283/view.html
@theredace Thanks for this, it really helped me figure out what to do. That said, parts of your nested if code seem like they aren't necessary and/or might make the function not work in some cases. Here's my code:
float yIntercept = (slope * character.x) + (object.y - (slope * object.x));
if(character.y < yIntercept)
return true; //Character is above object
return false; //Character is below object
You don't need to check the slope or charPosition.x against obstaclePosition.x because those values are already factored into the equation. All you need to do is check the character's position against the yIntercept.
For cases that might not work, what would happen if slope < 0, charPosition.x <= obstaclePosition.x and charPosition.y > y-intercept? Again, thanks for the help, I'm just kinda curious why you coded it the way you didSat, 02 Feb 2019 01:02:43 GMTEulogyForTheFallenComment by theredace on theredace's answer
http://answers.unity.com/comments/1522471/view.html
Yeah, I ended up doing it with math. Basically using the equation of a 2D line: y = mx + c.
"m" is the slope of the line. For a 45-degree angle line, "m" is either 1 or -1 depending on the direction the object is facing. Art is typically not drawn perfectly isometric, it's actually at a lower angle, so to get my equation right for my particular art I had to use 0.57f or -0.57f. If the side of the object you see is the right side, then you use the positive number, or if you see the left side then you use the negative number. I set the direction as an enum on each object, either Left or Right, so I know what to use in the equation when layering with that particular object.
"c" is the Y-intercept along that line. You can flip the equation around to c = y - mx to solve for this. You get c = objectYposition - (m * objectXposition). You can do this for any point along the line, so presumably just the object's position if the pivot point is placed centered somewhere along that line.
"x" is the character's x position.
So now we can solve for y in our original equation:
y = (m * characterXposition) + (objectYposition - (m * objectXposition)).
Again, the value of "m" depends on the direction the object is facing, and the particular value you work out depending on the angle your art is drawn at.
So that gives us the Y-intercept along that line at the character's current position. We can then have some branches to figure out the layering by comparing the character's X position to the obstacle's X position, and comparing the character's Y position to the Y-intercept that we just calculated. We have to know both in order to determine where the character should layer. There are basically 4 possibilities, something like:
if (m < 0)
{
if (charPosition.x <= obstaclePosition.x && charPosition.y < y-intercept)
//set character layered in front of object
else if (charPosition.x > obstaclePosition.x && charPosition.y > y-intercept)
//set character layered behind object
}
else
{
if (charPosition.x >= obstaclePosition.x && charPosition.y < y-intercept
//set character layered behind object
else if (charPosition.x < obstaclePosition.x && charPosition.y > y-intercept
//set character layer in front of object
}
I put a trigger collider on the object and trigger this to code to loop on the character while it's inside that collider (while it's close to the object). So it just keeps resetting as it moves around close to the object. I do have some other stuff going on to get the sorting just right, but this is the general gist of how it works. I know it's a bit confusing, hopefully it makes sense.Tue, 26 Jun 2018 21:02:36 GMTtheredaceAnswer by Asofi
http://answers.unity.com/answers/1522386/view.html
Hey, @theredace, just wanna to ask, have you solved this problem?
I have the same now(Tue, 26 Jun 2018 17:00:25 GMTAsofiComment by theredace on theredace's comment
http://answers.unity.com/comments/1137202/view.html
Alright, it sounds like this isn't really a great option as it requires a lot of manual adjustment for every object, since I do need my character to go behind most things. I'll keep searching.Wed, 03 Feb 2016 13:21:08 GMTtheredaceComment by sn1ckers
http://answers.unity.com/comments/1137127/view.html
I suspect you dont uses a grid system? I think it will be very difficult to sort the sorting out if you don't use one. Check out this tutorial http://bigspritegames.com/isometric-tile-based-game-part-1/ it is for xcode but the principals are the same.Wed, 03 Feb 2016 08:25:05 GMTsn1ckersComment by sn1ckers on sn1ckers's comment
http://answers.unity.com/comments/1137117/view.html
You need to have all you sprites in positive x and y otherwise it will not work. I calculate from my mapsize so the one sprite that is most to the left begins at position.x 0 and the top one starts at position.y. Than the rest will be only positiv values. Also a problem is that you need to define your static object so that you cant enter them, because then your character stands on that object. If that should be possibly you have to work with the pivot on each object because this are what pos.x and pos.y will get from that sprite. It is shown in the video, but this will be hard to achieve the best way is to set that tile that has a object to not accessabel for the character.Wed, 03 Feb 2016 07:40:44 GMTsn1ckersComment by theredace on theredace's comment
http://answers.unity.com/comments/1137084/view.html
I'm still not seeing how this solves my specific problem. I added an image to my original post (and a bit more explanation) to illustrate the problem. I've got sorting set up exactly how they show it in that video you linked to. It works fine except in cases of thin diagonal objects like in my image.
I looked into using POS.x * POS.y but I don't understand how to make that work because those numbers can be either positive or negative and I end up with wildly inaccurate calculations where for example the X is positive but the Y is negativeWed, 03 Feb 2016 05:42:42 GMTtheredaceComment by sn1ckers on sn1ckers's comment
http://answers.unity.com/comments/1136761/view.html
I updated my post, and it also needs to be in the same **sortingLayer** to work becouse sorting order only works in the same layer.Tue, 02 Feb 2016 12:45:55 GMTsn1ckersComment by theredace on theredace's answer
http://answers.unity.com/comments/1136577/view.html
Can you elaborate exactly what you're doing with POS.x and POS.y? I'm not quite following.Mon, 01 Feb 2016 23:29:44 GMTtheredaceAnswer by sn1ckers
http://answers.unity.com/answers/1136573/view.html
I just uses this in the update function `imageRenderer.sortingOrder = POS.x * POS.y * - constant` on moving objects and it works well. And all the other object are sorted with the same formula but not in update if it is static objects. I use the constant if I need a deeper sorting on the character for example, if you have 5 body parts that's also needs to be sorted in that game object I will use 5 as a constant.
It is explained more here 33min in https://www.youtube.com/watch?v=HM17mAmLd7kMon, 01 Feb 2016 23:20:51 GMTsn1ckers