# Fresh pair of eyes

I’ve been working on this problem for quite a while now and I’ve had complete failure at every turn. I finally came up with logic that I swear will work but I have no clue why it doesn’t so I need a fresh pair of eyes to give me a different perspective here’s my situation (And this could be a long one):

I’m working on a Snake game (To get experience with the program) and I’m working on how the body follows the ‘head’. Since I know that the ‘head’ can make multiple turns the body has to essentially mimic the ‘head’. To do this I’ve made an array of turn points that the body uses to store the points where the head turns. To achieve this I’m using MoveTowards as a means for movement. BUT when tested the body doesn’t work correctly. Before I give my code I’d like to tell you that the turnPoint array only gets filled if the length of the snake is greater than 0. (This is part of my logic)

Here’s my code for the Body Controller:

``````enterusing UnityEngine;
using System.Collections;

public class BodyController : MonoBehaviour
{
public int direction;
public Vector3 v3NextPos;
private Vector3 zeroVector;
public bool isLast;
//public bool debug;

PlayerControl playerControl;

// Use this for initialization
void Start()
{
direction = 1;
zeroVector = new Vector3(0,0,0);
playerControl = GameObject.FindWithTag("Player").GetComponent<PlayerControl>();
v3NextPos = playerControl.turnPoint[0];
}

// Update is called once per frame
void Update()
{
//Initilization test condidion. If turnPoint wasn't set.
if(v3NextPos == zeroVector)
{
followPlayer();
if(playerControl.turnPoint[0] != zeroVector) 	//Condition to make sure that if there is
v3NextPos = playerControl.turnPoint[0];		//only one body segment it still gets
//turnPoints.
}
else if(v3NextPos != zeroVector)
{

if(transform.position != v3NextPos) 			//If a turnpoint was set, follow that
{
followTurnPoint();

}
else if(transform.position == v3NextPos)		//When the body reaches the point remove it from the
removePoint();

v3NextPos = zeroVector; 					//Set v3NextPos equal to zeroVector so that it
}
}

}

void followPlayer()
{
transform.position = Vector3.MoveTowards(transform.position, playerControl.transform.position,
Time.deltaTime * playerControl.speed);
}

void followTurnPoint()
{
transform.position = Vector3.MoveTowards(transform.position, v3NextPos,
Time.deltaTime * playerControl.speed);
}

void removePoint()
{
//ignore this for now.
}
}
``````

If you need any further explaining please don’t hesitate to ask, all I’m asking is what could possibly go wrong with this code. I would even take a suggestion on a concept that handles achieving the same thing without doing all this craziness. I can even send you an .exe if you’d like so you can see how the game works with this code.

Thanks for reading if you did,

Sincerely,
At the end of my rope.

#could moderators reject questions with a title that does not summarise the question. this message is for moderators only, cheers