Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 risheebatra · Jul 10, 2015 at 11:43 AM · movement scriptfollowsnake

360 Degree Snake

Hi! I'm using Unity / C# to make a variation on a 360-degree snake game, but I'm having trouble just getting the movement of the tail right (I have a similar problem as in this question: http://gamedev.stackexchange.com/questions/30400/how-to-approach-360-degree-snake, but I think my game is structured a little differently). I'm running into the problem where the snake's tail does follow the head, but instead of getting longer, the tail game objects just stack on top of each other. They are spawning in the right spot (which is behind the head, at a distance based on the size of the tail) but moving to the stack. In my code, I've left in some stuff that is commented out, because that almost worked...but it created some strange problems as well. Anyway, here is my code for the "Move" function, and I'd greatly appreciate any help!

         // Save current position
         Vector3 pos = transform.position;
         pos -= transform.right;
 
         // Move head into direction of rotation
         transform.Rotate (0, 0, rotation);
         transform.Translate (Vector2.right * Time.deltaTime * moveSpeed);
 
         // Insert new element if the snake has eaten
         if (ate) {
             pos -= transform.right * tail.Count;
 
             // Load Prefab
             GameObject g = (GameObject)Instantiate (tailPrefab, pos, transform.rotation);
 
             // Keep track of it the tail list
             tail.Insert (0, g.transform);
 
             // Reset the ate bool
             ate = false;
         } 
         if (tail.Count == 1) {
             tail [0].position = pos;
             tail [0].rotation = transform.rotation;
         }
         // Is there a multiple tail
         else if (tail.Count > 1)
         {
             // Move last Tail to where the Head was
             tail.Last().position = pos;
             tail.Last().rotation = transform.rotation;
             
             // Add to front of list and remove from the back
             tail.Insert(0, tail.Last());
             tail.RemoveAt(tail.Count - 1);
             /*
             Vector3 unitShift = transform.right;
             for (int i = 1; i < tail.Count; i += 1) {
                 tail[i].position = v - unitShift * i;
             }
 
             /*
             // I guess I need an array of positions?
             Vector3[] positions = new Vector3[tail.Count];
             for (int i = 0; i < tail.Count; i += 1) {
                 positions[i] = tail[i].position;
             }
 
             // loop through tail elements and make them follow the head
             for (int i = 1; i < tail.Count; i += 1) {
                 tail[i].position = positions[i];
                 tail[i].rotation = transform.rotation;
             }
             /*
              */
         }

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

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

2 People are following this question.

avatar image avatar image

Related Questions

Tail follow object - getting the point behind an object 0 Answers

Is this an [optimal, adequate, ftarded] way to make a snake follow its head? 2 Answers

Following AI - Similar to Snake Game 2 Answers

How to add Bezier Curves on a randomly moving object? 3d 1 Answer

Snaking - making body parts follow each other. 3 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