My first Game (Snake) and some Problems
Hi guys, thats my first experience with unity. Im working with unity for 4 days so not that long time Ive created a little basic Snake Game, without automatic movement, where i want to add some features later ... but i have already some problems:
my box collider does not work well , sometimes the snake head goes through the food object without triggering or the trigger gets activated too late
i want my snake grow faster but im using the list function to remember the position of my snakehead, to put the tail on this position , but my problem is that im not working with a 1x1 pixel snake. Thats why the snake tail appears in the snake head to 75%. One snake part is 500x500 pixel xD (i know its way too big, the game is already 30 mb , but im just testing because i want to play the game on fullscreen later with a large field and i want to keep space for later graphic updates.
Maybe someone can tell me what will be a good size for one snakepart?
Is there any way to let the snaketails connect at the end of my snakehead? If not how can i change the movement of the snake to larger steps , one step big as a snake part?
My food is sometimes spawning on the border and not between the borders, also i wish me that the food dont spawn at a part of the snake
ive uploaded my project so you can test for yourself : https://dropfile.to/9g9ms
the code by now:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class SnakeScript : MonoBehaviour
{
public GameObject tailPrefab;
public GameObject food;
public Transform border_l;
public Transform border_r;
public Transform border_b;
public Transform border_t;
public float speed = 10f;
bool eat = false;
bool left = false;
bool right = false;
bool up = false;
bool down = false;
List<Transform> tail = new List<Transform>();
void Start()
{
Spawn();
}
void Update()
{
Move();
}
void Move()
{
if (Input.GetKey(KeyCode.RightArrow) && left == false)
{
transform.Translate(Vector2.right * speed*Time.deltaTime);
right = true;
up = false;
down = false;
}
else if (Input.GetKey(KeyCode.DownArrow) && up == false)
{
transform.Translate(Vector2.down * speed*Time.deltaTime);
down = true;
right = false;
left = false;
}
else if (Input.GetKey(KeyCode.LeftArrow) && right == false)
{
transform.Translate(Vector2.left * speed*Time.deltaTime);
left = true;
up = false;
down = false;
}
else if (Input.GetKey(KeyCode.UpArrow) && down == false)
{
transform.Translate(Vector2.up * speed* Time.deltaTime);
up = true;
left = false;
right = false;
}
Vector2 v = transform.position;
if (Input.GetKey(KeyCode.UpArrow) && down == false || Input.GetKey(KeyCode.RightArrow) && left == false || Input.GetKey(KeyCode.DownArrow) && up == false || Input.GetKey(KeyCode.LeftArrow) && right == false)
{
if (eat)
{
GameObject g = (GameObject)Instantiate(tailPrefab,v,Quaternion.identity);
tail.Insert(0, g.transform);
eat = false;
}
else if (tail.Count > 0)
{
tail.Last().position = v;
tail.Insert(0, tail.Last());
tail.RemoveAt(tail.Count - 1);
}
}
}
public void Spawn()
{
int x = (int)Random.Range(border_l.position.x, border_r.position.x);
int y = (int)Random.Range(border_t.position.y, border_b.position.y);
Instantiate(food, new Vector2(x, y), Quaternion.identity);
}
void OnTriggerEnter2D(Collider2D coll)
{
if (coll.name.StartsWith("food_prefab"))
{
eat = true;
Destroy(coll.gameObject);
Spawn();
speed=speed+5f;
}
}
}
sry for my bad english and my bad unity skills ^^ hope someone can help me !
Answer by hexagonius · Oct 02, 2015 at 09:48 AM
Quite a lot you ask there. I'd use a size around 1 for each part, that's where Unity works the best. instead of a list I'd use a queue for the waypoints. It's just another data type where you put in on the top and take at the bottom (first in last out). worked perfect for me when I did something like that. your triggering problems come from translating, which is essentially teleporting ignoring physics. use a rigidbody and set its velocity in FixedUpdate, or use AddForce, which is less controllable in your case. another option would be having a 2D array for your movement grid which holds info about each tile, if it's occupied by the snake, food, or free. you then could translate and would not need any collision, because by this array you know the state of each tile.
Thx for answering ;) Can you shortly explain how to use a queue in this case ? The Trigger problem is gone , i just rebuilded the scene and now its working again, why ever, but i tryed it to move with FixedUpdate ins$$anonymous$$d of Update and thats way better because the snake moves in bigger steps , but these steps getting larger and larger by speed up. Is there any way i can fix these steps by Fixed Update?
Your answer
Follow this Question
Related Questions
Box Collider 2Ds not colliding percisely 0 Answers
Problem with SmoothDamp when applied to each child of a gameObject 1 Answer
Turning every color in an image to many GameObjects ? 0 Answers
Unity : How to handle compound colliders 2D physics collision detection 0 Answers
objects overlap on collision 0 Answers