can't use Vector3.Distance with object at origin(0,0,0)
I am using a lot of Vector3.Distance in my script, but I do not think it is too much that it is causing my current problem - which I think may be unity's fault? but I may just be ignorant on this one. One specific Vector3.Distance involves using a GameObject at the origin point (0,0,0). I know it does not work because I am using the vector3.distance to destroy any 'floor' outside that point of space, and the 'floor' at the origin point is the only one not being Destroyed(which the obvious distance is too far away that it should have been destroyed. I even walked passed it- forming the floors around it again(none were created in it)(Line 70 is where the Vector3.Distance is).
![using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BuildManager : MonoBehaviour {
public List<GameObject> floorsMade = new List<GameObject>();
public List<Vector3> floorSpotsAroundPlayer = new List<Vector3>();
public List<bool> floorBool = new List<bool> ();
public GameObject player;
public GameObject floor;
public Vector3 currentFloor;
public Vector3 previousFloor;
public bool startGame;
void Start () {
startGame = false;
player = GameObject.Find ("Player");
floorSpotsAroundPlayer.Add (new Vector3 (0, -50, 0));
floorSpotsAroundPlayer.Add (new Vector3 (30, -50, 0));
floorSpotsAroundPlayer.Add (new Vector3 (0, -50, 30));
floorSpotsAroundPlayer.Add (new Vector3 (-30, -50, 0));
floorSpotsAroundPlayer.Add (new Vector3 (0, -50, -30));
floorSpotsAroundPlayer.Add (new Vector3 (-30,-50, -30));
floorSpotsAroundPlayer.Add (new Vector3 (30, -50, 30));
floorSpotsAroundPlayer.Add (new Vector3 (-30, -50,30));
floorSpotsAroundPlayer.Add (new Vector3 (30, -50, -30));
ClearFloorBool ();
}
void Update(){
StartGame ();
CheckFloorChange ();
}
public void StartGame(){
if (!startGame) {//CREATES THE BASE 9 SQUARES THAT STARTS THE GAME AND ADDS THEM TO floorsMade
startGame = true;
GameObject temp = Instantiate (floor, floorSpotsAroundPlayer [0], Quaternion.identity) as GameObject;
temp.transform.parent = gameObject.transform;
floorsMade.Add (temp);
previousFloor = GameObject.Find ("GameManager").GetComponent<GameManager> ().currentFloor;
}
}
public void CheckFloorChange(){
currentFloor = GameObject.Find ("GameManager").GetComponent<GameManager> ().currentFloor;
if (currentFloor != previousFloor) {
previousFloor = currentFloor;
foreach (GameObject floor in floorsMade) {//FIND FLOORS SPOTS FILLED,FILL THEM WITH TRUE IN floorBool
for (int i = 1; i < 9; i++) {
Vector3 tempvect = new Vector3 (floor.transform.position.x - currentFloor.x,-50,floor.transform.position.z - currentFloor.z);
if (tempvect == floorSpotsAroundPlayer[i]) {
floorBool [i - 1] = true;//MINUS ONE BECAUSE SKIPPED ONE IN THE 'FOR' STATEMENT, WHICH IS EQUAL TO CURRENT FLOOR
}
}
}
for (int i = 0; i < 8; i++) {
if (!floorBool [i]) {//INSTANTIATE FLOORS IN false SPOTS IN floorBool
//FLOORS ARE 100 HIGH AND NEED -50 FOR Y FOR PROPER PLACEMENT
Vector3 tempVect = new Vector3 (currentFloor.x + floorSpotsAroundPlayer [i + 1].x, floorSpotsAroundPlayer [i + 1].y, currentFloor.z + floorSpotsAroundPlayer [i + 1].z);
GameObject temp = Instantiate (floor, tempVect, Quaternion.identity) as GameObject;
temp.transform.parent = gameObject.transform;
floorsMade.Add (temp);
}
}
ClearFloorBool ();
for (int i = floorsMade.Count - 1; i > 0; i--) {//DELETE FLOORS THAT ARE TOO FAR AWAY. BETTER EFFECT AND TOO FAR AWAY IS USLESS
if (Vector3.Distance (currentFloor,floorsMade[i].transform.position) > 120) {
Destroy (floorsMade[i]);
floorsMade.RemoveAt (i);
}
}
}
}
public void ClearFloorBool(){
floorBool = new List<bool> ();
floorBool.Add (false);
floorBool.Add (false);
floorBool.Add (false);
floorBool.Add (false);
floorBool.Add (false);
floorBool.Add (false);
floorBool.Add (false);
floorBool.Add (false);
}
}][1]
(the lone square is at the origin point)
[1]: /storage/temp/63393-capture.png
I did try reversing the two inside the Vector3.Distance, but that still came up with the same results.
Try printing this two Vector3(positions) to console. See what showing up
Hmmm ... it's a really basic thing that distance(A,B) is the same as distance(B,A). And there's nothing special at all about the point (0,0,0) -- distance to/from (0,0,0) is never a problem.
I'd say, since you we're even thinking that way, restart with some very simple stuff, display as much as you can (as Vagonn suggests) and just get a feel for how it works. You'll probably discover a handful of things that don't work the way you thought they did, and see "what was I thinking" spots in your code above.
Your answer

Follow this Question
Related Questions
vector 3 to unity.transform 1 Answer
How limit the movement using distance 2 Answers
Vector3.Distance returning offset values due to down force interference. 1 Answer
Distance interaction 0 Answers
Lerp to a fixed distance 0 Answers