Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Avedis · Jan 25, 2021 at 09:45 PM · colliderspositioning

How to find the closest point outside a collider from a point inside the collider.

I'm trying to figure out how to reposition a transform that is inside a object with a collider to the nearest position just outside of the collider.

I have been trying to use these 3 methods I've found:

         //Method 1
         Vector3 closestPoint = avoidanceObject.GetComponent<Collider>().ClosestPoint(target.transform.position);

         //Method 2
         Vector3 closestPointInBounds = avoidanceObject.GetComponent<Collider>().ClosestPointOnBounds(target.transform.position);

         //Method 3
         Vector3 pos = target.transform.position;
         Quaternion rot = target.transform.rotation;
         Vector3 closestPointCollider = Physics.ClosestPoint(target.transform.position, target.GetComponent<Collider>(), pos, rot);

         target.transform.position = closestPointCollider;


These three methods do not change the position, is it because the transform is inside the collider? If so how would I find the nearest point or direction to move outside of the collider?

Thank you!

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by marck_ozz · Jan 26, 2021 at 05:16 AM

You can try with "Physics.OverlapSphere"

I use a code like this to detect an enemy inside of the radius of a bomb explosion:

 public float radius = 200f;
 
 void Explosion()
     {   
         Collider[] colliders = Physics.OverlapSphere(transform.position, radius);
         foreach(Collider enemy in colliders)
         {
             if(enemy.CompareTag("Enemy"))
             {
                 EnemyBehaviour exploded = enemy.GetComponent<EnemyBehaviour>();
                 exploded.DestroyedByBomb();
                 Destroy(gameObject);
             }
         }
     }
 }

and you can Make something like this:

 public float radius = 200f;
 public GameObject yourObject;
 
 public void DetectNewPosition()
     {   
         Collider[] colliders = Physics.OverlapSphere(transform.position, radius);
         foreach(Collider newPos in colliders)
         {
             if(newPos.CompareTag("NearestPoint"))
             {
                 yourObject.transform.Position = newPos.transform.Position;
             }
         }
     }
 }

This requires that your "Nearest Point" object have a collider and a tag "NearestPoint"

Another option is to use "OnCollisionEnter" like this:

 public GameObject yourObject;
 
 private void OnCollisionEnter(Collision collision1)
     {
         //Debug.Log("Colision");
         if (collision1.collider.tag == "NearestPoint")
         {
             NewPos();
         }
         
         void NewPos()
         {
             ContactPoint contact = collision1.GetContact(0); //contacts[0];
             Quaternion qua = Quaternion.FromToRotation(Vector3.up, contact.normal);
             Vector3 pos = contact.point;
 
             yourObject.transform.position = pos;
             yourObject.transform.rotation = qua;
         }
     }

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 Avedis · Jan 26, 2021 at 02:08 PM 0
Share

Thank you, I'm not sure I'll be able to integrate it though. I currently use OnTriggerEnter() to generate a list of objects the AI needs to avoid, selecting the nearest one as the target object. I then send out a raycast forward to find the point it hits the object. From there I need to deter$$anonymous$$e the nearest edge so the AI knows what direction to steer. $$anonymous$$aybe I can find the offset of the hitpoint from the center of the object to find a direction to turn.

avatar image marck_ozz Avedis · Jan 27, 2021 at 01:01 AM 0
Share

well, that's another story haha, I suggested that because you mentioned a "Collider".

avatar image DearUnityPleaseAddSerializableDictionaries · May 22, 2021 at 09:48 PM 0
Share

What about Box 2D colliders? Or Polygon?

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

113 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

Related Questions

physics.OverlapSphere colliders 1 Answer

How do I stack my objects? 1 Answer

How to find the direction and distance to move an object so its no longer jammed into another? 0 Answers

Model Teleports after Animation From Spawn to Idle. Collider doesn't follow 0 Answers

Spawning an object on the opposite side of an activator object with no overlaps 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