I have build a big Floor with wall, doors and obstacles. My first Person Controller have a big capsule Collider around himself to detect a Collision. If this Capsule Collider get in contact with one of this Obsatcles, doors or walls, he would transform a sphere to this Point. The sphere has an Audio Sound to warn the First Person Controller. So i would like to find the nearest Object in an Collision, not only one Collision Point. If i move my FirstPersonController, he should find the new nearest Point an trasnform the ball to this Point. How can i do that (probably with OnCollisionStay)?
My Code:
public class Collision : MonoBehaviour
{
private AudioSource audioSource;
public GameObject roteKugel;
public GameObject warnKugel;
private AudioSource audioSourceWarn;
private Renderer rend;
private Renderer rend1;
void Start()
{
audioSource = roteKugel.GetComponent();
rend = roteKugel.GetComponent();
rend.enabled = true;
for (int i = 0; i < 1; i++)
{
Instantiate(roteKugel, new Vector3(0, 0, 0), Quaternion.identity);
}
audioSourceWarn = warnKugel.GetComponent<AudioSource>();
rend1 = warnKugel.GetComponent<Renderer>();
rend1.enabled = true;
for (int i = 0; i < 1; i++)
{
Instantiate(warnKugel, new Vector3(0, 0, 0), Quaternion.identity);
}
}
void OnCollisionEnter(UnityEngine.Collision collision)
{
float distance;
if (collision.gameObject.tag == "Untagged")
{
return;
}
ContactPoint contact = collision.contacts[0];
if (collision.gameObject.tag == "Tuer" || collision.gameObject.tag == "Wand" || collision.gameObject.tag == "Fenster" || collision.gameObject.tag == "Hindernis")
{
roteKugel.transform.position = contact.point;
}
audioSource.Play();
}
}
If you want to use OnCollisionStay, then the following should work (if you add private GameObject redOrb
at the top):
private void OnCollisionStay(UnityEngine.Collision collision)
{
GameObject nearObject = null;
float nearObjectDistance = 0f;
foreach (ContactPoint contact in collision.contacts)
{
float curDistance = Vector3.Distance(contact.otherCollider.transform.position, transform.position);
Debug.Log("GO " + contact.otherCollider.name + " is " + curDistance + " units away");
if (!nearObject || curDistance < nearObjectDistance)
{
nearObjectDistance = curDistance;
nearObject = contact.otherCollider.gameObject;
}
}
if (!redOrb)
redOrb = Instantiate(roteKugel, new Vector3(0, -2, 0), Quaternion.identity);
redOrb.transform.position = nearObject.transform.position;
}

Okay sorry, he detect the nearest Collider, but he dont detect the nearest Point of the Collider…
You could do something like this:
public class NearestObject : MonoBehaviour
{
public GameObject roteKugel;
private GameObject redOrb;
private void OnCollisionStay(UnityEngine.Collision collision)
{
GameObject nearObject = null;
float nearObjectDistance = 0f;
float nearObjectSeparation = 0f;
Vector3 nearObjectContactPoint = Vector3.zero;
foreach (ContactPoint contact in collision.contacts)
{
float curDistance = Vector3.Distance(contact.otherCollider.transform.position, transform.position);
Debug.Log("GO " + contact.otherCollider.name + " is " + curDistance + " units away" + "
"
+ "contact.separation => " + contact.separation + "
");
if (nearObject == null || curDistance < nearObjectDistance)
{
nearObjectDistance = curDistance;
nearObject = contact.otherCollider.gameObject;
nearObjectContactPoint = contact.point;
nearObjectSeparation = contact.separation;
}
}
if (!redOrb)
redOrb = Instantiate(roteKugel, new Vector3(0, -2, 0), Quaternion.identity);
Vector3 offsetVector = (nearObjectContactPoint - transform.position).normalized * nearObjectSeparation;
//redOrb.transform.position = nearObject.transform.position;
redOrb.transform.position = nearObjectContactPoint + offsetVector;
}
}

nearest point: https://gfycat.com/beautifulickyantipodesgreenparakeet