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 /
  • Help Room /
avatar image
0
Question by BenHeimberg · Feb 01, 2017 at 10:58 AM · vector3distancevector3.distance

Vector3.Distance - Triggering an action on distance

Hello All! I`m working on a project of mine, and i feel kind of lost. Here is the scenario: I have a player who walks around, and some Cube objects around him. I want to measure the distance between the player and the objects, and when the player gets close enough to any of the objects then trigger a popup window saying "Press X button to do Y".

Now on every object i have a - public float minDist; and also - public float currDist; Obviously I`m using: Vector3.Distance to measure the distances between the player and the objects, and set currDist by it.

When currDist is lower when minDist I want the trigger to happen, but for some reason i found that only 1 object sets the trigger and not all of them..

Any ideas please?

Code:

 public float minDist;
 public float currDist;
 
 void Update () {
 
     currDist = Vector3.Distance (transform.position, Player.transform.position);
             if (currDist < minDist) {
                 //Enable Trigger
             }
             else if (currDist > minDist){
                 //Disable Trigger 
             }
 
 }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by HenryStrattonFW · Feb 01, 2017 at 01:24 PM

This will be because all of the items are running at the same time, and whilst one object will run, enable the trigger, and carry on, another item will immediately say "I'm too far away" and disable the trigger again.

IO would suggest storing a value in the objects, when they enable the trigger, something like "ResponsibleForTrigger" and only let them disable the trigger, if this flag is set. In other words any object can enable the trigger, but only an object that has enabled the trigger can disable it.

Hope this makes sense.

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 BenHeimberg · Feb 01, 2017 at 02:03 PM 0
Share

First of all Thank you @HenryStrattonFW for the quick support, i think i understand your point, but i cant get it to work. $$anonymous$$aybe can you provide a small code for it?

$$anonymous$$any thanks!

avatar image BenHeimberg · Feb 01, 2017 at 02:20 PM 0
Share

@HenryStrattonFW finally i succeed! Thank you very much! Understood your point very clearly :)

avatar image HenryStrattonFW BenHeimberg · Feb 01, 2017 at 05:52 PM 0
Share

Hey, sorry answered on my lunch break so wasn't able to respond with an example, but glad to hear you worked it out. Best of luck with your project.

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

100 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

Related Questions

Don't trust "Vector3.Distance"? 1 Answer

Error with vector3.distance 0 Answers

Lerp to a fixed distance 0 Answers

Distance between two objects, from the view of the camera. 0 Answers

Calculating distance is not working 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