- Home /
Physics.OverlapSphere not giving proper coordinates
So Im trying to use Physics.OverlapSphere to get a set of objects around the object with the attached script. when testing its showing their locations to be exactly the same, I cant figure out why, please help.
///////////////////////////////////////////////////////////////////////////// Update Class /////////////////////////////////////////////////////////////////////////////
private List<GameObject> rootNodeList;
private GameObject curRoot = null;
private GameObject nextRoot = null;
void Awake()
{
rootNodeList = new List<GameObject>();
}
void Update ()
{
int val = Random.Range(0, rootNodeList.Count);
curRoot = rootNodeList[val];
if (curRoot == null) { Debug.Log("Error:current Root is Null"); }
curRoot.GetComponent<RootNode>().findLinks();
nextRoot = curRoot.GetComponent<RootNode>().pickNextNode();
if (nextRoot == null) { Debug.Log("Notify:current Root has no connections"); }
curVec = nextRoot.transform.position - curRoot.transform.position;
Debug.Log(nextRoot.transform.position);
Debug.Log(curRoot.transform.position);
}
///////////////////////////////////////////////////////////////////////////// RootNode Class /////////////////////////////////////////////////////////////////////////////
private List<GameObject> linkedNodes;
void Awake()
{
linkedNodes = new List<GameObject>();
}
public void findLinks()
{
Collider[] linkedObjects = Physics.OverlapSphere(this.transform.position, linkRadius);
Debug.Log(linkedObjects.Length);
for (int i = 0; i < linkedObjects.Length; i++)
{
if (linkedObjects[i].tag == "ROOTNODE")
{
linkedNodes.Add(linkedObjects[i].gameObject);
}
}
}
public GameObject pickNextNode()
{
int val = Random.Range(0, linkedNodes.Count);
if (linkedNodes.Count <= 0) { Debug.Log("Notify: there is no Nodes around"); }
return linkedNodes[val];
}
Nothing is jumping out at me on this - after 14 in RootNode maybe do Debug.log(linkedObjects[i].tranform.position.ToString()) to see if the bounding volumes are reporting the same t.pos
Thats what seems to be happening, the positions are the same and i cant figure out why its happening.
Answer by wibble82 · Feb 18, 2014 at 08:54 AM
The only cause I can think of is if linkedObjects.Length is 1. In your loop on line 10 of the root node class you don't explicitly ignore 'this', so the sphere test is going to return the current root node in addition to any others nearby. As a result the rest of your code would always end up looking at the same object.
Try giving each of your root nodes a different name, then printing out the name of each linkedNode inside that for loop. If it always prints out the same name then there's your problem, probably solved by:
Ignoring the node corresponding to 'this' inside the for loop
Making sure the radius you're searching for is large enough to detect other objects nearby
The only other possible cause I could think of would be if you had a hierachy for each of your nodes, and the root nodes were staying in the same place but their children where moving (if, for example, the child nodes have rigid bodies that move under physics but the root nodes don't). This would be pretty obvious in the editor though.
-Chris