Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. 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 Propixel, Aug 13, 2023.

  1. Propixel

    Propixel

    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