public void move(Vector3 newPosition, bool didEat) { Vector3 oldPosition = transform.localPosition; transform.localPosition = newPosition; if (isTail()) { if (didEat) { GameObject newPart = Instantiate(Resources.Load("Body")) as GameObject; newPart.transform.SetParent(transform.parent); SnakeBodyScript newPartScript = newPart.GetComponent <SnakeBodyScript>(); newPartScript.prevBodyPart = transform; GetComponent <SnakeBodyScript>().nextBodyPart = newPart.transform; nextBodyPart.GetComponent <SnakeBodyScript>().move(oldPosition, false); Vector3 relativePos = newPosition - oldPosition; transform.rotation = Quaternion.LookRotation(relativePos, Vector3.up); } } else { Vector3 relativePos = newPosition - oldPosition; transform.rotation = Quaternion.LookRotation(relativePos, Vector3.up); nextBodyPart.GetComponent <SnakeBodyScript>().move(oldPosition, didEat); } }
private bool availablePosition(Vector3 position) { SnakeBodyScript cur = this.snakeHead; while (true) { if (cur.GetComponent <Transform>().position.Equals(position)) { return(false); } if (cur.nextBodyPart) { cur = cur.nextBodyPart.GetComponent <SnakeBodyScript>(); } else { break; } } return(true); }