I am having trouble programming the game of snake on unity, my problem and move the body parts of the snake with its head in the game when I move my head the rest of the snake moves from behind the snake and not as it should, someone there has an example or any idea how I can make the body of the snake roam back of the head of the snake? I’m waiting if someone can help thank you.
If the snake body parts are connected via parent-child game object relationships, that probably won’t work very well. For a snake game, the body parts generally need to be ‘autonomous’, and to follow the same path followed by the head of the snake, but delayed.
Try searching the forums here for ‘snake’; I know there was a thread on this subject recently, and I imagine there are other previous threads as well.
If you’re not sure how to get started, a common approach for a ‘basic’ snake game is to represent the game area with a grid, where each cell can contain a wall, a ‘food item’, or a part of the snake (with the direction of motion also specified).
So I built an array, I got to play the part of the head goes, I’m not getting, not so much in logic, is how do I do for these other parts follow the snake’s head, you could not send me your e-mail, then I send my project to give you a look, I would thank you very much.
What I can not understand is where I put the coding of the other parties for them to be able to communicate with the head, being in different scripts and different parts. The logic I know I have to take the head position -1 and remove the line when the head is to the right and +1 if it is left and so on, which I’m not getting is where I put this logic to it works.
I’m sorry if it’s a simple doubt, is that I never programmed games, only programmed web. and I am not able to adapt to this way of programming.
If anyone can help me I’m forever indebted to that person.
Thank you for understanding all.
You can do it like this:
*Make all the movement and collecting work on head script
*For other body parts instantiate a different prefab
*Keep a transform variable in each part that points to part next to it
*First body part will point to head and so on
*Keep an old position variable for each part and on every cycle grab the old position of next part and make it own position.
I’ll put my script here for you to look.
In this code I create an array of cubes all inactive.
public class Colidir : MonoBehaviour
{
public static List<Vetor1> listaVetor = new List<Vetor1>();
public static int i;
public static int j;
public static bool direita = false;
public static bool esquerda = false;
public static bool cima = false;
public static bool baixo = false;
public static int posI;
public static int posJ;
public static bool colidiu = false;
public static int Ci;
public static int Cj;
public void Start()
{
CriarMatrixCobra();
i = 0;
j = 0;
Comessar();
Comida.CriarComida();
}
//it appears to head in the first position.
public void Comessar()
{
listaVetor[i].listaVetor2[j].listaGame[0].active = true;
listaVetor[i].listaVetor2[j].listaGame[0].name = "Cobra";
listaVetor[i].listaVetor2[j].listaGame[0].AddComponent("CabecaCobra");
listaVetor[i].listaVetor2[j].listaGame[0].AddComponent<Rigidbody>();
listaVetor[i].listaVetor2[j].listaGame[0].rigidbody.useGravity = false;
}
//I try to verify that the charges passed through the food /position. This is not working.
public void Update()
{
if (Vetor1.Vetor2.vi == Comida.Ci)
{
if (Vetor1.Vetor2.vj == Comida.Cj)
{
Comida.CriarComida();
Comida.DesligarComida();
}
}
}
//Create an array and fill each position with a GameObject.CreatePrimitive (PrimitiveType.Cube).
List<Vetor1> CriarMatrixCobra()
{
Vector3 vector = new Vector3();
Vetor1 vetor1 = new Vetor1();
Vetor1.Vetor2 vetor2 = new Vetor1.Vetor2();
List<Vetor1.Vetor2> listaVetor2 = new List<Vetor1.Vetor2>();
vector.x = -1.5f;
vector.y = 0;
vector.z = 0;
int v1 = 0;
for (int i = 0; i < 25; i++)
{
vetor1 = new Vetor1();
listaVetor2 = new List<Vetor1.Vetor2>();
int v2 = 0;
vector.x = -1.5f;
for (int j = 0; j < 25; j++)
{
vetor2 = new Vetor1.Vetor2();
if (vector.x <= 36f)
{
vector.x = vector.x + 1.5f;
}
GameObject CorpoCobra = GameObject.CreatePrimitive(PrimitiveType.Cube);
CorpoCobra.active = false;
CorpoCobra.name = "Xuleta";
CorpoCobra.transform.position = vector;
vetor2.listaGame.Add(CorpoCobra);
listaVetor2.Insert(v2++, vetor2);
}
if (vector.z >= -27.7f)
{
vector.z = vector.z - 1.5f;
}
vetor1.listaVetor2 = listaVetor2;
listaVetor.Insert(v1++, vetor1);
}
return listaVetor;
}
}
//Create an array
public class Vetor1
{
public List<Vetor2> listaVetor2 = new List<Vetor2>();
public class Vetor2
{
public List<GameObject> listaGame = new List<GameObject>();
public static int vi;
public static int vj;
}
}
Here I deal with the movements of the head of the snake. this code works as usual.
public class CabecaCobra : MonoBehaviour
{
public static int i;
public static int j;
public static bool direita = false;
public static bool esquerda = false;
public static bool cima = false;
public static bool baixo = false;
static float nextFire = 0.0f;
static float fireRate = 0.3f;
//the two methods below, I'm playing away and see the object in the array.
public void CriarCobra()
{
Colidir.listaVetor[i].listaVetor2[j].listaGame[0].active = true;
Colidir.listaVetor[i].listaVetor2[j].listaGame[0].name = "Cobra";
Colidir.listaVetor[i].listaVetor2[j].listaGame[0].AddComponent("CabecaCobra");
}
public void Desligar()
{
Colidir.listaVetor[i].listaVetor2[j].listaGame[0].active = false;
Colidir.listaVetor[i].listaVetor2[j].listaGame[0].name = "Xuleta";
Colidir.posI = i;
Colidir.posJ = j;
}
public void Update()
{
VerificarTeclaPressionada();
MovimentaCobra();
Vetor1.Vetor2.vi = i;
Vetor1.Vetor2.vj = j;
}
//Check which key was pressed to move the head of the snake
public void VerificarTeclaPressionada()
{
if (Input.GetKeyDown(KeyCode.RightArrow))
{
direita = true;
esquerda = false;
cima = false;
baixo = false;
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
esquerda = true;
direita = false;
cima = false;
baixo = false;
} if (Input.GetKeyDown(KeyCode.DownArrow))
{
baixo = true;
direita = false;
cima = false;
esquerda = false;
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
cima = true;
baixo = false;
esquerda = false;
direita = false;
}
}
//I make the head move to all positions.
public void MovimentaCobra()
{
if (direita == true)
{
if (Time.time > nextFire)
{
Desligar();
j++;
CriarCobra();
nextFire = Time.time + fireRate;
}
}
if (esquerda == true)
{
if (Time.time > nextFire)
{
Desligar();
j--;
CriarCobra();
nextFire = Time.time + fireRate;
}
}
if (cima == true)
{
if (Time.time > nextFire)
{
Desligar();
i--;
CriarCobra();
nextFire = Time.time + fireRate;
}
}
if (baixo == true)
{
if (Time.time > nextFire)
{
Desligar();
i++;
CriarCobra();
nextFire = Time.time + fireRate;
}
}
}
}
these are the two classes that I have, now the problems, I’m not able to make the food disappear and appear somewhere else when the head of the snake passes through it.
and I’m not able to make the other parts of the body below the head in the game as the snake does.
if anyone can help, or say I’m completely wrong and that way ever going to get this game, and I have to start from the beginning again.
Comments are written in english but the code is…eh ? ![]()
By the way, what does your snake look like ? Lot of small boxes / spheres ? Or is it a solid line ?
hehehe, I am Brazilian, the code is in Portuguese but I have tried from the comments give an idea of what the codes do.
are lots of small boxes, the first of which controls the other in a row.
![]()