I am making a snake game in Unity. The game is simple, using Texture2D to display the head, tail and body parts.

I am stuck with this problem: I stored 4 textures (each one for one of the four directions) for the head and tail. I check the direction and in onGUI, I select the appropriate image. The snake displays correctly, but the tail image doesn't:

problem illustration

How can I fix this?

  • 2
    \$\begingroup\$ Could you be a bit more specific as to what is not working: what direction do you get, and what direction do you expect. Also, it might help to post the relevant piece(s) of code that selects the picture based on orientation. \$\endgroup\$ – MicroVirus Apr 16 '14 at 11:53
  • \$\begingroup\$ I just thought of one other thing that could be the issue. You're probably keeping your tail oriented as it was originally, but if you are going 1 up and then 1 right, then after the tail has gone one up it still points up, right? But you'd want it to point to the right now, because that's where the rest of the body is. If that's the case, you'll need to pick differently for the tail depending on whether it's on a corner piece of your movement. \$\endgroup\$ – MicroVirus Apr 16 '14 at 11:56
  • \$\begingroup\$ Exactly @MicroVirus Exactly !!! But I am unable to figure it out. As I have tried so many things. I put every direction on list, another made array to store previous directions but the tail is never synchronous. :( \$\endgroup\$ – Adnan Nazir Apr 16 '14 at 11:59
  • \$\begingroup\$ The Snake is a doubly linked list. What you need is a list of positions. Where each position in the list points to the next position. Then the tail checks where is the next position is and compares it with its position and points that way. \$\endgroup\$ – AturSams Apr 17 '14 at 4:11

Without seeing your code, it's hard to give on solid answer, but here's an idea that might help you:

The tail always points away from the previous piece of the snake.

If we see the last piece of the snake as the tail, then the one that comes before it gives the direction of the connecting end of the tail, and the opposite direction is then the direction of the taily end of tail.

Here's a poorly drawn ascii picture to indicate the situation:

o o }         < 0 0 }

The second picture should 'logically' have the tail pointing down as in the first, but because the next piece is to the right of it, the tail should point to the left.

| improve this answer | |

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