Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Feedback A mistake or something that needs a Fix in unity script reference about ( Physics.BoxCast )

Discussion in 'Documentation' started by RayMax1, Aug 13, 2023.

  1. RayMax1

    RayMax1

    Joined:
    May 28, 2017
    Posts:
    1
    Hi, hope you have nice times.
    in the script here https://docs.unity3d.com/ScriptReference/Physics.BoxCast.html
    code works well but there is something that isn't match.
    - - - - - - - - - -
    in this line :

    m_HitDetect = Physics.BoxCast(m_Collider.bounds.center, transform.localScale, transform.forward, out m_Hit, transform.rotation, m_MaxDistance);

    we are defining the size of the box as the scale of the object, since we know it's a vector3 halfExtents and will be double.
    and if we are going with this size, then in the void OnDrawGizmos() we need to double the size of cube that we are drawing, in this line :
    Gizmos.DrawWireCube(transform.position + transform.forward * m_Hit.distance, transform.localScale);
    - - - - - - - - - -
    So : we need to match the size of the castbox to the size of the drawing box in the gizmo.
    and the best solution is just use *0.5f on the size of the box in the casting function.

    m_HitDetect = Physics.BoxCast(m_Collider.bounds.center, transform.localScale*0.5f, transform.forward, out m_Hit, transform.rotation, m_MaxDistance);

    - - - - - - - - - -
    it's what we get with the current codes in the reference :
    s1.png

    and its what we get after fixing the code :


    s2.png

    As you can see in the first picture the gizmo box is in the air because the size of the Boxcast is twice bigger, by changing the size of that to 1/2 the problem got solved and we make a cube exactly on the surface of the place we are casting box at.
    - - - - - - - - - -
    Thank you >.<
     
    Last edited: Aug 13, 2023