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
1
Question by 8r3nd4n · Jul 30, 2013 at 11:44 AM · movementpositionvector3range

Check if 2 objects positions are close enough

How would I go about checking to see if two objects positions are pretty close to each other? The reason being is that I have a grid based movement for my player piece and when it moves onto certain pieces, I want to check to see if their position is pretty much the same so I can then do certain functionality relating to that grid piece position. I say pretty much because as the piece moves now in set distances, it is never in a whole number (say its x position is 0, then when it moves 1 unit to the left, it becomes 0.9998 or 1.0002, etc.)

What I think would be the way to do it would be:

 if(player.position.x >= grid.position.x - 0.1 || player.position.x <= grid.position.x + 0.1)
 {
 canDoStuff = true;
 }

Then repeat for the y and z co-ordinates.

This seems like a long way around and was wondering if there was a unity function (min/max range?) that would make this easier?

So it would be in pseudocode: if player.position.x == grid.position.x give or take a few.

Cheers.

Comment
Add comment · Show 1
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 Fattie · Jan 19, 2016 at 07:30 PM 0
Share

Check out "Vector3.Distance" in the manual. Is there a chance you could first check the many duplicates of this question to see what your issue is; you'll almost certainly find it!

2 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by RPGstandard · Jul 30, 2013 at 11:47 AM

http://docs.unity3d.com/Documentation/ScriptReference/Vector2.Distance.html

this should help

Comment
Add comment · 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
1

Answer by 8r3nd4n · Jul 30, 2013 at 11:03 PM

Yes!

Thank you. Can't believe I didn't think of this as I have used it countless times before in different scenarios.

Comment
Add comment · 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

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

18 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

Related Questions

How to smooth movement object in one-axis? 1 Answer

2D Background Stop Moving on BoundingBox Exit? 2 Answers

Moving an Object to the vector of other objects on button press using a vector3 array? 0 Answers

Setting target position in Vector3.MoveTowards 2 Answers

How do i make an object move unpredictably? 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