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 bakhtosh · Jan 26, 2015 at 05:06 AM · 2dcontrolssnake

Implementing snake-like 2D controls and behavior

Hi, I'm totally new to Unity, and I'm trying to get the hang of it by creating a simple snake clone. So the controls I'm after are:

  • Each fixed time frame, snake advances a fixed distance in the direction it's currently looking,

  • left key changes the direction by -90 degrees around the z-axis,

  • right key changes it by +90 degrees.

I've got some code that sort of does that, but occasionally it makes 2 turns after a key is pressed instead of 1. Any ideas how I might fix this are much appreciated!

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
     int ticks = 0;
     int tickThreshold = 30;
     float stepSize = 1.0f;
     Vector3 nextDirectionChange = Vector3.zero;
     
     void Start () {
         transform.forward = Vector3.right;
     }
     
     void FixedUpdate () {
         this.ticks++;
         if (this.ticks > this.tickThreshold) {
             transform.Rotate(this.nextDirectionChange);
             transform.position += transform.forward * stepSize;
             this.ticks = 0;
             this.nextDirectionChange = Vector3.zero;
         }
 
         if (Input.GetKey(KeyCode.LeftArrow)) {
             this.nextDirectionChange = Vector3.left * 90;
         }
         if (Input.GetKey(KeyCode.RightArrow)) {
             this.nextDirectionChange = Vector3.right * 90;
         }
     }
 }

Comment
Add comment · Show 2
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 Imtiaj Ahmed · Jan 26, 2015 at 07:17 AM 0
Share

As Getyour411 said, Get$$anonymous$$eyUp/Down will solve. But try not to do the input things in FixedUpdate(). They should run in Update().

avatar image bakhtosh · Jan 26, 2015 at 08:30 AM 0
Share

That makes sense, thank you!

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by getyour411 · Jan 26, 2015 at 05:07 AM

Use GetKeyUp/Down instead

Comment
Add comment · Show 1 · 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 bakhtosh · Jan 26, 2015 at 08:27 AM 0
Share

Thanks, I'll try that! Could you explain why, or point me to some docs to help me understand? I get that Get$$anonymous$$ey fires all the time while the key is pressed and Get$$anonymous$$eyUp/Down only fires once per up/down action, but I was thinking that there wouldn't be a difference in behavior since I'm only using the input to set the nextDirectionChange variable. As I see it, all that should happen is that nextDirectionChange gets set multiple times -- the movement should only happen once every tickThreshold ticks. What am I missing?

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

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

My character moves in seemingly random directions. 1 Answer

How Can I Rotate My 2D Top-Down Character Rotation With Mobile Joystick? 0 Answers

Top down 2d movement without being affected by rotation 2 Answers

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 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