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 /
This question was closed Sep 04, 2015 at 03:35 AM by getyour411 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Blink · Aug 21, 2015 at 04:45 PM · playerdistancebooleantagtree

Distance from tagged objects problem

I'm trying to change my boolean to true if the player finds himself within a certain distance of any object tagged as "tree", ive searched for an answer everywhere but im still unsure as to what is causing the problem in my script. I don't get any errors however the boolean does not change regardless of the players distance with the tagged objects (which are definitely tagged). Can someone help me out please?

 var player : GameObject;
 var tree : GameObject[];
 var NearTree : boolean = false;
 
 function Awake () {
 
 tree = GameObject.FindGameObjectsWithTag("tree");
 }
 
 function Update () {
 
 var distance = Vector3.Distance(player.transform.position, tree.transform.position);
 
 if(distance > 5){
   NearTree = false;
 }
 
 if(distance < 5){
   NearTree = true;
 }
 }
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

  • Sort: 
avatar image
1
Best Answer

Answer by Positive7 · Aug 21, 2015 at 05:07 PM

Something like this (not tested) :

  #pragma strict
 var player : GameObject;        //Player GameObject
 var tree : GameObject[];        //Tree Array
 var dist : float[];             //Distane Array
 var NearTree : boolean[];        //Boolean Array        
 
 function Awake () { 
     tree = GameObject.FindGameObjectsWithTag("tree");   //Let's find all the tree
     dist = new float[tree.length];                         //Distance Array match with Tree Array size
     NearTree = new boolean[tree.length];                 //Boolean Array match with Tree Array size
 }
 
 function Update () {
 
    for (var i = 0; i < tree.Length; i++) {                    //For all Tree in the tree Array
    
              //Let's Find all distance (dist[i]) beetween player and all the tree (tree[i])
           dist[i]= Vector3.Distance(player.transform.position, tree[i].transform.position);
           
           if(dist[i] > 5){            
           
               NearTree[i] = false;
           }
           if(dist[i] < 5){                    //if any distance in dist Array is closer then 5 unit
           
               NearTree[i] = true;            //set NearTree bool to true for that tree
           }
           if(NearTree[i]){
               
           }
           if(Input.GetKeyDown("e") && NearTree[i] == true){   //If close to tree and "E" is pressed
               print("near tree and e has been pressed");
               }
            }
    }
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 Blink · Aug 21, 2015 at 06:47 PM 0
Share

This works perfectly, thanks a lot!

Ive stumbled upon another problem however through adding these lines of code, im not entirely sure how the script you posted works, can you explain how it does and why my new lines produce the error "array index is out of range"? thanks,

var player : GameObject; var tree : GameObject[]; var dist : float[]; var NearTree : boolean[];

function Awake () { tree = GameObject.FindGameObjectsWithTag("tree"); dist = new float[tree.length]; NearTree = new boolean[tree.length]; }

function Update () {

   for (var i = 0; i < tree.Length; i++) {
   
          dist[i] = Vector3.Distance(player.transform.position, tree[i].transform.position);
          
          if(dist[i] > 5){
          
              NearTree[i] = false;
          }
          if(dist[i] < 5){
          
              NearTree[i] = true;
          }
          if(NearTree[i]){
              
          }
      }
      
  if(Input.Get$$anonymous$$eyDown("e") && NearTree[i] == true){
    print("near tree and e has been pressed");

} }

avatar image Positive7 · Aug 21, 2015 at 07:49 PM 0
Share

Updated my answer! Your if(Input.Get$$anonymous$$eyDown("e") && NearTree[i] == true){ was outside of the for loop. That's why you got the error. [i] should be used inside for loop it iterates through the loop. A bit more about for loops : https://msdn.microsoft.com/en-us/library/ch45axte.aspx

avatar image Blink · Aug 21, 2015 at 09:10 PM 0
Share

Oh I see haha, well thats just about bang on, thankyou.

Follow this Question

Answers Answers and Comments

26 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

Related Questions

Terrain Tree view distance 1 Answer

"Player" tag collider from other GameObject? 1 Answer

changing objects color through a raycast? 1 Answer

Move player same distance every touch 2 Answers

Make Raycast ignore anything that "Isn't" my player(Solved) 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