Есть такой мануал по змейке от Noobtuts
Сделал змейку по нему, всё хорошо, добавил разное от себя, но вот проблема - ведёт себя некорректно хвост. А именно - после съедания 3 "яблока" начинает делать дырки в хвосте, если змейка ползёт горизонтально, и хвост ползет ступеньками параллельно телу, если змейка ползёт вертикально. Наглядно на видео, ютуб. https://youtu.be/YR6lfFX5xEI
Код самой змейки
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine.UI;
public class Snake2: MonoBehaviour {
public Text tt;
int score = 0;
public void ClickTestu() {
dir = Vector2.up; // This code is executed every frame that the RepeatButton remains clicked
}
public void ClickTestd() {
dir = -Vector2.up; // This code is executed every frame that the RepeatButton remains clicked
}
public void ClickTestl() {
dir = Vector2.left; // This code is executed every frame that the RepeatButton remains clicked
}
public void ClickTestr() {
dir = Vector2.right; // This code is executed every frame that the RepeatButton remains clicked
}
// Did the snake eat something?
bool ate = false;
// Tail Prefab
public GameObject tailPrefab;
List < Transform > tail = new List < Transform > ();
Vector2 dir = Vector2.right;
// Use this for initialization
void Start() {
InvokeRepeating("Move", 0.3 f, 0.3 f);
}
// Update is called once per frame
void Update() {
tt.text = "Score " + score;
}
void Move() {
// Save current position (gap will be here)
Vector2 v = transform.position;
// Move head into new direction (now there is a gap)
transform.Translate(dir);
// Ate something? Then insert new Element into gap
if (ate) {
// Load Prefab into the world
GameObject g = (GameObject) Instantiate(tailPrefab,
v,
Quaternion.identity);
// Keep track of it in our tail list
tail.Insert(0, g.transform);
// Reset the flag
ate = false;
}
// Do we have a Tail?
else if (tail.Count > 0) {
// Move last Tail Element to where the Head was
tail.Last().position = v;
// Add to front of list, remove from the back
tail.Insert(0, tail.Last());
tail.RemoveAt(tail.Count - 1);
}
}
void OnTriggerEnter2D(Collider2D coll) {
// Food?
if (coll.name.StartsWith("food")) {
// Get longer in next Move call
ate = true;
// Remove the Food
Destroy(coll.gameObject);
score++;
}
// Collided with Tail or Border
else {
dir = -Vector2.up; // ToDo 'You lose' screen
}
}
Вопрос - как избежать такого и что сделать чтобы хвост нормально следовал за головой, клеточка за клеточкой?