• 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 /
This question was closed May 13, 2013 at 01:44 PM by Fattie for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by MrZalib · May 13, 2013 at 01:06 AM · syntax-errorsnake

Fresh pair of eyes

I've been working on this problem for quite a while now and I've had complete failure at every turn. I finally came up with logic that I swear will work but I have no clue why it doesn't so I need a fresh pair of eyes to give me a different perspective here's my situation (And this could be a long one):

I'm working on a Snake game (To get experience with the program) and I'm working on how the body follows the 'head'. Since I know that the 'head' can make multiple turns the body has to essentially mimic the 'head'. To do this I've made an array of turn points that the body uses to store the points where the head turns. To achieve this I'm using MoveTowards as a means for movement. BUT when tested the body doesn't work correctly. Before I give my code I'd like to tell you that the turnPoint array only gets filled if the length of the snake is greater than 0. (This is part of my logic)

Here's my code for the Body Controller:

 enterusing UnityEngine;
 using System.Collections;
 
 public class BodyController : MonoBehaviour 
 {
     public int direction;
     public Vector3 v3NextPos;
     private Vector3 zeroVector;
     public bool isLast;
     //public bool debug;
     
     PlayerControl playerControl;
     
     // Use this for initialization
     void Start() 
     {
         direction = 1;
         zeroVector = new Vector3(0,0,0);
         playerControl = GameObject.FindWithTag("Player").GetComponent<PlayerControl>();
         v3NextPos = playerControl.turnPoint[0];
     }
     
     // Update is called once per frame
     void Update() 
     {
         //Initilization test condidion. If turnPoint wasn't set.
         if(v3NextPos == zeroVector)
         {
             followPlayer();
             if(playerControl.turnPoint[0] != zeroVector)     //Condition to make sure that if there is
                 v3NextPos = playerControl.turnPoint[0];        //only one body segment it still gets
                                                              //turnPoints.
         }    
         else if(v3NextPos != zeroVector)
         {
             
             
             if(transform.position != v3NextPos)             //If a turnpoint was set, follow that
             {
                 followTurnPoint();
                 
             }
             else if(transform.position == v3NextPos)        //When the body reaches the point remove it from the
             {                                                //array and follow player
                 removePoint();
                 
                 v3NextPos = zeroVector;                     //Set v3NextPos equal to zeroVector so that it
                                                             //will return to following the player.
             }
         }
             
     }
     
     
     void followPlayer()
     {    
         transform.position = Vector3.MoveTowards(transform.position, playerControl.transform.position, 
                                                         Time.deltaTime * playerControl.speed);
     }
     
     void followTurnPoint()
     {
         transform.position = Vector3.MoveTowards(transform.position, v3NextPos, 
                                                         Time.deltaTime * playerControl.speed);
     }
     
     void removePoint()
     {
         //ignore this for now.
     }
 }

If you need any further explaining please don't hesitate to ask, all I'm asking is what could possibly go wrong with this code. I would even take a suggestion on a concept that handles achieving the same thing without doing all this craziness. I can even send you an .exe if you'd like so you can see how the game works with this code.

Thanks for reading if you did,

Sincerely, At the end of my rope.

Comment
Add comment · Show 4
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 Fattie · May 13, 2013 at 12:56 PM 0
Share

is turnPoint a List ? (I don't think the declaration is included.)

If not, I suggest using List as it is tremendously more useful for this sort of thing.

avatar image MrZalib · May 13, 2013 at 01:00 PM 0
Share

turnPoint is an array of Vector3's, I didn't really feel I need to functionality of a List in this situation since all I'd be doing is adding to it and removing from it.

Edit: failed to mention what turnPoint was an array of

avatar image Fattie · May 13, 2013 at 01:03 PM 0
Share

can you say more clearly what the problem is, I'm struggling to see the problem issue.

avatar image MrZalib · May 13, 2013 at 01:11 PM 1
Share

Well consequently upon second look of my code I figured out my problem. And because I didn't really think things through before posting this "question" I'll be taking it down. It wasn't being useful, and like you made me see, I neither had a useful title nor did I include what the problem actually was. Thanks for your willingness to help though! =)

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Fattie · May 13, 2013 at 05:53 AM

could moderators reject questions with a title that does not summarise the question. this message is for moderators only, cheers

Comment
Add comment · Show 3 · 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 MrZalib · May 13, 2013 at 12:59 PM 0
Share

This made my day. Haha thank you.

avatar image Fattie · May 13, 2013 at 01:02 PM 0
Share

lol dude :)

avatar image Bunny83 · May 13, 2013 at 01:37 PM 0
Share

This wasn't ment to be funny....

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

16 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

Related Questions

Instantiate a object after destroying a instantiated object. 1 Answer

Snake Mechanic 0 Answers

The script does not work the same as it works for the head(main) 1 Answer

error with script plz help! 2 Answers

Enemy AI scripting error 2 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