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 /
avatar image
0
Question by alexthomson666 · Mar 03, 2017 at 06:23 PM · collision detection

How to detect how far another collider is intersecting another!

I am using OnCollisionStay () to find out how far an object is colliding into another. If it is colliding more than it should be then it will send another script a message saying it can't be placed. Code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ClippingDetector : MonoBehaviour {
 
     private Structure structure;
     private Bounds structureBounds;
     private LayerMask layerMask;
 
     void Awake () {
         structure = GetComponent<Structure> ();
         structure.QueryCollisions ();
         structureBounds = structure.GetComponent<Collider> ().bounds;
         layerMask = FindObjectOfType<GameController> ().strictLayerMask;
     }
 
     public void OnCollisionStay (Collision coll) {
         if (coll.gameObject.GetComponent<Structure> () != null && coll.gameObject.GetComponent<Structure> ().strictCollisions && layerMask == (layerMask.value | 1<<coll.gameObject.layer)) { // Its a structure and has strict collisions
             structureBounds = new Bounds (structureBounds.center, structureBounds.size * .95f); // New bounds
         } else {
             structureBounds = new Bounds (structureBounds.center, structureBounds.size * .8f); // New bounds
         }
         if (coll.collider.bounds.Intersects (structureBounds) && coll.collider.tag == "Structure") {
             structure.CollisionEnter (coll.gameObject);
         }
     }
 
     public void OnCollisionExit (Collision coll) {
         structure.CollisionExit (coll.gameObject);
     }
 }
 
Comment
Add comment · Show 2
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 toddisarockstar · Mar 04, 2017 at 11:06 PM 0
Share

us vector3.distance between the objects:

https://docs.unity3d.com/ScriptReference/Vector3.Distance.html

avatar image alexthomson666 · Mar 05, 2017 at 01:53 PM 0
Share

Already tried using Vector3.Distance. The problem is that when I place objects directly next to each other it says they are colliding when they aren't intersecting. It's nothing to do with the colliders and I cant use Vector3.Distance as it would not work if the player is going to place a non square placeable

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Glurth · Mar 05, 2017 at 02:34 PM

Alas, this problem is not quite a simple as it sounds. Mostly because of the possible variation in the SHAPE of your objects. I see you use bounding boxes, which might be fine approximation, but will be very limited in accuracy. Consider a 3d plus sign; with this shape the bounding box corner might intersect something, even if the plus-sign does not.

Computing the overlap/intersection volume of two arbitrary meshes is a pretty complex operation, usually employing lots of line/plane or triangle/triangle intersection tests. (here is a good resource on that stuff: http://www.realtimerendering.com/intersections.html)

The additional difficulty with using bounding boxes, is that they don't rotate: they are always an axis-aligned box.

Given the above limitations, will using the bound box be sufficient for your needs?

If so, once you have confirmed the bounding boxes intersect, you will need to:

   loop through all the corners points of bounds A
       if this point intersect bound B ( use Bounds.Contains to test)
           loop though all the FACES/planes of bounds B
              compute the distance between our intersecting bound A corner, and the current bound B face
              keep the largest distance value computed (this is your intersection depth)

(I was hoping Bounds.SqrDistance would work for this, rather than having to loop, but that only seems to work for vectors OUTSIDE the bounds. Passing vectors "Contained" inside the bounds, returns 0.)

Comment
Add comment · Show 2 · 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 alexthomson666 · Mar 05, 2017 at 08:48 PM 0
Share

Thanks Glurth, works perfectly :D

avatar image Glurth alexthomson666 · Mar 06, 2017 at 04:45 AM 0
Share

oh great! I'll convert it to an answer

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Coin collection script (not working) 1 Answer

I have no idea how to check if two 2D objects are colliding 2 Answers

OnParticleCollision not being called on Rigidbody 1 Answer

Collision detection problem 0 Answers

Tile collision by sprite is not accurate with Tilemap Collider 0 Answers


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