Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Aquillis · Mar 23, 2020 at 12:10 AM · 2d gamesnaketail

2D Snake Game Tail Problems

Hello. I'm currently working on my first actual project to allow children to experience the games that preceded things like Mario and Legend of Zelda. I'm currently calling it 'Project Ancient Pack', and right now I'm working on a 2D snakes game that, right now, only has one player. However, I'm running into a problem where the tail, when the snake 'eats' an item, spawns the tail object but does not actually 'follow' the snake at the moment.

Currently, this is the script that I'm working on, based off of Coffee Break Codes' own snake game tutorial:

 using System.Collections;
 using System.Collections.Generic;
 using System.Linq;
 using UnityEngine;
 
 public class Snake : MonoBehaviour
 {
     public Transform head;
     List<Transform> tail = new List<Transform>();
 
     public GameObject food;
     public GameObject tailPrefab;
     public Transform rBorder;
     public Transform lBorder;
     public Transform tBorder;
     public Transform bBorder;
 
     private float speed = 0.003f;
 
     Vector2 dir = Vector2.up;
     Vector2 moveVector;
 
     bool ate = false;
     bool horizontal = true;
     bool vertical = false;
     
 
     
 
     // Start is called before the first frame update
     void Start()
     {
         Spawn();
         InvokeRepeating("Move", 0.003f, speed);
     }
 
     // Update is called once per frame
     void Update()
     {
         if (Input.GetKey(KeyCode.RightArrow) && horizontal)
         {
             horizontal = false;
             vertical = true;
             dir = Vector2.right;
         }
         else if (Input.GetKey(KeyCode.UpArrow) && vertical)
         {
             horizontal = true;
             vertical = false;
             dir = Vector2.up;
         }
         else if (Input.GetKey(KeyCode.LeftArrow) && horizontal)
         {
             horizontal = false;
             vertical = true;
             dir = -Vector2.right;
         }
         else if (Input.GetKey(KeyCode.DownArrow) && vertical)
         {
             horizontal = true;
             vertical = false;
             dir = -Vector2.up;
         }
         moveVector = dir / 3f;
     }
 
     void Move()
     {
         Vector2 ta = transform.position;
         if (ate)
         {
             if (speed > 0.002)
             {
                 speed = speed - 0.002f;
             }
             GameObject g = Instantiate(tailPrefab, ta, Quaternion.identity);
             tail.Insert(0, g.transform);
             Debug.Log(speed);
             ate = false;
         }
         else if (tail.Count < 0)
         {
             tail.Last().position = ta;
             tail.Insert(0, tail.Last());
             tail.RemoveAt(tail.Count - 1);
         }
         transform.Translate(moveVector);
     }
     
     void Spawn()
     {
         //X position between the left and right borders
         int x = (int)Random.Range(lBorder.position.x, rBorder.position.y);
 
         //Y position between the top and bottom borders
         int y = (int)Random.Range(bBorder.position.y, tBorder.position.y);
 
         //initiate the food at (x, y)
         Instantiate(food, new Vector2(x, y), Quaternion.identity); //Default Rotation
     }
 
     void OnTriggerEnter2D(Collider2D col)
     {
         if (col.name.StartsWith("Food"))
         {
             ate = true;
             Destroy(col.gameObject);
             Spawn();
         }
     }
     
 }

Everything works fine in this, save for the problem of the snake tail... dropping off like the blocks are doing mitosis.

Is this too small to see? I only just managed to get it streamed.

Anyways, I'd like to know a good solution to this without changing what I already got.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

232 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Snake game, problem with tail 0 Answers

Smooth snake game turning animation - Bring me on the rigth track 0 Answers

Issues with snake game in C# 2 Answers

in a 2d game do i use 3d phsyics since theres no 2d game objects? 1 Answer

crouch attack collider 2d question 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges