Find nearest Gameobject in a Collision
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();
}
}
Answer by DrDigg0R · Oct 30, 2019 at 11:04 PM
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;
}
Thank you for your answer! But with your Code, he dont detect the nearest Collision You can see it on my picture.$$anonymous$$y player (the Black point) have to detect the nearest Collider (green line) and transform the ball to this point. I take your Code to my Project, but my Player dont detect the nearest point, only the point in front of him..
I think you should be able to deter$$anonymous$$e which of the objects is the nearest one by using ContactPoint.separation: https://docs.unity3d.com/ScriptReference/ContactPoint-separation.html
How Can I do that? Can you give me an example?
Answer by raypower1 · Nov 03, 2019 at 02:03 AM
Okay sorry, he detect the nearest Collider, but he dont detect the nearest Point of the Collider..
Answer by DrDigg0R · Nov 17, 2019 at 12:52 PM
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" + "\n"
+ "contact.separation => " + contact.separation + "\n");
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