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 /
avatar image
0
Question by OBess · Jun 14, 2018 at 03:32 PM · 2d2d gamegamesnaketail

SNAKE [Tail]

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class ControllSnake : MonoBehaviour {
 
     public Button buffe;
     public Text coin;
     public Text score;
     public GameObject SnakeBody;
     private int c = 0;
     private int s = 0;
     private int i = 0;
     private List<GameObject> Tail = new List<GameObject>();
 
 
     void Start () {
         //transform.localScale = new Vector3 (PlayerPrefs.GetFloat ("ScaleX"),PlayerPrefs.GetFloat ("ScaleY"), 1f);
         //GetComponent<SpriteRenderer> ().sprite = Resources.Load<Sprite>("Image/" + PlayerPrefs.GetString ("Name"));
 
         PlayerPrefs.SetInt ("Click", 0);
 
         Tail.Clear ();
         for(int i = 0; i < 3; i++)
             AddTail();
     }
 
     void Update () {
         TailMove ();
 //move head
         i = PlayerPrefs.GetInt ("Click");
         if (i == 0) {
             transform.Translate (Time.deltaTime * PlayerPrefs.GetFloat ("Power"), 0f, 0f);
         } else if (i == 1) {
             transform.Translate (0f, Time.deltaTime * PlayerPrefs.GetFloat ("Power"), 0f);
         } else if (i == 2) {
             transform.Translate (-Time.deltaTime * PlayerPrefs.GetFloat ("Power"), 0f, 0f);
         } else if (i == 3) {
             transform.Translate (0f, -Time.deltaTime * PlayerPrefs.GetFloat ("Power"), 0f);
         } else if (i == -1) {
             transform.Translate (0f, 0f, 0f);
         }
     }
 
     void OnCollisionEnter2D(Collision2D col){
         if (col.gameObject.tag == "coin") {
             c = c + 1;
             coin.text = c.ToString ();
             Destroy (col.gameObject);
         }
 
         if (col.gameObject.tag == "score") {
             s = s + 1;
             score.text = s.ToString ();
             Destroy (col.gameObject);
             AddTail ();
         }
     }
 //move tail
     public void TailMove(){
         Tail [0].transform.position = this.transform.position;
         for (int j = Tail.Count-1; j > 0; j--) {
             Tail [j].transform.position = Tail [j - 1].transform.position;
         }
     }
 //add tail
     public void AddTail(){
         Vector3 Position = transform.position;
 
         if (Tail.Count > 0) {
             Position = Tail [Tail.Count - 1].transform.position;
         }
         Position.x--;
         GameObject Body = Instantiate (SnakeBody, Position, Quaternion.identity) as GameObject;
 
         Tail.Add (Body);
     }
 }



White is a head, black is a tail

How to make my tail look like a tail. I use a position on the head, this is for the tail, which should know where the head is, but I do not know how to make the tail long.

снимок-экрана-107.png (27.8 kB)
Comment
Add comment · Show 2
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
avatar image OBess · Jun 14, 2018 at 03:34 PM 0
Share

Black is tail, white is a head

avatar image Harinezumi · Jun 14, 2018 at 04:02 PM 1
Share

$$anonymous$$ore information is needed to be able to efficiently help you. Does the tail have a SpriteRenderer? In that case, part of the problem is that the tail does not have transparent pixels.
The code is also difficult to understand: should the head be at the end of Tail or at the beginning? I would recommend changing to the beginning, as you always need a head, and its the tail that "grows". AddTail() is very confusing: it adds a head to the end of Tail, but it is called 3 times in Start() (is this a hydra? :) ).
I think it would be better to store the head separately. I'll try to come up with a solution if I have some time.

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

204 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

Related Questions

How to have coins burst out in random directions after destroying an object in unity? 2 Answers

How do I create a 2d shooting mechanic (Left and Right)? 1 Answer

Drag and release 2 Answers

How do i dash in a direction an arrow is showing? 1 Answer

2d Lighting - Weird light triangles 0 Answers


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