Search Unity

Noob question: (UnityEngine.BoxCollider) Log message

Discussion in 'Physics' started by BenVenNL, Oct 20, 2019.

  1. BenVenNL

    BenVenNL

    Joined:
    Oct 14, 2019
    Posts:
    56
    Just learning the basics here.

    I'm having two Cube object slide on a surface hitting each other from opposite sides. A red cube and a green cube.

    Both have exactly the same script attached and it works fine. Having some fancier Debug.Log scripts made under the OnCollisionEnter event to better understand what is going on. But when the two hit each other there is a difference in the Log.

    [14:52:20] RedCube hit Object GreenCube (UnityEngine.BoxCollider) with tag called Player
    [14:52:20] GreenCube hit Object RedCube with Tag called Player


    Why the is the "(UnityEngine.BoxCollider)" added?
     
  2. BenVenNL

    BenVenNL

    Joined:
    Oct 14, 2019
    Posts:
    56
    Addin to this, not wanting to spam the forum but there is no clear awnser I could find.

    HitInfo.collider.ClosestPoint(new Vector3 (0, 1000,1000))
    HitInfo.collider.ClosestPointOnBounds(new Vector3(0, 1000, 1000))

    Gives same result for my two cubes example.
    Under wich surcomstances would the results be different? I looked up "Bounding Box vs Collider" but could not find a clear awnser.
     
  3. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    Because that is what .ToString() method returns for that collider object. When you output anything via Debug.Log(...) it requires a string to be passed. And to convert object to the string - .ToString() is used.

    Its not something Unity related, it just how C# works.

    https://docs.unity3d.com/ScriptReference/Collider.ClosestPoint.html
    https://docs.unity3d.com/ScriptReference/Collider.ClosestPointOnBounds.html
    Always check the manual first if you've got those kind of questions. There's always some useful information available.
     
  4. BenVenNL

    BenVenNL

    Joined:
    Oct 14, 2019
    Posts:
    56
    Thnx of taking the time to try to explain., I don't really understand, but that doesn't matter:oops: the "ït's just how C# works" was good enough for me :) In only 4 hours deep into C#. (But not new at scripting though …)

    Regarding the 2nd part;
    This is what the manual says " Note: The difference from ClosestPointOnBounds is that the returned point is actually on the collider instead of on the bounds of the collider. (bounds is a box that surrounds the collider.) " Thats what I already came across and did not help explain.

    Clickin on 'bounds' will only teach a script how to fetch data from 'bounds'. So I'm still confused as to what it really is and when you would choose one over the other.

    The only thing I can think of is 'bounds' being the most external coördinates fo a collider. For example, having a star shaped collider will have a 'bound' that is the cube, that fits closest around the star shape collider. Is it something like that?
     
  5. xVergilx

    xVergilx

    Joined:
    Dec 22, 2014
    Posts:
    3,296
    It really depends on the context where its used.
    https://en.wikipedia.org/wiki/Bounding_volume

    In this case its just a simplified box, which envelops whole collider.

    I'd say you'd almost always want to use ClosestPoint if you need point that lies in / on the actual collider volume.

    ClosestPointOnBounds will only return point that is on the "edge" of the collider, in case passed position is inside of the collider.

    Edit:
    You can visualize it by creating a gizmos script:

    (Untested)
    Code (CSharp):
    1. public class VisualizePoint : MonoBehaviour {
    2.    [SerializeField]
    3.    private Collider _collider;
    4.  
    5.    [SerializeField]
    6.    private Transform _testPoint;
    7.  
    8.    private void OnDrawGizmos() {
    9.         if (_testTransform == null) return;
    10.         if (_collider == null) return;
    11.      
    12.         Vector3 closest = _collider.ClosestPoint(_testPoint.position);
    13.  
    14.         Gizmos.color = Color.cyan;
    15.         Gizmos.DrawWireSphere(closest, 0.3f);
    16.  
    17.         closest = _collider.ClosestPointOnBounds(_testPoint.position);
    18.  
    19.         Gizmos.color = Color.red;
    20.         Gizmos.DrawWireSphere(closest, 0.3f);
    21.    }
    22. }
    Attach this script to any object, and setup a collider + test point that you can drag in the scene view.
     
    Last edited: Oct 23, 2019
    BenVenNL likes this.
  6. BenVenNL

    BenVenNL

    Joined:
    Oct 14, 2019
    Posts:
    56
    Thank you!

    For some future reference. Here is a screenshot example. I changed the colours.
    GreenCube and RedCub collided, and red is falling off the edge. The referencepoint is the white sphere hanging above and a bit behind the scene.

    .ClosestPoint = is on te colliders surface. Indeed.
    .ClosestPointOnBounds = on an imaginairy box surrounding the object, sides equal to the scenes/wolds xyz- axis.

    I wish the Unity manual came with such clear awnsers.
     
    Last edited: Oct 24, 2019
  7. BenVenNL

    BenVenNL

    Joined:
    Oct 14, 2019
    Posts:
    56