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 trueicecold · Aug 29, 2014 at 05:31 PM · animation2dmovement

Moving an object in 2d using keyboard causes inconsistent behavior

Hello all

I'm trying to move a sprite X pixels in each direction using the keyboard along 0.5 seconds.

my code so far:

 using UnityEngine;
 using System.Collections;
 
 public class Ball : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     }
 
     private float speed = 3f;
     private float movementX = 0;
     private float newPosX = 0;
 
     private bool isMoving = false;
 
     // Update is called once per frame
     void Update () {
         if (!isMoving)
         {
             movementX = 0;
             newPosX = 0;
             if (Input.GetKey(KeyCode.LeftArrow))
             {
                 isMoving = true;
                 movementX = -speed * Time.deltaTime;
                 newPosX = transform.position.x - renderer.bounds.size.x;
             }
             if (Input.GetKey(KeyCode.RightArrow))
             {
                 isMoving = true;
                 movementX = speed * Time.deltaTime;
                 newPosX = transform.position.x + renderer.bounds.size.x;
             }
         }
         transform.position = new Vector3(transform.position.x + movementX, transform.position.y, transform.position.z);
         if (transform.position.x >= newPosX)
         {
             isMoving = false;
             movementX = 0;
         }
     }
 }
 

Problem is the movement is kind of erratic. sometimes it's fast, sometimes it's slow as hell... I've tried taking deltaTime into account, but that didn't seem to help...

Any ideas? Please be easy, I'm a n00b when it comes to Unity :)

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

· Add your reply
  • Sort: 
avatar image
0

Answer by Pyrian · Aug 29, 2014 at 06:27 PM

You're not taking the DeltaTime into account correctly. As written, you collect the Time.deltaTime once per move, but you need it once per frame. I.e., next time Update is called, there's a new Time.deltaTime, but your MovementX has already been set and does not change. Try removing both " Time.deltaTime" from inside the if statement, and instead using it inside the "new Vector3" instead: "transform.position.x + movement Time.deltaTime".

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 trueicecold · Aug 29, 2014 at 06:56 PM 0
Share

Thanks Pyrian, it looks like it handled the "right" movement ok, but the "left" is still buggy after this fix:

 using UnityEngine;
 using System.Collections;
 
 public class Ball : $$anonymous$$onoBehaviour {
 
     // Use this for initialization
     void Start () {
     }
 
     private float speed = 3f;
     private float movementX = 0;
     private float newPosX = 0;
 
     private bool is$$anonymous$$oving = false;
 
     // Update is called once per frame
     void Update () {
         if (!is$$anonymous$$oving)
         {
             movementX = 0;
             newPosX = 0;
             if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.LeftArrow))
             {
                 is$$anonymous$$oving = true;
                 movementX = -speed;
                 newPosX = transform.position.x - renderer.bounds.size.x;
             }
             if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.RightArrow))
             {
                 is$$anonymous$$oving = true;
                 movementX = speed;
                 newPosX = transform.position.x + renderer.bounds.size.x;
             }
         }
         transform.position = new Vector3(transform.position.x + movementX * Time.deltaTime, transform.position.y, transform.position.z);
         if (transform.position.x >= newPosX)
         {
             is$$anonymous$$oving = false;
             movementX = 0;
         }
     }
 }
 
avatar image Pyrian · Aug 29, 2014 at 08:18 PM 0
Share

Hmm. Left movement is going to trigger is$$anonymous$$oving = false immediately, so you also need to fix "if (transform.position.x >= newPosX)".

$$anonymous$$aybe: if (((movementX > 0) && (transform.position.x >= newPosX)) || ((movementX < 0) && (transform.position.x

avatar image trueicecold · Aug 29, 2014 at 09:33 PM 0
Share

Thanks a lot Pyrian, it's works just fine now... now I'm going to try and figure out why my condition was wrong...

avatar image
0

Answer by kacyesp · Aug 29, 2014 at 07:07 PM

Can you substitute what you have on line 35 for this?

  if ( movementX <= 0 ) 
      isMoving = false;
  else 
  {
      transform.position = new Vector3(transform.position.x + movementX * Time.deltaTime, transform.position.y, transform.position.z);
      movementX -= movementX * Time.deltaTime;
 }
Comment
Add comment · Show 2 · 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 Pyrian · Aug 29, 2014 at 08:14 PM 0
Share

That's not a bad approach, but it's not going to work just dropped in like that.

avatar image trueicecold · Aug 29, 2014 at 09:30 PM 0
Share

Nope kacyesp, now it won't move to the left at all...

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Sprite animation 2 Answers

SpriteManager 2 1 Answer

Move Transform to Match 2d Animation 0 Answers

Beginner using the animation controller to get a sprite walking left and right..Need some help going from here 0 Answers

Rolling Barrels through Platforms 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