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 /
avatar image
1
Question by ZhavShaw · Aug 07, 2017 at 12:34 AM · rigidbodyvector3distance

Check if object is moving towards a point

Greetings! I'm looking to find a solution to check if an object is moving towards a vector3 position (or another object's position). Basically if the distance is decreasing, return true, else if it's increasing, return false.

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
3
Best Answer

Answer by nt314p · Aug 07, 2017 at 03:24 PM

Thanks to Elthen for giving the Vector3.distance idea.

Put this code at the top:

 public float dist;

And this function at the bottom:

 public bool CheckDirection () {
     float distTemp = Vector3.Distance(destination.position, transform.position);
     if (distTemp < dist) {
         dist = distTemp;
         return true;
     } else if (distTemp > dist) { // rigorous checking
         dist = distTemp;
         return false;
     }
 }

Make sure the function is being called constantly (I am assuming it will be)

Basically it checks if the distance last frame was further or closer than the distance this frame, and then returns the desired value.

Tell me if it doesn't work and I can see what I can change.

Sorry if you wanted it in Java, but hopefully you get the main idea of the code.

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 ZhavShaw · Aug 07, 2017 at 08:49 PM 0
Share

Yes this is exactly what I needed, but if we're returning true or false, we wouldnt reach the last line. Thank you so much for the help!

avatar image nt314p ZhavShaw · Aug 08, 2017 at 09:27 PM 0
Share

O$$anonymous$$, I changed the code so that the dist = distTemp will run if you haven't figured out a workaround already (I'm sure you have).

avatar image EDartBlade · Oct 25, 2020 at 03:12 PM 0
Share

So at least 2 frames are required...

avatar image
1

Answer by Elthen · Aug 07, 2017 at 01:49 PM

Hmmm not sure if this would help; anyway, you could keep track of the distance by using Vector3.Distance() inside an Update() to check the distance between two Vector, like so:

     if (Vector3.Distance(destination.position, transform.position) > 0.5f)
     {
     //do something
     }

See: https://docs.unity3d.com/ScriptReference/Vector3.Distance.html

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 ZhavShaw · Aug 07, 2017 at 08:49 PM 0
Share

Hey, thanks for that!

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

102 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

Related Questions

Enemy AI help with height check 1 Answer

rigidbody.velocity.normalized application 1 Answer

Closest point on multiple lines? 2 Answers

Javascript to C#: Vector 3 rigidbody 1 Answer

Constrain rigidbody movement to spline at high speeds / sharp curves 3 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