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 sushanta1991 · Mar 11, 2013 at 04:43 PM · movetowards

Any one know maths behind this MoveTowards() function ?

Hi guys any one know, how this MoveTowards() function works ? whats the maths behind this function ?

I was trying to create a function to move my character from current position to a target position in X,Y plane, Let me tell you i am not good in maths. And than i found this function which does the same thing cheers. but i would love to know the maths behind this function. So, Please help me understand whats happening in this MoveTowards() function.

thanks

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

3 Replies

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

Answer by Bunny83 · Mar 11, 2013 at 04:57 PM

This is the actual code behind the function ;)

 // Vector3.MoveTowards
 public static Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta)
 {
     Vector3 a = target - current;
     float magnitude = a.magnitude;
     if (magnitude <= maxDistanceDelta || magnitude == 0f)
     {
         return target;
     }
     return current + a / magnitude * maxDistanceDelta;
 }

Just in case you didn't ment Vector3.MoveTowards, here's Mathf.MoveTowards:

 // Mathf.MoveTowards
 public static float MoveTowards(float current, float target, float maxDelta)
 {
     if (Mathf.Abs(target - current) <= maxDelta)
     {
         return target;
     }
     return current + Mathf.Sign(target - current) * maxDelta;
 }
Comment
Add comment · Show 6 · 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 FLASHDENMARK · Mar 11, 2013 at 05:20 PM 0
Share

How do you access this code? I'm pretty sure you are getting this code from the UnityEngine/UnityEditor DLL? But how do you open that DLL file?

$$anonymous$$aybe I am wrong, but whatever I try, I get a bunch of incomprehendable text when trying to open the DLLs.

avatar image Bunny83 · Mar 11, 2013 at 05:25 PM 1
Share

Well, either with the Assembly browser that's integrated in $$anonymous$$onoDevelop (but it's poorly integrated and fails a lot), or use the open source tool ILSpy which is just another .NET-Reflector which supports C#, Visual Basic and, of course, IL (the intermediate assembly language).

avatar image sushanta1991 · Mar 11, 2013 at 05:40 PM 0
Share

Thanks @Bunny83 your answers really explains what happening inside the $$anonymous$$oveTowards() function.

avatar image FLASHDENMARK · Mar 11, 2013 at 06:06 PM 0
Share

Ah, many thanks.

The ILSpy tool works great!

avatar image ChristoferLV · Feb 01, 2020 at 03:07 AM 0
Share

What is maxDelta?

avatar image Mashimaro7 ChristoferLV · Apr 24, 2021 at 02:13 PM 0
Share

I know this is super late, but in case you're still wondering, maxDelta is essentially the speed.

avatar image
1

Answer by robertbu · Mar 11, 2013 at 04:48 PM

The basic calculation is:

 newPos = startPos + (endPos - startPos).normalized * maxDist;

If you want it to go no further than the endPos, you will need to clamp the distance you use.

Comment
Add comment · Show 1 · 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 sushanta1991 · Mar 11, 2013 at 05:44 PM 0
Share

thanks for answering @robertbu I really appreciate your answer :)

avatar image
1

Answer by Statement · Mar 11, 2013 at 04:52 PM

It figures out the direction you are moving towards, and adds direction * maxDistanceDelta. If the remaining distance is less than maxDistanceDelta, then it simply returns the target.

 if (remainingDistance <= maxDistanceDelta)
     return target;
 else 
     return current + directionToTarget * maxDistanceDelta;

That's pretty much how it works.

Comment
Add comment · Show 1 · 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 sushanta1991 · Mar 11, 2013 at 05:42 PM 0
Share

thanks for answering this my question @Statement :) i got it now.

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

17 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

Related Questions

Lerp bugging for- and backwards 2 Answers

Move Camera towards Object 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