0
\$\begingroup\$

First, I'd like to apologize because this is a very basic and repetitive question, but I am completely new to game development.

I understand that I can find a distance between two objects using:

float dist = Vector3.Distance(other.position, transform.position);

However, how can I find the distance between a point of one object to other object?

For instance let's say my object is this sphere

enter image description here

Now, how can I return an array that says that there are no objects to the left (null), in the front there is an object at 1, and to the right there is an object at 0.5?

Thank you for your patience and understanding

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .