8
\$\begingroup\$

I'm using Unity to create a 2D top-down game. Everything has been great so far, but I have come into a problem that I fail to solve properly.

I want to implement "tall grass areas" where part of the character will be occluded.

A visual example:

I though about making the tall grass by layers, but that would create a LOT of game objects, and the transition as the character moves up won't be smooth enough.

Any idea about how could I approach this? I thought that maybe using shaders I could achieve something like this, but my knowlegde on shaders is very very limited, so I don't even know if such thing is possible.

\$\endgroup\$
5
  • 6
    \$\begingroup\$ Sometimes you need to work with a LOT of game objects, you know. \$\endgroup\$
    – user15805
    Jan 21, 2015 at 12:02
  • \$\begingroup\$ A kind of hack to avoid more game objects would be to replace the player sprite with a custom "isInGrass" version of the sprite (very crude example here) while it's in the grass and just draw it on top. With some nice art and a bit of transparency at the bottom (to help it blend in with the actual grass) it might look good enough. \$\endgroup\$
    – Christian
    Jan 21, 2015 at 12:46
  • \$\begingroup\$ I thought about that approach but, besides of all the work that would require (we'd need twice as animations for each actor at least), the transition from outside the grass to inside from the lateral scenario (in the example image: Top row, third column), when half the body is inside, and the other half is outside, the result would be pretty messy. \$\endgroup\$
    – Leo
    Jan 21, 2015 at 13:43
  • 2
    \$\begingroup\$ Sounds like a mask or stencil is what you need. This thread contains some useful info. This extension demonstrates what I am talking about, though I don't know if it will do exactly what you need. \$\endgroup\$
    – user39686
    Jan 21, 2015 at 21:48
  • 1
    \$\begingroup\$ This sounds pretty much like I was looking for! I'll have to do some research and experiment with it. If I manage to have something working I'll post it as an answer. \$\endgroup\$
    – Leo
    Jan 22, 2015 at 15:43

3 Answers 3

1
\$\begingroup\$

I agree with @Christian (though I wouldn't call it a hack). Detect the condition when the object should look different and then just draw it different. Every single thing in the game doesn't have to be a "model" of how it works in "nature". That's a mistake I've made many times and so it's become something of a mantra for me. Ask yourself this: what would you gain by having the bush occlude the player "realistically" instead of just drawing the player the way they should look? I put this in quotes, because in trying to imagine what this would look like I think the layered occlusion approach may actually look inferior.

If your concern with @Christian's approach is in managing the "potentially unbounded" list of little things you have to check each frame, this is another mistake I've made many times. I'd recommend you "just go with it" for now and periodically check how long the list has actually gotten--and whether it's actually become a problem. One of two things will happen. Either it hasn't become a problem, in which case--why are you trying to solve it? Or it has and you need to engineer a (minimal) solution to solve it. Don't try to solve it by simulating it.

\$\endgroup\$
0
\$\begingroup\$

I finally found a satisfying solution! @Thebluefish comment pointed me to the right direction with masks.

The idea is to use a shader that draws the character in two passes:

  • In the first pass the character is rendered normally, BUT being completely behind the bushes.
  • In the second pass I draw it again, using a mask that olny draws the top part of the character, on top of the bushes.

This is done even when the chatacter is not inside the bushes. This way I don't have to detect collisions agains the bushes, and also it makes the transition inside the bush smooth on limit cases like this one:

The half of the character that is outside the bush is rendered in the first pass. The other half will be completely occluded by the bush.

In the second pass the masked character will be rendered again. The part outside the bush will remain the same, but the part inside the bush will be rendered on top with the mask on top of the bush, making it look like its inside the bush.

\$\endgroup\$
-1
\$\begingroup\$

You can detect wether or not character is in the grass. Then draw only top-half of character.

\$\endgroup\$
1
  • \$\begingroup\$ I believe this answer was already suggested. Do you have additional details to add to your answer? \$\endgroup\$
    – House
    Feb 19, 2015 at 1:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .