# Snake game tail

I am working at a 2D Snake Game in Unity. This is the head of the Snake: http://i.imgur.com/RrKr9cg.png And this is the Extension of the tail of the snake. It is added everytime I eat something: http://i.imgur.com/xf9zsqZ.png I made the head of the snake to move, but my question is how do I move the extensions? I tried to add these extensions as childs to the head , but when i rotate the head, it rotates the all extensions at the same time, but I want to rotate one extension at a time like a chain. I am using a C# Script

if (position == 1 || position == 3) {
if (Input.GetKey (KeyCode.UpArrow)) {
position = 2;
rotation=90;
transform.rotation=Quaternion.Euler(0,0,rotation);

} else if (Input.GetKey (KeyCode.DownArrow)) {
position = 4;
rotation=270;
transform.rotation=Quaternion.Euler (0,0,rotation);
}
} else if (position == 2 || position == 4) {
if(Input.GetKey(KeyCode.RightArrow)){
position=1;
rotation=0;
transform.rotation=Quaternion.Euler (0,0,rotation);
}
else if(Input.GetKey(KeyCode.LeftArrow)){
position=3;
rotation=180;
transform.rotation=Quaternion.Euler (0,0,rotation);
}
}
if (position == 1)
transform.Translate (Vector3.right * movementSpeed * Time.deltaTime,Space.World);
else if (position == 2)
transform.Translate (Vector3.up* movementSpeed * Time.deltaTime,Space.World);
else if(position==3)
transform.Translate(Vector3.left*movementSpeed*Time.deltaTime,Space.World);
else if(position==4)
transform.Translate (Vector3.down*movementSpeed*Time.deltaTime,Space.World);
int i;
for (i=1; i<extensions.Count; i++) {
extensions[i].transform.position=extensions[i-1].transform.position;
extensions[i].transform.rotation=extensions[i-1].transform.rotation;
}

• I think it would be best to create some form of path for each piece to follow, as in wherever the head goes, the other parts would follow a step away. That would include rotation and transformation of course. Sep 20, 2015 at 9:56
• Have you seen this question/answer? It covers what you want, even though it's not specific to Unity.
– House
Sep 20, 2015 at 15:08

In unity, you will want to child all the segments (including the head) to an empty GameObject. Then you can move/rotate each child peice, including the head.

The way it works in the classic game is that only the head actually moves. The tail piece is removed from the end and a new segment is added where the head was last time. So only two segments are being moved each frame (head and tail). Not all segments.

Eg.
1. Move the head one space
2. Remove the tail piece
3. Fill in the space where the head was, with a new body/tail segment. Since you are removing the tail segment, you can reuse that GameObject for the new (neck) segment. Unless of course if you are using a unique segment for the tail piece. Then you would need to move 3 segments each frame.

• Yeah, but how do I know when to rotate the specific segment? Sep 20, 2015 at 14:26
• If you do like I described, only the head rotates. The body doesn't actually move. You just remove from the back and add to the front. Sep 20, 2015 at 15:31
• But these sprites are not the 70's squares. My extension is not a square, is a rectangle and if I only remove it and add it to the front it will have the same rotation and it will look different if my head is going up for example Sep 20, 2015 at 16:10
• @Bogdan15 Move the piece as described in the answer. Since you're only moving one segment, and it's always where the head was, use the rotation of the head from the last update. Alternatively, rotate it to point towards the head.
– House
Sep 20, 2015 at 17:25
• @Bogdan15 I think I understand what you mean but it's unclear how you want your snake to bend. IE. Does it turn 90 degrees each time or any angle? Please post a picture of what you want your snake to look like during the game with some bends. Either way, you can still do the actual movement like I described. How to rotate it so your snake doesn't look like it has a broken back is a different problem. Sep 20, 2015 at 18:01