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 HYPERSAVV · Feb 01, 2013 at 01:21 AM · 2dgridjittery

Jittery Constant Grid Movement

Hello everyone,

I just started using Unity this past week. To get some practice I decided to make a Pacman/Snake hybrid. Eat cubes that are randomly generated, simple enough. At first I had a simple movement system that moved the player in a direction so long as the key was pressed. Ideally, I wanted to have grid like movement that also remembered your last key press to keep you going in one direction without having to hold a key down.

Without having to dive into code, I'll go into the implementation just a little. I keep track of current direction and new direction. So long as we are not at a bound/wall, we will to the next point in our grid. Once we hit a point we check to see if a new direction was pressed, and there was we set it to current.

I implemented this separately to make sure I understood how both worked, and they performed just fine. However, once I put them together things went downhill. Yes, they both technically work together, but not how I'd like. Rather than a smooth continuous line I get constant movement that pauses at each point. I've tried rearranging things, lowering settings (the FPS script showed 60+), trying to optimize my code, etc. Nothing has worked, so I figured I'd use my resources!

Here is the script attached to my Player object

 using UnityEngine;
 using System.Collections;
 
 public class PlayerScript : MonoBehaviour {
 
     enum Directions:int {NONE, UP, DOWN, LEFT, RIGHT};
     
     public float movementSpeed = 10;
     Vector3 nextPosition = default(Vector3);
     bool onRouteToDestination = false;
     
     Directions newDirection = Directions.NONE;
     Directions currentDirection = Directions.NONE;
     
     // Update is called once per frame
     void Update () {
         // Set the players direction. We check their current direction to see if
         // the key pressed is a NEW direction. If it is we queue it up.
         if(Input.GetButtonDown ("RIGHT") && currentDirection != Directions.RIGHT){
             if(currentDirection == Directions.NONE){
                 currentDirection = Directions.RIGHT;
             }
             else{
                 newDirection = Directions.RIGHT;
             }
         }
         else if(Input.GetButtonDown ("LEFT") && currentDirection != Directions.LEFT){
             if(currentDirection == Directions.NONE){
                 currentDirection = Directions.LEFT;
             }
             else{
                 newDirection = Directions.LEFT;
             }
         }
         else if(Input.GetButtonDown ("UP") && currentDirection != Directions.UP){
             if(currentDirection == Directions.NONE){
                 currentDirection = Directions.UP;
             }
             else{
                 newDirection = Directions.UP;
             }
         }
         else if(Input.GetButtonDown ("DOWN") && currentDirection != Directions.DOWN){
             if(currentDirection == Directions.NONE){
                 currentDirection = Directions.DOWN;
             }
             else{
                 newDirection = Directions.DOWN;
             }
         }
             
         // If we do not have a destination, and we are moving, calculate our next point
         if(onRouteToDestination == false && currentDirection != Directions.NONE){
             // Calculate our desired position
             if(currentDirection == Directions.RIGHT){
                 nextPosition = new Vector3(transform.localPosition.x+0.5f,transform.localPosition.y,transform.localPosition.z);
             }
             else if(currentDirection == Directions.LEFT){
                 nextPosition = new Vector3(transform.localPosition.x-0.5f,transform.localPosition.y,transform.localPosition.z);
             }
             else if(currentDirection == Directions.UP){
                 nextPosition = new Vector3(transform.localPosition.x,transform.localPosition.y,transform.localPosition.z+1);
             }
             else if(currentDirection == Directions.DOWN){
                 nextPosition = new Vector3(transform.localPosition.x,transform.localPosition.y,transform.localPosition.z-1);
             }
             
             // We're good to go!
             onRouteToDestination = true;
             
             // Also make sure that our next point is within bounds
             // If it isn't, put a halt to this vacation
             // Check Left, Right, Top, and Bottom bounds
             if(nextPosition.x > 4.75f || nextPosition.x < -4.75f || 
                 nextPosition.z > 4.50f || nextPosition.z < -4.50f){
                 onRouteToDestination = false;
                 currentDirection = newDirection;
                 newDirection = Directions.NONE;
             }
         }
         
         // Time to move (or continue moving)
         if(onRouteToDestination){
             // Check to see if the points are approximatly the same
             if(!Mathf.Approximately(Vector3.Distance(transform.localPosition,nextPosition),0f)){
                 if(currentDirection == Directions.RIGHT){
                     transform.Translate(new Vector3(movementSpeed*Time.deltaTime, 0, 0));
                     if(transform.localPosition.x > nextPosition.x){
                         transform.localPosition = nextPosition;
                     }
                 }
                 else if(currentDirection == Directions.LEFT){
                     transform.Translate(new Vector3(-movementSpeed*Time.deltaTime, 0, 0));
                     if(transform.localPosition.x < nextPosition.x){
                         transform.localPosition = nextPosition;
                     }
                 }
                 else if(currentDirection == Directions.UP){
                     transform.Translate(new Vector3(0, movementSpeed*Time.deltaTime, 0));
                     if(transform.localPosition.z > nextPosition.z){
                         transform.localPosition = nextPosition;
                     }
                 }
                 else if(currentDirection == Directions.DOWN){
                     transform.Translate(new Vector3(0, -movementSpeed*Time.deltaTime, 0));
                     if(transform.localPosition.z < nextPosition.z){
                         transform.localPosition = nextPosition;
                     }
                 }    
             }
             else{
                 onRouteToDestination = false;
                 if(newDirection != Directions.NONE){
                     currentDirection = newDirection;
                     newDirection = Directions.NONE;
                 }
             }
         }
     }    
 }

I've looked over it quite a few times now and nothing really sticks out as computationally difficult, aside from my Approximately and Distance. I'm not sure what the overhead is on calling those. Either way, if anyone could help me out in the slightest bit I would be grateful.

To get a better feel, I uploaded this buggy version to my website. The last working one can be found here.

Note: I had used StartCoroutine before that called a method that would then figure out my next point and move. This worked, but the object moved at a ridiculous rate (using the same calculation I'm doing now as well as entering in a small number manually).

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

0 Replies

· Add your reply
  • Sort: 

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

!URGENT! How to make a grid of clickable objects. 1 Answer

Why is 2d camera not smooth at 30 FPS 0 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

creating 2d table\chess board\2d array 2 Answers

How can i make the Unity2d grid use rounded numbers? 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