So basically my question is, how do i make it constantly move foreward or sideways, ive tried for loops if statements within if statements, and cannot understand why it still is only moving one peice at a time.
i have this script on the PLAYER with no other dependancy
and i have another script on the food prefab with the positions of them for collisions.
i am trying to use as little unity to develope this.
///script start
var cube : GameObject;//bpdy of the snake
var keyPressUp = false;
var keyPressDown = false;
var keyPressRight = false;
var keyPressLeft = false;
var tailLength : int;// later on
var food : GameObject;
var foodpos : foodpos;
static var charx : float;
static var chary : float;
function Start(){
//create the food random location within the grid system
Instantiate(food, Vector3(Random.Range(-12 / 1, 12 / 1), Random.Range(-8 / 1, 10 / 1)), Quaternion.identity);
}
function FixedUpdate()
{
//boundary for game movement
transform.position.x = Mathf.Clamp(transform.position.x, -13, 13);
transform.position.y = Mathf.Clamp(transform.position.y, -9, 11);
}
function Update()
{
//mark characters x y coordinates for collision
charx = transform.position.x;
chary = transform.position.y;
//if player hits position of the food x and y
if(transform.position == Vector3(foodpos.foodx,foodpos.foody,0))
{
Destroy(GameObject.FindWithTag("food"));//destroy the food
Instantiate(food, Vector3(Random.Range(-12 / 1, 12 / 1), Random.Range(-8 / 1, 10 / 1)), Quaternion.identity);//create the food
//check what direction player is going to place tail on correct side
if(keyPressLeft == true)
{
Instantiate(cube, Vector3(transform.position.x +1,transform.position.y,0), Quaternion.identity);
}
if(keyPressRight == true)
{
Instantiate(cube, Vector3(transform.position.x - 1,transform.position.y,0), Quaternion.identity);
}
if(keyPressUp == true)
{
Instantiate(cube, Vector3(transform.position.x,transform.position.y - 1,0), Quaternion.identity);
}
if(keyPressDown == true)
{
Instantiate(cube, Vector3(transform.position.x,transform.position.y +1,0), Quaternion.identity);
}
}
//If Button W is down and D A S are not then move
if (Input.GetKeyDown("w") && keyPressRight == false && keyPressLeft == false && keyPressDown == false )
{
//mark last positions
lastPosy = transform.position.y;
lastPosx = transform.position.x;
print (" lastposy : " + lastPosy);
//move one position up
transform.position.y += 1;
keyPressUp = true;
GridCheck();
}
//If Button S is down and D A W are not then move
if (Input.GetKeyDown("s") && keyPressRight == false && keyPressLeft == false && keyPressUp == false)
{
//mark last positions
lastPosy = transform.position.y;
lastPosx = transform.position.x;
print (" lastposy : " + lastPosy);
//move one position down
transform.position.y += -1;
keyPressDown = true;
GridCheck();
}
//if Button A is down and S D W are not then move
if (Input.GetKeyDown("a") && keyPressDown == false && keyPressRight == false && keyPressUp == false)
{
//mark last positions
lastPosy = transform.position.y;
lastPosx = transform.position.x;
print (" lastposx : " + lastPosx);
transform.position.x += -1;
//move one position left
keyPressLeft = true;
GridCheck();
}
//If Button D is down and A S W are not then move
if (Input.GetKeyDown("d") && keyPressLeft == false && keyPressDown == false && keyPressUp == false)
{
//mark last positions
lastPosy = transform.position.y;
lastPosx = transform.position.x;
print (" lastposx : " + lastPosx);
//move one position right
transform.position.x += 1;
keyPressRight = true;
GridCheck();
}
//Release button check
if (Input.GetKeyUp("w"))
{
keyPressUp = false;
}
if (Input.GetKeyUp("s"))
{
keyPressDown = false;
}
if (Input.GetKeyUp("a"))
{
keyPressLeft = false;
}
if (Input.GetKeyUp("d"))
{
keyPressRight = false;
}
}
//if player hits boundary transform to opposite side
function GridCheck()
{
//if hit boundary x go to opposite side of grid
if(transform.position.x == -13 && keyPressLeft == true)
{
transform.position.x = 13;
}
if(transform.position.x == 13 && keyPressRight == true)
{
transform.position.x = -13;
}
//if hit boundary y go to opposite side of grid
if(transform.position.y == -9 && keyPressDown == true)
{
transform.position.y = 11;
}
if(transform.position.y == 11 && keyPressUp == true)
{
transform.position.y = -9;
}
}
///script end