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 /
  • Help Room /
avatar image
1
Question by MiroJankura · Mar 14, 2019 at 10:35 AM · movementcoroutinemouseclickmovetowards

Move object to another position after Mouse Click with MoveTowards function

I want to achieve smooth object move from actual position to another position after mouse click. I'm using MoveTowards function in Update method.

Another position is defined as empty object attached to parameter "wayPoint"

Problem was, that after mouse click has object moved just in one frame towards destination position. So I tried to prevent it with Coroutine, but even with that is result still the same. An object is still moving just in one frame and not the whole path from start to destination. And I have to click every frame to move it towards destination.

My code is below. Does anybody know, where can be a problem? Thanks for any response.

 [SerializeField] float speed = 20f;
 [SerializeField] Transform wayPoint;
 
 void Update()
     {
         if (Input.GetMouseButtonDown(0))
         {
             StartCoroutine(MoveTo(transform.position, wayPoint.transform.position, speed));
         }
     }
 
 IEnumerator MoveTo(Vector2 start, Vector2 destination, float speed)
     {
            transform.position = Vector2.MoveTowards(start, destination, speed * Time.deltaTime);
            yield return null;
     }

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

1 Reply

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

Answer by Hellium · Mar 14, 2019 at 11:30 AM

MoveTowards is not a magic function that automatically move an object over time. It's just a function helping you determining a new position between two points.

Calculate a position between the points specified by current and target, moving no farther than the distance specified by maxDistanceDelta


  void Update()
  {
      if (Input.GetMouseButtonDown(0))
      {
          StopAllCoroutines();
          StartCoroutine(MoveTo(transform.position, wayPoint.transform.position, speed));
      }
  }
  IEnumerator MoveTo(Vector2 start, Vector2 destination, float speed)
  {
     while( (transform.position - destination).sqrMagnitude > 0.01f )
     {
         transform.position = Vector2.MoveTowards(transform.position, destination, speed * Time.deltaTime);
         yield return null;
     }
  }
Comment
Add comment · Show 5 · 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 MiroJankura · Mar 14, 2019 at 12:31 PM 0
Share

Thanks for answer @Hellium

Yes, that makes sense. I added WHILE loop to the coroutine, but unfortunately, results are still the same. Update is still waiting for new mouse click every frame to move it closer to the destination. So it's moving just a small piece every frame.

avatar image Hellium MiroJankura · Mar 14, 2019 at 12:51 PM 0
Share

$$anonymous$$y bad, I've stupidly copy-pasted your code without realizing that the 1st argument of $$anonymous$$oveTowards is the current position of the object. Answer fixed, and solution tested in empty project.

avatar image MiroJankura · Mar 14, 2019 at 01:33 PM 0
Share

Works perfectly now. Thank you @Hellium. I very appreciate your help. I would probably spend days to solve it.

avatar image Hellium MiroJankura · Mar 14, 2019 at 01:42 PM 0
Share

Glad it worked. Please, don't forget to click on the "Accept" button below the answer to indicate your problem has been solved.

avatar image MiroJankura · Mar 14, 2019 at 02:04 PM 0
Share

@Hellium. Accepted. Sorry, I'm newbie here.

I also noticed that "start" config parameter in coroutine declaration is now unnecessary and can be removed completely (also in Update, where coroutine is called). Because it's not used anywhere. So "destination" and "speed" parameters are enough to run this.

Just for clarification for someone in the future.

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

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

The object moves more that what I want (MoveTowards) 0 Answers

Objects slowly move by their own? 1 Answer

Hit a wall in my RTS movement controller. Issues with MoveToward and Coroutine logic. 0 Answers

MoveTowards resets position 1 Answer

A gameobject(cylinder) moves along with the Hand tool (top-left corner) when right clicked, even when the gameobject is not selected 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