There is such a snake manual by Noobtuts

I made a snake on it, everything is fine, I added different things from myself, but here’s the problem - the tail behaves incorrectly. Namely - after eating 3 "apples" starts to make holes in the tail, if the snake crawls horizontally, and the tail crawls with steps parallel to the body, if the snake crawls vertically. Visually on the video, YouTube. https://youtu.be/YR6lfFX5xEI

Snake code

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 } } 

The question is how to avoid this and what to do so that the tail normally follows the head, cell by cell?

Closed due to the fact that off-topic party Nick Volynkin ♦ Jan 9 '17 at 16:45 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - Nick Volynkin
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    Very interesting. And what is your question? - VladD
  • 3
    @VladD Added a question to the question. - Dmitrii
  • The reason for closing the emphasis on the word minimal . - Nick Volynkin ♦

1 answer 1

Well, maybe I'm blind, but I do not see where it’s written that the previous object from the list remembers the location of the current object, and so along the chain for each object. You have written that the tail moves in the direction where the head was, but in order to make it to remember the current position, you need to register it. You only say half the code. You need to add the rest yourself.