- Home /
How to make snake game using pacman behaviour?
Im trying to make a game Like Snake using PacMan mechanics.
The snake is divided into 3 parts: head, body and tail.. I control the Head and I would like the body to follow the head and the tail to follow the body, but the body and tail are not following the head. They are following different paths. I used the GetBlinkyTargetTile method to make the body always follow the head. As I understand it, Blinky always looks for the Nearest Node through this calculation:
Vector2 vec = targetPosition - (Vector2) previousNode.transform.position;
return vec.sqrMagnitude;
How can I make the body always follow Head's currentNode?
void Move()
{
if (targetNode != currentNode && targetNode != null && head.transform.position != startHeadPosistion)
{
if (OverShotTarget())
{
currentNode = targetNode;
transform.localPosition = currentNode.transform.position;
GameObject otherPortal = GetPortal(currentNode.transform.position);
if (otherPortal != null)
{
transform.localPosition = otherPortal.transform.position;
currentNode = otherPortal.GetComponent<Node>();
}
targetNode = ChooseNextNode();
previousNode = currentNode;
currentNode = null;
}
else
{
transform.localPosition += (Vector3)(direction * speed) * Time.deltaTime;
}
}
}
float LengthFromNode(Vector2 targetPosition)
{
Vector2 vec = targetPosition - (Vector2)previousNode.transform.position;
return vec.sqrMagnitude;
}
bool OverShotTarget()
{
float nodeToTarget = LengthFromNode(targetNode.transform.position);
float nodeToSelf = LengthFromNode(transform.localPosition);
return nodeToSelf > nodeToTarget;
}
Vector2 GetBodyTargetTile()
{
Vector2 headPosition = head.transform.position;
Vector2 targetTile = new Vector2(Mathf.RoundToInt(headPosition.x), Mathf.RoundToInt(headPosition.y));
return targetTile;
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612224729im_/https://answers.unity.com/themes/thub/images/avi.jpg)