Skip to main content
3 of 3
corrected grammer
plainerman
  • 331
  • 2
  • 10

If it should only work with left, up, right, and down use Raycasts! Your code will look like this

public float[] CheckCollisions() {
    Raycast hit;
    float[] collisions = new float[4];
    if (Physics.Raycast(transform.position, transform.Vector3.Up, 100.0f, out hit))
        collisions[0] = hit.transform.position - transform.position;
    else
        collisions[0] = Float.NaN;
    //other directions
    return collisions;
}

It isn't the most beautiful way - make a Vector3 array with all directions you wanna check and then iterate through, and check current Vector for hit.

Sorry for any mistakes, wrote this with my cellphone.

plainerman
  • 331
  • 2
  • 10