Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by raypower1 · Oct 29, 2019 at 06:28 PM · collision detectioncollisoncollision.contacts

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();
       }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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;
     }


nearest game object

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image raypower1 · Nov 03, 2019 at 01:08 AM 0
Share

Thank you for your answer! But with your Code, he dont detect the nearest Collision alt text 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..

export.png (52.3 kB)
avatar image DrDigg0R raypower1 · Nov 12, 2019 at 08:43 PM 0
Share

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

avatar image raypower1 DrDigg0R · Nov 12, 2019 at 08:58 PM 0
Share

How Can I do that? Can you give me an example?

avatar image
0

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..alt text


export-1.png (22.1 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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;
     }
 }

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

191 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How Can I Know which collider has call OnCollisionExit() on the script's gameObject?? 1 Answer

Best Practice for Maze Runner Collectible Scenario - mlagents 0 Answers

Collisions without colliders 0 Answers

Simple 3D collision issue 0 Answers

Rogue Like 2D - OnTriggerEnter2D fails 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges