Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
0
Question by Crystalline · Apr 20, 2015 at 02:39 PM · vectorcompareaverage

Compare Vector3

Hi,how do I compare 2 vectors? Ex: if vectorone is almost the same as vectortwo by a percentage then do something;

I want to be able to do something if the vectors are almost the same , the error being another variable.

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

Answer by lordlycastle · Apr 20, 2015 at 03:43 PM

You can use Distance as @Sarthak123 showed, that would be the simplest, but won’t give more control over what you wanna do. Writing you own function should be quite simple. You could do something like

 //With absolute value
 public bool Approximately(Vector3 me, Vector3 other, float allowedDifference)
         {
             var dx = me.x - other.x;
             if (Mathf.Abs(dx) > allowedDifference)
                 return false;
 
             var dy = me.y - other.y;
             if (Mathf.Abs(dy) > allowedDifference)
                 return false;
 
             var dz = me.z - other.z;
 
             return Mathf.Abs(dz) >= allowedDifference;
         }
 //With percentage i.e. between 0 and 1
 public bool Approximately(Vector3 me, Vector3 other, float percentage)
         {
             var dx = me.x - other.x;
             if (Mathf.Abs(dx) > me.x * percentage)
                 return false;
 
             var dy = me.y - other.y;
             if (Mathf.Abs(dy) > me.y * percentage)
                 return false;
 
             var dz = me.z - other.z;
 
             return Mathf.Abs(dz) >= me.z * percentage;
         }

You can also write a function that take in a vector to compare in a similar fashion, so you’ll be able to send different values for each element.

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 Crystalline · Apr 20, 2015 at 03:51 PM 0
Share

Thanks a lot, your solution is really useful and what I was looking for!

avatar image Bonfire-Boy · Apr 20, 2015 at 04:31 PM 0
Share

$$anonymous$$inor bugbear of $$anonymous$$e... it's best not to use the word "percentage" when it varies between 0 and 1, because it can confuse the hell out of collaborators and lead to bugs. The word "fraction" does the job.

avatar image TheCellCH · Nov 18, 2019 at 10:53 AM 0
Share

This function as written like this returns the wrong answer doesn't it? We send back false if the distance is higher for x or y this meaning if x or y is not close the function returns what we expect. But for z the distance has to be further away to be true, which is the opposite of what we want.

Correct me if I'm mistaken here.

avatar image proandrius · May 09, 2020 at 04:33 PM 0
Share

It returns wrong answer.

It should be this at the end:

 if ($$anonymous$$athf.Abs(dz) > allowedDifference)
     return false;
 
 return true;
avatar image aleksei_sobolevskii · Nov 18, 2021 at 07:42 PM 0
Share

This is a great answer but not quite right. In the variant with percentage, when you compare abs delta for an axis with a percentage value, the percentage part should also be abs. Consider comparing vectors with negative values.

avatar image
4

Answer by Hrungdak · Apr 20, 2015 at 02:44 PM

You can use Vector3.Distance(firstVector, secondVector). It returns a float value that gives you the distance between two coordinates in Unity3d-Units.

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 Crystalline · Apr 20, 2015 at 03:31 PM 0
Share

Thanks It should work.

avatar image
2

Answer by Sarthak123 · Apr 20, 2015 at 03:10 PM

It depends on how you want it to be, according to your problem, this might help -

 //UnityScript
 
 var vctr1 : Vector3; // first vector
 var vctr2 : Vector3; // second vector
 var factor : float; // Distance you want between 2 vectors.
 var dis : float; // distance between 2 vectors
 
 function Calc()
 {
 dis = Vector3.Distance(vctr1, vctr2); // Calculating Distance
 if(dis <= factor) // checking if distance is less than required distance.
 {
 //do something
 }
 }

However, if you want to calculate/check on only a single axis, like if you want to know if player has crossed destination in Z-axis, i.e., if player is ahead of destination, then this will be more effective -

 if(player.position.z > destination.position.z)
 {
 //do something
 }

The above process can be used for any axis. Finally, it depends on how you want it, and how you use it.

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 Crystalline · Apr 20, 2015 at 03:30 PM 0
Share

Thanks this should work !

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

25 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

Related Questions

Comparing two coordinates in a Vector3 2 Answers

compare 2 vector3 variables 1 Answer

On which side of the object is this point - 2D 0 Answers

Is it clockwise or counter-clockwise for positive angle for Vector2.SignedAngle? 1 Answer

How to get the tangent and normal of a collision? 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