Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ThePeat · Dec 26, 2014 at 10:51 PM · c#2dmovementtransformprefab

Emerging Gap when moving my "Snake"

Hi there!

I am new to Unity but I am not as this new to C# - still I am a beginner.

So I am developing a snake game and I made a SpecialFood, which creates 5 new tails, when it is eaten. The 5 tails are added correctly, but after 6"Tails" has moved on - a gap is emerging (see Pic 1).

I can't explain to myself why this is happening - but I think there is a problem with the list (I "storage" the tails in a List called "tail").

So this is the code snippet for adding 5 tails:

 //SpecialFood -> Add 5 Tails
 for (int i = 0; i <= 4; i++ )
 {

     //Instantiate a TailPrefab
     GameObject g = (GameObject)Instantiate(tailPrefab,
                                            v,
                                            Quaternion.identity);

     //Add it to the list         
     tail.Add(g.transform);
 }


And this is the code for moving the tails:

 // Move last Tail Element to where the Head was
 tail.Last().position = v;

 // Add to front of list, remove from the back
 tail.Insert(0, tail.Last());
 tail.RemoveAt(tail.Count-1);


If any more stuff is needed to help me solving this problem, please tell me.

Edit:

Vector2 v = transform.position;

Just if you are wondering, what "v" is.

Greetings Picture 1

snake_gap.jpg (68.9 kB)
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
Best Answer

Answer by RudyTheDev · Dec 27, 2014 at 12:04 AM

Okay, this approach was interesting to analyze. I think you don't move the snake when you encounter the "superfood":

Image

It seems v is your current/head position. Then you spawn 3 segments there. Then you move those new segments first/next (since they are last in list). This results in a gap, because you never placed an "older" segment on v in a sequential order.

I guess you could move the snake, and instead spawn the new segments on at its tail old location.

Personally, I would keep a segmentToAdd counter. Each frame, when moving the snake, I would check if (segmentToAdd > 0) and spawn a segment instead (inserting at 0).


snake.png (14.3 kB)
Comment
Add comment · Show 2 · 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 ThePeat · Dec 27, 2014 at 02:17 PM 0
Share

Thank you, your picture and your suggestion to spawn the segments at the tail's last position were helpful :)

Could you implement this segmentToAdd counter thing? It sounds interesting, it would also enrich my knowledge :)

avatar image RudyTheDev · Dec 28, 2014 at 02:02 PM 0
Share

Without seeing all of your code, I can't really "implement" it.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

Sprite is not shown moving 1 Answer

Uneven speed in 2d movement script 2 Answers

I need to change the animation when my speed increases in a 2D game 0 Answers

Unity's mouse look causing movement issues 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