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 coral_push · Feb 19, 2014 at 05:21 AM · c#timersnake

Problem with Snake Game(input help)

I'm creating the classic snake game in Unity but I'm having trouble with my code that controls the direction of the snake. I have the snake "head" and body all in an array, in which i then update each position to the position of the object in front of it using a simple timer. Each tick of the timer also moves the snake 1 unit in a direction based on the input from the user.

The problem I'm having is since everything is working under a timer / tick in the update function. Sometimes the input doesnt get called even though its been pressed, due to the function that controls movement is inside the timer. Though if I put that input function outside the timer it creates a problem in which the player can move into itself if inputed directions very quickly.

I'm not wording my problem well at all and I'm sorry for that.. Here is some of my code that is attached to the snake object.

// Update is called once per frame

void Update() {

     if(!isDead){
         
         sinceLastTick += Time.deltaTime;
         
         //My tick timer that updates position of snake
         if(sinceLastTick > tickTime){
             
             //Gets input from arrow key
             HeadMovement();
             //updates each position of object in array to object in front(follow snake head)
             DrawSnake();    
             
             //move snake 
             cached.position += direction;

             sinceLastTick -= tickTime;
         }
     }
 }
                 
     
 //Keeps track of user input and stores vector 3 in 'direction' var
 private void HeadMovement() { 
     
     if(Input.GetKey("up") && !down) {
         up = true;
         down = false;
         left = false;
         right = false;
         
         //This is where we store our vector3 to get called each tick (mygrid.up == vector3.up)
         direction = myGrid.up;
                 
     }
     
     else if( Input.GetKey ("down") && !up){
         up = false;
         down = true;
         left = false;
         right = false;
             
         direction = -myGrid.up;
     
     }
     
     else if(Input.GetKey ("left") && !right ){
         up = false;
         down = false;
         left = true;
         right = false;
             
         direction = -myGrid.right;
     
     }
     
     else if(Input.GetKey ("right") && !left){
         up = false;
         down = false;
         left = false;
         right = true;
             
         direction = myGrid.right;
 
     }
 }
 
 //Method to draw each snake and update position
 private void DrawSnake() {
     
     //for every object in list 
     for (int i = (snakeUpdate.Count - 1); i > 0; i--){
         
         //give that object the position of object in front
         snakeUpdate[i].transform.position = snakeUpdate[i-1].transform.position;
         
         
     }
 }

I just cant wrap my head around the logic anymore, I know if i move my HeadMovement() function outside of the tick timer then it grabs input quicker then the snake can update itself (DrawSnake()) so it makes it able to turn into itself.

let me know if you have any questions. Thanks.

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 robertbu · Feb 19, 2014 at 06:29 AM

In Update(), every frame, collect a possible new direction. Get rid of up/down/left/right and the direction check for each key. Just do:

     if (Input.GetKeyDown(KeyCode.UpArrow))    nextDir = myGrid.up;
     else if (Input.GetKeyDown(KeyCode.DownArrow))  nextDir = -myGrid.up;
     else if (Input.GetKeyDown(KeyCode.LeftArrow))  nextDir = -myGrid.right;
     else if (Input.GetKeyDown(KeyCode.RightArrow)) nextDir = myGrid.right;

Then when you go to your timed step code, do:

 if (-nextDir != direction) {
    direction = nextDir;
 }

That way the user can press any number of keys. You only process the last one, and you only use it if the direction does not conflict.

Comment
Add comment · Show 7 · 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 coral_push · Feb 19, 2014 at 08:08 AM 0
Share

Thanks for the post! I'm going to try that tomorrow morning! I will post a build so you can try yourself and maybe get the feeling of the unresponsive input.

avatar image coral_push · Feb 19, 2014 at 10:47 PM 0
Share

So it sometimes still feels unresponsive, like it doesnt grab the input in time. (I think it has something to do with the tick/timer if statement that executes the code)

I have a dropbox webplayer for you to try out and maybe you can get the same feeling I have.

Edit: Took down dropbox link

avatar image robertbu · Feb 20, 2014 at 12:08 AM 0
Share

I can see how your original code would have this problem. But if you rewrite the script as I indicated, it should fix the problem. Post your modified script here, and I'll take another look.

avatar image coral_push · Feb 20, 2014 at 12:20 AM 0
Share

This is how I have it right now and it still feels a bit unresponsive

// Update is called once per frame

void Update() {

     if(!isDead){
         
         sinceLastTick += Time.deltaTime;

if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.UpArrow)) nextDir = myGrid.up; else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.DownArrow)) nextDir = -myGrid.up; else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow)) nextDir =-myGrid.right; else if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow)) nextDir = myGrid.right;

         //$$anonymous$$y tick timer that updates position of snake
         if(sinceLastTick > tickTime){

             if(-nextDir != direction){
                               direction = nextDir;
                      }
             
             //updates each position of object in array to object in front(follow snake head)
             DrawSnake();    
             
             //move snake 
             cached.position += direction;

             sinceLastTick -= tickTime;
         }
     }
 }
                 
     

avatar image coral_push · Feb 22, 2014 at 12:22 PM 0
Share

any idea??

Show more comments

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

19 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

Related Questions

how to get in game numerical input 1 Answer

how to create car wheel impression on ground in unity3d? 1 Answer

Android Development; How to use SDK manager! 1 Answer

How to draw an arrow from fps contoller 1 Answer

when game is resumed it would not be resumed from where it was paused ? 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