Hi, I’m making snake and i have an problem with part conection. Each part has an script with own smooth grid movement:
using UnityEngine;
using System.Collections;
public enum Direction { Right, Up, Left, Down}
public class Part : MonoBehaviour
{
public const float speed = 0.5f;
public Direction direction = Direction.Right;
public Vector3 endpos; //target coordinates
public bool isMoving = false; //serves to indicate if the part is on the center of cell
public bool changedDirection = false; //serve to indicate if direction is changed
public int partRank; //register order of the part
public Vector3 ChangingPosition; //this is a position where parts should change their direction
void Update()
{
if (isMoving == false)
{
endpos = transform.position;
if (direction == Direction.Right)
{
endpos += Vector3.right;
}
else if (direction == Direction.Up)
{
endpos += Vector3.up;
}
else if (direction == Direction.Left)
{
endpos += Vector3.left;
}
else if (direction == Direction.Down)
{
endpos += Vector3.down;
}
isMoving = true;
}
if (isMoving == true)
{
transform.position = Vector3.MoveTowards(transform.position, endpos, speed * Time.deltaTime);
if (transform.position == endpos)
{
isMoving = false;
if (changedDirection == true && transform.position == ChangingPosition)
{
GameObject g = gameObject.GetComponentInParent<Snake>().snakeParts[partRank] as GameObject;
//g.GetComponent<Part>().direction = direction;
StartCoroutine("setDirection", g);
g.GetComponent<Part>().changedDirection = true;
g.GetComponent<Part>().ChangingPosition = ChangingPosition;
changedDirection = false;
}
}
}
}
IEnumerator setDirection(GameObject g)
{
yield return new WaitForSeconds(Time.deltaTime);
g.GetComponent<Part>().direction = direction;
}
}
Parts transmit informations about changing their direction. It works, but if i change direction twice consecutively it doesn’t work.
To all parts is superior one object, which changes direction with following script:
using UnityEngine;
using System.Collections;
public class Snake : MonoBehaviour
{
public GameObject hlava;
public GameObject clanek;
public GameObject ocas;
public ArrayList snakeParts = new ArrayList();
void Start()
{
GameObject head = Instantiate(this.hlava, new Vector3(0, 0, 0), new Quaternion(0, 0, -90, 90)) as GameObject;
this.snakeParts.Add(head);
head.GetComponent<Part>().partRank = 1;
head.transform.parent = transform;
GameObject part1 = Instantiate(this.clanek, new Vector3(-1, 0, 0), new Quaternion(0, 0, -90, 90)) as GameObject;
this.snakeParts.Add(part1);
part1.GetComponent<Part>().partRank = 2;
part1.transform.parent = transform;
GameObject part2 = Instantiate(this.clanek, new Vector3(-2, 0, 0), new Quaternion(0, 0, -90, 90)) as GameObject;
this.snakeParts.Add(part2);
part2.GetComponent<Part>().partRank = 3;
part2.transform.parent = transform;
GameObject part3 = Instantiate(this.clanek, new Vector3(-3, 0, 0), new Quaternion(0, 0, -90, 90)) as GameObject;
this.snakeParts.Add(part3);
part3.GetComponent<Part>().partRank = 4;
part3.transform.parent = transform;
GameObject tail = Instantiate(this.ocas, new Vector3(-4, 0, 0), new Quaternion(0, 0, -90, 90)) as GameObject;
this.snakeParts.Add(tail);
tail.GetComponent<Part>().partRank = 5;
tail.transform.parent = transform;
}
void Update()
{
if (Input.GetKey(KeyCode.RightArrow))
{
GameObject g = snakeParts[0] as GameObject;
g.GetComponent<Part>().direction = Direction.Right;
g.GetComponent<Part>().changedDirection = true;
g.GetComponent<Part>().ChangingPosition = g.GetComponent<Part>().endpos;
}
else if (Input.GetKey(KeyCode.UpArrow))
{
GameObject g = snakeParts[0] as GameObject;
g.GetComponent<Part>().direction = Direction.Up;
g.GetComponent<Part>().changedDirection = true;
g.GetComponent<Part>().ChangingPosition = g.GetComponent<Part>().endpos;
}
else if (Input.GetKey(KeyCode.DownArrow))
{
GameObject g = snakeParts[0] as GameObject;
g.GetComponent<Part>().direction = Direction.Down;
g.GetComponent<Part>().changedDirection = true;
g.GetComponent<Part>().ChangingPosition = g.GetComponent<Part>().endpos;
}
else if (Input.GetKey(KeyCode.LeftArrow))
{
GameObject g = snakeParts[0] as GameObject;
g.GetComponent<Part>().direction = Direction.Left;
g.GetComponent<Part>().changedDirection = true;
g.GetComponent<Part>().ChangingPosition = g.GetComponent<Part>().endpos;
}
}
}
Does anyone have any advice? Maybe there is a better solution, than delay by coroutine.