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 DevonJavaScript · Nov 24, 2011 at 10:32 PM · transformarrayvector3distancedynamic

if close to one of a kind?

Well I understand what I need to know as in the methods of Vector3.Distance(a,b); and also var x: Transform[] or in C# public Transform[] name;

but my question is how would I make it so if I am within distance of any of the array objects what I would imagine looks like: if(Vector3.Distance(object[1 - object.length],transform.position) < distance); but how would I say one of the objects in the array ?

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

Answer by aldonaletto · Nov 24, 2011 at 11:33 PM

Your question is very confusing, but I suppose you want to check the distance from one specific element of the array, or from any elements. If you want to verify the distance from the element n of the array, you should use:

   if (Vector3.Distance(object[n].position, transform.position) < distance){
     // do something
   }

But if you want to check all elements in the array, you should use:

   for (var i = 0; i < object.length; i++){
     if (Vector3.Distance(object[i].position, transform.position) < distance){
       // do something
     }
   }

If you want to know if you're closer than distance to any object in the array, you can use a boolean flag:

   var near = false;
   for (var i = 0; i < object.length; i++){
     if (Vector3.Distance(object[i].position, transform.position) < distance){
       near = true;
     }
   }
   if (near){
     // do something
   }


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 DevonJavaScript · Nov 26, 2011 at 12:44 AM 0
Share

Thank you Spot on!!!!!

avatar image
1

Answer by kilguril · Nov 25, 2011 at 01:16 AM

To access a member of an array, simply use an index value for the item you wish to access - i.e. object[0], object[1], object[object.Length]. Also, keep in mind array indices start from 0 and not 1. So if you had an array of 3 items, the index range would be from 0 to 2.

As for testing against all items in an array, use a simple for loop to iterate over all items - see wikipedia for reference

 C#
 Transform[] arrayOfTransforms;
 
 for (int i=0; i < arrayOfTransforms.Length; i++)
 {
     arrayOfTransforms[i] // This will access the array at position "i" which iterates over the entire array
 }

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

Find gameobject furthest from player along z axis 0 Answers

Reposition to minimum distance from target 2 Answers

How do I find the farthest verts of a mesh relative to its object position and store them in an array 1 Answer

Keep camera at certain distance from character when rotating 3 Answers

Dynamic Enemy Transform List 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