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
3
Question by iamthecoolguy11 · Sep 09, 2014 at 02:52 AM · c#2dmovetowards

How do I use MoveTowards in 2d?

I am trying to get this object to move toward the position slowly and stop moving when it is there.

here is the script

 public Vector2 aPosition0 = new Vector2(0, 0);
 
 public Vector2 aPosition1 = new Vector2(0, 0);
      
     void Update () 
         {
             Vector2.MoveTowards(aPosition0, aPosition1, 10 * Time.deltaTime);
         }



Comment
Add comment · Show 3
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 iamthecoolguy11 · Sep 09, 2014 at 03:13 AM 0
Share

going to be a it slow to respond because I well probably be asleep

avatar image Jeric-Miana · Sep 09, 2014 at 03:30 AM 1
Share

Try this one brother! hope it helps

         private  Vector3 target ;
         public  GameObject position1;
         public float speed =1 ;
     
     
     
         void Update () 
         {
             target = position1.transform.position;
             target.z = transform.position.z;
             transform.position = Vector3.$$anonymous$$oveTowards(transform.position, target, speed  * Time.deltaTime);
             
         }

avatar image iamthecoolguy11 · Sep 09, 2014 at 11:38 AM 0
Share

I took out the transform.position but I just added it back in and it seemed to work.

1 Reply

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

Answer by iamthecoolguy11 · Sep 09, 2014 at 11:40 AM

Ok I got it working here is it fixed

 public Vector2 aPosition1 = new Vector2(3,3);
 
 void Update () 
     {
             transform.position = Vector2.MoveTowards(new Vector2(transform.position.x, transform.position.y), aPosition1, 3 * Time.deltaTime);
     }
Comment
Add comment · Show 4 · 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 alex-dacol · Aug 05, 2015 at 12:53 PM 0
Share

thanks m8 , really helped !

avatar image Blaugrana · Jan 14, 2018 at 11:56 AM 0
Share

3 days searching for the right way to use $$anonymous$$oveTowards thanks a lot mate!

avatar image LeoFigs · Jun 20, 2018 at 05:53 PM 0
Share

This works great for what I'm trying to do, except I want the transform to move only along the x-axis. right now the transform is following the player even along the y-axis, when the player is jumping, but I want it to stay on the ground. How can I set his y-position to a constant value?

  private void Update()
     {
         playerPos = new Vector2(Player.position.x, Player.position.y);
         float playerDistance = Vector2.Distance(transform.position, Player.position);
 
         print("Distance from Enemy to Player: " + playerDistance);
 
         if (playerDistance <= $$anonymous$$axDist)
         {
             transform.position = Vector2.$$anonymous$$oveTowards(new Vector2(transform.position.x, transform.position.y), playerPos, $$anonymous$$oveSpeed * Time.deltaTime);
            // transform.position = new Vector2(Player.position.x, transform.position.y) *$$anonymous$$oveSpeed * Time.deltaTime;
         }
     }
avatar image Coder104 LeoFigs · May 13, 2020 at 04:03 PM 0
Share

I know this is really late but you could just add a Rigidbody2D to the desired object. This is stupid because you probably found an answer so yeah... bye

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

26 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

Related Questions

What is wrong with my Counter Script? (C#) 1 Answer

Distribute terrain in zones 3 Answers

Using Texture Assets in Code 1 Answer

Multiple Cars not working 1 Answer

How to update assigned vector variable in unity C# 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