• 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 sipinho · Jun 15, 2015 at 02:57 PM · snakeparts

Conecting snake parts

Hi, I'm making snake and i have an problem with part conection. Each part has an script with own smooth grid movement:

 using UnityEngine;
 using System.Collections;
 
 public enum Direction { Right, Up, Left, Down}
 
 public class Part : MonoBehaviour 
 {
     public const float speed = 0.5f;
     public Direction direction = Direction.Right;
     public Vector3 endpos;                                  //target coordinates
     public bool isMoving = false;                           //serves to indicate if the part is on the center of cell
     
     public bool changedDirection = false;                   //serve to indicate if direction is changed
     public int partRank;                                    //register order of the part
     public Vector3 ChangingPosition;                        //this is a position where parts should change their direction  
 
     void Update()
     {
         if (isMoving == false)
         {
             endpos = transform.position;
 
             if (direction == Direction.Right)
             {
                 endpos += Vector3.right;
             }
             else if (direction == Direction.Up)
             {
                 endpos += Vector3.up;
             }
             else if (direction == Direction.Left)
             {
                 endpos += Vector3.left;
             }
             else if (direction == Direction.Down)
             {
                 endpos += Vector3.down;
             }
 
             isMoving = true;
         }
 
         if (isMoving == true)
         {
             transform.position = Vector3.MoveTowards(transform.position, endpos, speed * Time.deltaTime);
             if (transform.position == endpos)
             {
                 isMoving = false;
 
                 if (changedDirection == true && transform.position == ChangingPosition)
                 {
                     GameObject g = gameObject.GetComponentInParent<Snake>().snakeParts[partRank] as GameObject;
                     //g.GetComponent<Part>().direction = direction;
                     StartCoroutine("setDirection", g);
                     g.GetComponent<Part>().changedDirection = true;
                     g.GetComponent<Part>().ChangingPosition = ChangingPosition;
                     changedDirection = false;
                 }
             }
         }
     }
 
     IEnumerator setDirection(GameObject g)
     {
         yield return new WaitForSeconds(Time.deltaTime);
         g.GetComponent<Part>().direction = direction;
     }
 }


Parts transmit informations about changing their direction. It works, but if i change direction twice consecutively it doesn't work.

To all parts is superior one object, which changes direction with following script: using UnityEngine; using System.Collections;

 public class Snake : MonoBehaviour 
 {
     public GameObject hlava;
     public GameObject clanek;
     public GameObject ocas;
 
     public ArrayList snakeParts = new ArrayList();
 
     void Start()
     {
         GameObject head = Instantiate(this.hlava, new Vector3(0, 0, 0), new Quaternion(0, 0, -90, 90)) as GameObject;
         this.snakeParts.Add(head);
         head.GetComponent<Part>().partRank = 1;
         head.transform.parent = transform;
         GameObject part1 = Instantiate(this.clanek, new Vector3(-1, 0, 0), new Quaternion(0, 0, -90, 90)) as GameObject;
         this.snakeParts.Add(part1);
         part1.GetComponent<Part>().partRank = 2;
         part1.transform.parent = transform;
         GameObject part2 = Instantiate(this.clanek, new Vector3(-2, 0, 0), new Quaternion(0, 0, -90, 90)) as GameObject;
         this.snakeParts.Add(part2);
         part2.GetComponent<Part>().partRank = 3;
         part2.transform.parent = transform;
         GameObject part3 = Instantiate(this.clanek, new Vector3(-3, 0, 0), new Quaternion(0, 0, -90, 90)) as GameObject;
         this.snakeParts.Add(part3);
         part3.GetComponent<Part>().partRank = 4;
         part3.transform.parent = transform;
         GameObject tail = Instantiate(this.ocas, new Vector3(-4, 0, 0), new Quaternion(0, 0, -90, 90)) as GameObject;
         this.snakeParts.Add(tail);
         tail.GetComponent<Part>().partRank = 5;
         tail.transform.parent = transform;
     }
 
     void Update()
     {
         if (Input.GetKey(KeyCode.RightArrow))
         {
             GameObject g = snakeParts[0] as GameObject;
             g.GetComponent<Part>().direction = Direction.Right;
             g.GetComponent<Part>().changedDirection = true;
             g.GetComponent<Part>().ChangingPosition = g.GetComponent<Part>().endpos;
         }
         else if (Input.GetKey(KeyCode.UpArrow))
         {
             GameObject g = snakeParts[0] as GameObject;
             g.GetComponent<Part>().direction = Direction.Up;
             g.GetComponent<Part>().changedDirection = true;
             g.GetComponent<Part>().ChangingPosition = g.GetComponent<Part>().endpos;
         }
         else if (Input.GetKey(KeyCode.DownArrow))
         {
             GameObject g = snakeParts[0] as GameObject;
             g.GetComponent<Part>().direction = Direction.Down;
             g.GetComponent<Part>().changedDirection = true;
             g.GetComponent<Part>().ChangingPosition = g.GetComponent<Part>().endpos;
         }
         else if (Input.GetKey(KeyCode.LeftArrow))
         {
             GameObject g = snakeParts[0] as GameObject;
             g.GetComponent<Part>().direction = Direction.Left;
             g.GetComponent<Part>().changedDirection = true;
             g.GetComponent<Part>().ChangingPosition = g.GetComponent<Part>().endpos;
         }
     }
 }

Does anyone have any advice? Maybe there is a better solution, than delay by coroutine.

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 tedthebug · Jun 25, 2015 at 04:07 AM

Not sure if you wanted strict 90° turns but if so see if the code I posted here http://answers.unity3d.com/questions/909970/help-with-snake-like-movement.html

Is any use. I didn't script each body part, I used a game manager to manage it all as I actually destroyed & instantiated the bits.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

snake game revisit, some doubts in it 1 Answer

How to properly implement snake style controls for android? 0 Answers

Dealing with Mesh parts & Animations 0 Answers

How to use Spawner-Free 0 Answers

Allow collision but take no force from it, not increasing object mass 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