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 hvd · Oct 02, 2015 at 09:01 AM · boxcollider2dpixelssnake

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:

  1. 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

  2. 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?

  1. 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 !

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
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 hvd · Oct 02, 2015 at 04:22 PM 0
Share

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

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

30 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

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


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