First Method
This is what I did, but I'll give a more all-catching example at the end. Create an Object called Control, then create an End Step event. Paste this code in that event...
//set the position of Control
with Player{
with other{
x=other.x;
y=other.y;
}
}
//set depth of the player and enemy
with Player{
with pEnemy{
if other.y>y && depth!=0 depth=0;
if other.y<y && depth!=-1 depth=-1;
}
}
I use this with my enemy and my player. Basically it checks the player against and enemy, and the enemy changes it's depth to be above or below the player based on it's y position, and if it's not already that depth, I set it. Notice I only use 2 depth points here because that's all you need if you only have 2 objects using depth.
Second Method
However, if that's not the case, and you want all objects to change depth based on their position, just do this (slightly more taxing but not bad). Create an object called pDepth, place all depth related objects into children (in the parent tab of pDepth).
Now, Create an Object called Control, then create an End Step event. Paste this code in that event...
//set the position of Control
with Player{
with other{
x=other.x;
y=other.y;
}
}
//set depth
with pDepth{
if depth!=round(-y) depth=round(-y);
}
}
After this, you will need to make sure all HUD objects are above the greatest depth. In your HUD object, create an End Step event, and Paste this code
//set depth of the player and enemy
with pDepth{
with Other{
if depth<other.depth depth+=1;
}
}
Some Final Notes
Some would say that you can just set depth=-y in an end step event of each object, but GameMaker Studio 2 doesn't read depth as fast because of the new layers system. This causes your objects to flicker.
What I did to fix this is set every object's depth based on if it wasn't the right depth already. It checks every step, but doesn't set the depth every step, so the amount of change, and performance hit is lessened, and GameMaker Studio 2 has more time to render your character and set it's depth! :)
There was a lot of harder methods, but they would take me restructuring my already made large project, and harder isn't always better, especially in this case, since my method applies to more projects.
Congratulations @dreammix! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
Award for the number of posts published
Click on any badge to view your Board of Honor.
To support your work, I also upvoted your post!
For more information about SteemitBoard, click here
If you no longer want to receive notifications, reply to this comment with the word
STOP
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit