Snake movement from Snake VS Block
I am trying to re-create the popular SnakeVSBlock mobile game. Most of the physics are pretty straight forward but I cannot seem to re-create the snake movement. What's special about the snake movement in the game is that the previous node follows the next ones path perfectly, both in X and in Y. This is my current code which does work as a snake movement but does not follow the exact path of the next node. I'm wondering if anyone has a solution to this.
public void Move()
{
if (Input.GetMouseButton(0) && GameManager.Instance.IsGameOver() == false && Nodes.Count > 0)
{
Vector3 curPos = Input.mousePosition;
curPos.z = -1;
curPos = Camera.main.ScreenToWorldPoint(curPos);
Nodes[0].GetComponent<Rigidbody2D>().velocity = new Vector2(curPos.x-Nodes[0].position.x,0);
} else
{
Nodes[0].GetComponent<Rigidbody2D>().velocity = Vector3.zero;
}
if (Nodes[0].position.x > 2.56f)
{
Nodes[0].position = new Vector2(2.56f,Nodes[0].position.y);
}
else if (Nodes[0].position.x < -2.56f)
{
Nodes[0].position = new Vector2(-2.56f, Nodes[0].position.y);
}
for (int i = 1; i < Nodes.Count; i++)
{
Vector2 newPos = new Vector2(Nodes[i-1].position.x, Nodes[i-1].position.y-0.35f);
if (GameManager.Instance.GetPoweredUp())
{
Nodes[i].position = Vector2.Lerp(Nodes[i].position, newPos, Time.deltaTime * 14);
}
else
{
Nodes[i].position = Vector2.Lerp(Nodes[i].position, newPos, Time.deltaTime * 10);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Isometric movement for MoveTowards 0 Answers
Why is my bool being set to true when program is run? 1 Answer
How to have a child object camera with gaze move its parent object 0 Answers
Movement input problem? 1 Answer
Issues with snake game in C# 2 Answers