public class GridController : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
// Get closest free point in grid
public Vector3 snapPoint (GameObject GroundObject, Vector3 hit){
float worldX = 3.2F;
float worldY = 3.2F;
float worldZ = 3.2F;
bool IsGenerated = false;
float snap = 0.2F;
List<Vector3> possiblePositionsList = new List<Vector3>();
List<Vector3> tempPositionsList = new List<Vector3>();
List<float> distanceList = new List<float>();
int minIndex; // Shortest distance
float minVal;
this.transform.position = hit;
Vector3 position = this.transform.position;
position.x = Mathf.Round(position.x / snap) * snap;
position.y = Mathf.Round(position.y / snap) * snap;
position.z = Mathf.Round(position.z / snap) * snap;
this.transform.position = position;
Vector3 gridStart = transform.position - new Vector3(worldX / 2, worldY / 2, worldZ / 2);
for(float x =0F; x<worldX; x += snap) {
for(float y =0F; y<worldY; y += snap) {
for(float z =0F; z<worldZ; z += snap) {
Vector3 tempPosition;
tempPosition = new Vector3(gridStart.x + x, gridStart.y + y, gridStart.z + z);
tempPositionsList.Add(tempPosition);
}
}
}
IsGenerated = true;
foreach (Vector3 tempPosition in tempPositionsList){
GroundObject.transform.position = tempPosition;
Collider[] colliders = Physics.OverlapSphere(tempPosition, 10);
foreach (Collider current in colliders){
if(current.bounds.Intersects(GroundObject.collider.bounds) == false){
possiblePositionsList.Add (tempPosition);
}
}
colliders = null;
}
foreach (Vector3 possiblePosition in possiblePositionsList){
distanceList.Add(Vector3.Distance(possiblePosition, transform.position));
}
minVal = distanceList.Min();
minIndex = distanceList.IndexOf(minVal);
minIndex = distanceList.IndexOf(distanceList.Min());
// Debug.Log (minVal);
// Debug.Log (possiblePositionsList[minIndex]);
Collider[] testcolliders = Physics.OverlapSphere(possiblePositionsList[minIndex], 10);
foreach (Collider current in testcolliders){
if(current.bounds.Intersects(GroundObject.collider.bounds)){
Debug.Log ("INTERSECTS");
}
}
return possiblePositionsList[minIndex];
}
}
When runned it line 71 can be true. Why?