• 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 blablobloo · May 01, 2015 at 04:54 PM · movetowards

MoveTowards not working

So what I'm trying to do is to make the object attached to the script to move to certain positions on key presses. I'm using coroutines but it's not working, the object doesn't move at all.

 // Update is called once per frame
     void Update () {
         if (!Input.anyKeyDown){
             //transform.position = new Vector3(0,-0.5f,0);
             StartCoroutine(moveTo(new Vector3(0, -0.5f, 0)));
         }
         if (Input.GetKeyDown(KeyCode.RightArrow)) {
             //transform.position = new Vector3(2, -0.5f, 0);
             StartCoroutine(moveTo(new Vector3(2, -0.5f, 0)));
         }
         if (Input.GetKeyDown(KeyCode.LeftArrow)) {
             //transform.position = new Vector3(-2, -0.5f, 0);
             StartCoroutine(moveTo(new Vector3(-2, -0.5f, 0)));
         }
     }
     
     IEnumerator moveTo(Vector3 pos) {
         while((transform.position.x != pos.x) && (transform.position.y != pos.y)) {
             transform.position = Vector3.MoveTowards (transform.position, pos , speed * Time.deltaTime);
             yield return null;
         }    
     }

The commented parts work, but that way the object "teleports" to that position instead of the smooth movement I desire. I took the code from one of the answers here http://answers.unity3d.com/questions/640728/move-an-object-using-getkeydown-gradually.html and modified it just a bit so that it worked for me, but it isn't working. Help would be very appreciated

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 blablobloo · Apr 30, 2015 at 05:55 PM 0
Share

I forgot something that is probably important: The behavior I'm looking for is that if there is no key being pressed, object remains at (0, -0.5f, 0), and if left/right is pressed, move to (-2, 0.5f, 0)/(2, 0.5f, 0) respectively, and I want the object to remain at that position as long as the keys are being pressed. And then the object should return to (0, -0.5f, 0) if key is lifted.

avatar image Sessional · May 01, 2015 at 04:59 PM 0
Share

How high is your speed variable set?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by blablobloo · May 03, 2015 at 12:12 AM

I managed to fix the issue, it was very silly but oh well, I'm posting the solution for anyone having a similar problem.

 void Update () {
         if (!Input.anyKey){
             StartCoroutine(moveTo(new Vector3(0, -0.5f, 0)));
         }
         if (Input.GetKey(KeyCode.RightArrow)) {
             StartCoroutine(moveTo(new Vector3(3, -0.5f, 0)));
         }
         
         if (Input.GetKey(KeyCode.LeftArrow)) {
             StartCoroutine(moveTo(new Vector3(-3, -0.5f, 0)));
         }
     }
     
     IEnumerator moveTo(Vector3 pos) {
         transform.position = Vector3.MoveTowards (transform.position, pos , speed * Time.deltaTime);
         yield return null;   
     }

Comment
Add comment · 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
0

Answer by LT23Live · May 02, 2015 at 01:29 AM

I had the same issue so I tried this instead transform.Translate((Vector3.forward speed) Time.deltaTime);

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Lerp bugging for- and backwards 2 Answers

MoveTowards 1 Answer

Vector2.MoveTowards giving me a hard time 1 Answer

Camera moveTowards stuttering in place? 1 Answer

slow stop at position get from an httpRequest (www) 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