- Home /
The question is answered, right answer was accepted
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;
}
}
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");
}
}
}
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");
} }
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