S
S
shadowknight10202022-04-14 22:53:18
2D
shadowknight1020, 2022-04-14 22:53:18

How to make the player hide behind an object in a 2d game on Unity?

I'm making a 2d game on Unity and I got this problem - I have a character and for example a tree, when I'm in front of the tree, the player's sprite is "on the tree", but when I go behind it, the player's sprite is again "on the tree".
62587b9531072108635585.png
62587b9d166b8517941673.png
What can be done to fix this?
I will be glad to help!

Answer the question

In order to leave comments, you need to log in

5 answer(s)
N
Nekit Medvedev, 2022-04-15
@NIKROTOS

Move deep?

D
Dima Magas, 2022-04-15
@White_Night

An interesting task, I haven’t solved this myself yet, but I can advise you to add a collider to the tree, check the OnTrigger checkbox and check the Y-axis of the player in the OnTriggerStay2D method to know who is higher, and then just change the Depth of the tree

K
Kerim Rezai Shirazi, 2022-04-16
@judge971

try through layers

D
Default_Nickname, 2022-04-16
@Default_Nickname

you need to put a trigger on the tree, a collider on the character. When the character touches the trigger, then move it (the character) along the z axis back.

G
Gregory256, 2022-04-17
@Gregory256

Sprite Mask use, move forward one layer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question