Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Dec 05, 2019 at 09:20 PM by YJ8 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by YJ8 · Dec 02, 2019 at 05:53 PM · update problemprocessing

Snake game movement problem

Hello, currently learning how to make a snake game. The problem is that when user input two input very quickly, first input will be process but it won't update in the game. For example (the picture below ), Snake is currently moving right, and when I pressed down and left quickly, it will process the down button but won't update on the game and just turn left, which collide with the body which resulted in game over.

Here's the snake movement:

 public class Snake : MonoBehaviour
 {
     Vector2 direction = Vector2.right;
 
       void Start()
         {
             Time.timeScale = 1f;
             InvokeRepeating(nameof(this.Move), 0.09f, 0.09f);
             InvokeRepeating("Score", 5, 5);
         }
     
         // Update is called once per frame
         void Update()
         {
             if (Input.GetKey(KeyCode.RightArrow))
             {
                 if (direction != Vector2.left)
                 {
                     direction = Vector2.right;
                 }
             }
             if (Input.GetKey(KeyCode.DownArrow))
             {
                 if (direction != Vector2.up)
                 {
                     direction = Vector2.down;
                 }
             }
             if (Input.GetKey(KeyCode.LeftArrow))
             {
                 if (direction != Vector2.right)
                 {
                     direction = Vector2.left;
                 }
             }
             if (Input.GetKey(KeyCode.UpArrow))
             {
                 if (direction != Vector2.down)
                 {
                     direction = Vector2.up;
                 }
             }
     
         }

      void Move()
     {
 
     if (GameController.isPaused) return;

     Vector2 currentPos = transform.position;
     transform.Translate(direction);
     
     
     if (ate)
     {
         var newPart = (GameObject)Instantiate(tailPrefab, currentPos, Quaternion.identity);
         tail.Insert(0, newPart.transform);
         length+=5;
         ate = false;
     }
     else if (tail.Count != 0)
     {
         tail.Last().position = currentPos;
         tail.Insert(0, tail.Last());
         tail.RemoveAt(tail.Count - 1);
     }

     if (poison)
     {
         {

             Destroy(tail[tail.Count - 1].gameObject);
             tail.RemoveAt(tail.Count - 1);
             poison = false;
         }
     }

     }
 }
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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by Anis1808 · Dec 03, 2019 at 12:12 AM

Can you try this ?

  public class Snake : MonoBehaviour
  {
      Vector2 direction = Vector2.right;
      Vector2 waitingDirection = Vector2.zero;
      bool justChangedDirection, useNextDirection;
  
        void Start()
          {
              Time.timeScale = 1f;
              InvokeRepeating(nameof(this.Move), 0.09f, 0.09f);
              InvokeRepeating("Score", 5, 5);
          }
      
          // Update is called once per frame
          void Update()
          {
              if (Input.GetKey(KeyCode.RightArrow))
              {
                  if (direction != Vector2.left)
                  {
                      if(!justChangedDirection)
                             direction = Vector2.right;
                             justChangedDirection = true;
                      }else{
                             waitingDirection = Vector2.right;
                       }
                  }
              }
              if (Input.GetKey(KeyCode.DownArrow))
              {
                  if (direction != Vector2.up)
                  {
                      if(!justChangedDirection)
                             direction = Vector2.down;
                             justChangedDirection = true;
                      }else{
                             waitingDirection = Vector2.down;
                       }
                  }
              }
              if (Input.GetKey(KeyCode.LeftArrow))
              {
                  if (direction != Vector2.right)
                  {
                      if(!justChangedDirection)
                             direction = Vector2.left;
                             justChangedDirection = true;
                      }else{
                             waitingDirection = Vector2.left;
                       }
                  }
              }
              if (Input.GetKey(KeyCode.UpArrow))
              {
                  if (direction != Vector2.down)
                  {
                      if(!justChangedDirection)
                             direction = Vector2.up;
                             justChangedDirection = true;
                      }else{
                             waitingDirection = Vector2.up;
                       }
                  }
              }
      
          }
       void Move()
      {
  
      if (GameController.isPaused) return;
      Vector2 currentPos = transform.position;
       if(!useNextDirection){
                 transform.Translate(direction);
       }else{
                 transform.Translate(direction);
                  useNextDirection = false;
       }
      if(justChangedDirection){
               useNextDirection = true;
              justChangedDirection = false;
        }
      
      if (ate)
      {
          var newPart = (GameObject)Instantiate(tailPrefab, currentPos, Quaternion.identity);
          tail.Insert(0, newPart.transform);
          length+=5;
          ate = false;
      }
      else if (tail.Count != 0)
      {
          tail.Last().position = currentPos;
          tail.Insert(0, tail.Last());
          tail.RemoveAt(tail.Count - 1);
      }
      if (poison)
      {
          {
              Destroy(tail[tail.Count - 1].gameObject);
              tail.RemoveAt(tail.Count - 1);
              poison = false;
          }
      }
      }
  }
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 YJ8 · Dec 03, 2019 at 12:38 AM 0
Share

Thanks, it work. but the snake is not moving smoothly since it has to wait to changed direction. So if I press down left quickly, before it just die. Now it won't take do anything since it hasn't turn down yet. Thanks for your help.

avatar image Anis1808 YJ8 · Dec 03, 2019 at 12:47 AM 0
Share

But isn't that what you wanted in the first place ?

avatar image YJ8 Anis1808 · Dec 03, 2019 at 12:59 AM 0
Share

True, it does solve the problem i'm having. But now the control isn't smooth which will take the user experience away. I'm gonna try to see if I can make your code run smoother somehow. But still, thanks for the help and suggestion.

avatar image
0

Answer by Larry-Dietz · Dec 02, 2019 at 06:43 PM

Try changing all the if's in your input checks (except the first one, to else if

That way there is at least 1 frame between processing the keys.

Comment
Add comment · Show 9 · 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 YJ8 · Dec 02, 2019 at 07:53 PM 0
Share

I tried it, and it still occurs.

avatar image Larry-Dietz YJ8 · Dec 02, 2019 at 10:54 PM 0
Share

Where is $$anonymous$$ove being called from?

I would try adding the call to $$anonymous$$ove at the end of Update. As long as move gets called once between keypresses, this should work, as far as I can see (with the else if)

avatar image YJ8 Larry-Dietz · Dec 02, 2019 at 11:19 PM 0
Share

Call from the void start(){ InvokeRepeating(nameof(this.$$anonymous$$ove), 0.09f, 0.09f); } . I tried invoking move at the end of Update, but it update too fast which result in snake moving too quickly.

Show more comments

Follow this Question

Answers Answers and Comments

116 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 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 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 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 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 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 avatar image avatar image

Related Questions

Plane Processing 1 Answer

Updated from 5.3.3 to 5.3.4 and can't open project 1 Answer

Reflection code to test method override not working after updating Unity from 2017.1.0p4 to 2018.1.0f2 1 Answer

Upgrading Unity from 2018.2.16 to 2019.2.16 broke my project 1 Answer

GLSL - Post processing? 0 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