dimanche 7 février 2016

Check if transform is overlapping

I have a few rooms which are placed randomly so I have to check if a room is overlapping. The rooms have a size of 10x10 and are for test reasons placed exactly side by side (they don't overlap in the scene). The Floor is a Transform which consists out of 1 or more Transforms (in this case out of one square but for other forms it it could be 2 or more).

To check if they are overlapping I have this function which doesn't work. Debug log is always something between 3 and 61..

public bool Overlapping()
{
    //Lists for the position and of the size of each floor transform
    List<Vector3> positions = new List<Vector3>();
    List<Vector3> sizes = new List<Vector3>();
    //Check if floor consists out of more than 1 transform
    if (Floor.childCount > 0)
        foreach (Transform t in Floor)
        {
            positions.Add(t.position);
            sizes.Add(t.lossyScale);
        }
    else
    {
        positions.Add(Floor.position);
        sizes.Add(Floor.lossyScale);
    }

    //Save old room pos and move it out of the way
    Vector3 position = this.transform.position;
    this.transform.position = new Vector3(0, 100, 0);

    //Check if any floor transform would overlap
    for (int i = 0; i < positions.Count; i++)
    {
        //Returns the colliders which are overlapping
        if (Physics.OverlapBox(positions[i], sizes[i] / 2).Length > 0)
        {
            Debug.Log(Physics.OverlapBox(positions[i], sizes[i] / 2).Length);
            //return this room to it's old position
            this.transform.position = position;
            return true;
        }
    }

    //return this room to it's old position
    this.transform.position = position;
    return false;
}




Aucun commentaire:

Enregistrer un commentaire