5
\$\begingroup\$

Raycasting in rows is a common practice.

  1. Is using the Physics 2D BoxCast function a good alternative to using multiple raycasts spread out (an example use being collision detection in a custom character controller script)?
  2. There is limited documentation on boxcasting. Would boxcasting be too intensive or more friendly to call each frame?
\$\endgroup\$
  • 1
    \$\begingroup\$ An explanation of the parameters; Origin: The point in space for the box to be created around. Size: A Vector2 containing the width and height of a box. (The box streches out from the origin equal to one half of x and one half of y in their respective directions) Angle: A float containing how much the box should be rotated on the origin. Direction: A Vector2 specifying the direction to "drag" the box along. (The x and y are used to create the slope of a line, and the direction is the angle of that slope.) The rest of the parameters are well explained on Unity's scripting reference. \$\endgroup\$ – James Hurley May 31 '15 at 23:08
2
\$\begingroup\$

Box cast would probably be more performant since it's only checking 4 vertices but it'll definitely be more accurate, at least depending on what you want. Good idea!

Example use:

RaycastHit2D raycast = Physics2D.BoxCast (

    //Starting point of box
    Vector2.zero,

    //Size of the box
    new Vector2 (2,2),

    //Angle of box,
    0f,

    //Direction to cast
    Vector2.right,

    //Distance to cast
    5f

);

This BoxCasts starting from Vector2d.zero with a square of length 2 in the direction of (0,1), for a distance of 5. Conceptually, this is like dragging a 2x2 square from (0,0) to (0,5) and detecting any collisions along the way.

\$\endgroup\$
  • 1
    \$\begingroup\$ The parameters explained here as an example are not consistent to that of Physics2D.BoxCast \$\endgroup\$ – James Hurley May 29 '15 at 20:38
  • \$\begingroup\$ Oops, thanks for catching me on that. I updated my answer with a more accurate explanation. \$\endgroup\$ – JPtheK9 May 29 '15 at 22:03
  • \$\begingroup\$ Not to nitpick, but the size is actually a vector 2--feel free to refer to my comment explaining the parameters \$\endgroup\$ – James Hurley May 31 '15 at 23:14
  • \$\begingroup\$ No, please, nitpick away. Thanks again for catching me :). \$\endgroup\$ – JPtheK9 May 31 '15 at 23:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.