Hey all,
I’ve been having an issue with trying to get my snake game to function like the classic game of snake would. What I have is a player and a series of GameObjects that are spawned in every few seconds. Once the Player collides with the GameObject, the object is the added to a list of GameObjects called ‘tail’, like:
public List<GameObject> tail = new List<GameObject>();
My issue that I have been trying to solve for weeks is trying to get the list of GameObjects to follow around the tail, in a grid-like manner. Things I have tried are
tail[0].transform.position = Vector3.Lerp(tail[0].transform.position, GameObject.Find("Player").transform.position, Time.deltaTime);
for (int i = 1; i < tail.Count; i++)
{
tail[i].transform.position = Vector3.Lerp(tail[i].transform.position, tail[i - 1].transform.position, Time.deltaTime);
}
But when I do this, the snake’s body follows behind it in a non-grid manner, it just follows the shortest path.
I’ve also tried:
tail[0].transform.position = Vector3.Lerp(tail[0].transform.position, GameObject.Find("Player").transform.position, Time.timeScale);
for (int i = 1; i < tail.Count; i++)
{
tail[i].transform.position = tail[i - 1].transform.position;
}
But when I do it this way, the GameObjects just past on top each other, in a single location not moving.
Any insight into how I could get this working would be much appreciated. I’ve been stuck on this issue for weeks, and don’t think this is something I can solve on my own. I’ve consulted many tutorials on Unity for snake games, but none of them are helpful because they don’t seem to be creating a list of GameObjects for the tail, which is crucial for the functionality of my game.
In case this is needed, here is the code for my Player movement
public float unitsPerFrame = 1;
public float snakeSpeed = 0.2f;
if (Input.GetKey(KeyCode.UpArrow) && downDir == false)
{
transform.localRotation = Quaternion.Euler(0, 0, 0);
mDir = Vector3.up * unitsPerFrame;
upDir = true;
downDir = false;
rightDir = false;
leftDir = false;
}
… All the directions are similar.