- Home /
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);
}
}
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.
$$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.