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
0
Question by ZisGuy · Oct 21, 2011 at 04:53 AM · c#transformvector3distancemultiple

Check distance between multiple objects

What I'm trying to do is to make a script in C# that uses a function when the distance of any "People" is closer than 0.5 to the object this script is attached to. This is the (important part of) the script.

 using UnityEngine;
 using System.Collections;
 
 public class PersonScript : MonoBehaviour {
     
     public Transform[] People;
     
     void WhenGenerate(){        
         Debug.Log("Generating...");        
     }
     
     void Update () {    
         if(Vector3.Distance(People.position, transform.position) < 0.5){
             WhenGenerate();
         }
     }
 }

It doesn't work :( Help?

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
4

Answer by syclamoth · Oct 21, 2011 at 05:10 AM

Given that you already have an array full of people, the rest is easy!

Where you have

 if(Vector3.Distance(People.position, transform.position) < 0.5){
     WhenGenerate();
 }

instead, do this:

 bool peopleNear = false;
 foreach(Transform person in People)
 {
     if(Vector3.Distance(person.position, transform.position) < 0.5f)
     {
         peopleNear = true;
         break;
     }
 }
 if(peopleNear)
 {
     WhenGenerate();
 }

You almost had it, it's just that you need loops to be able to access every member of a collection like that.

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 syclamoth · Oct 22, 2011 at 07:19 AM 0
Share

Do you want it to poll at random intervals? Or do you want it to wait a random time after it has detected people, and then do the generation bit? I'm not sure what you are asking here. Depending on how you do it, the counter can act however you need it to!

avatar image ZisGuy · Oct 22, 2011 at 05:40 PM 0
Share

I'm talking about making it wait after it has detected the people, and before it has done WhenGenerate(). And I want it to wait a random amount of time before calling WhenGenerate()

avatar image syclamoth · Oct 23, 2011 at 04:51 AM 0
Share

Well then, that would be a bit different, but not very different. Ins$$anonymous$$d of just calling WhenGenerate, put it inside a coroutine, like this-

 IEnumerator WaitAndGenerate()
 {
     yield new WaitForSeconds(Random.Range(5, 10));
     if(CheckForPeople())
     {
         WhenGenerate();
     }
 }

Then put your proximity detection bit into a separate script, like this-

 bool CheckForPeople(){
     // the code from earlier, ending with
     return peopleNear;
 }

Then, probably don't put it in Update, make it trigger when the player meets certain conditions.

avatar image
0

Answer by BerggreenDK · Oct 22, 2011 at 07:10 PM

You could go another direction with your code.

In the Physics class, there is a function called CheckSphere that takes a radius as parameter.

http://unity3d.com/support/documentation/ScriptReference/Physics.CheckSphere.html

another approach could be this:

http://unity3d.com/support/documentation/ScriptReference/Physics.OverlapSphere.html

where you get a list of the colliders it overlaps.

Think of these functions as a cookie-cutter-shape that you place over your "world" and the inside of the form is your list of objects that matches your query.

I dont know if this function is more expensive than the other approach, but I would use it for a test, if I needed what you describe.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Decrease value at half the speed it's increased at? (using Vector3.Distance) 1 Answer

How to change Vector3 if an objects rotation is a certain number? 1 Answer

How to apply in opposite direction to transform? 1 Answer

Calculate the distance between an object and my player 1 Answer

C#: Change transform.position in code 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