2
public static bool BoxCast(Vector3 center,
                           Vector3 halfExtents,
                           Vector3 direction,
                           Quaternion orientation = Quaternion.identity, 
                           float maxDistance = Mathf.Infinity,
                           ...);

These are the parameters for a boxcast in Unity3D. I am confused over the purpose of the maxDistance parameter as we already draw the box with the halfExtents parameter. What if I don't want to move the box? I.e., I want to draw a box and get information about whatever is inside of it. I am not looking to move the box. Using maxDistance = 0 seems to do nothing as it does not register any hits. Using maxDistance > 0 will move the box, and I am looking to avoid this.

How can I use BoxCast(), avoiding moving the box?

blackhole
  • 137
  • 4
  • 15

3 Answers3

5

There is Physics.OverlapBox, perhaps that would better suit your needs?

public static Collider[] OverlapBox(Vector3 center, Vector3 halfExtents, 
      Quaternion orientation = Quaternion.identity, 
      int layerMask = AllLayers, 
      QueryTriggerInteraction queryTriggerInteraction = QueryTriggerInteraction.UseGlobal);

https://docs.unity3d.com/ScriptReference/Physics.OverlapBox.html

Or maybe even Physics.CheckBox if you dont care whats actually in the box.

https://docs.unity3d.com/ScriptReference/Physics.CheckBox.html

Fredrik Widerberg
  • 3,068
  • 10
  • 30
  • 42
1

Any Physics.^shape^Cast() calls are just raycasts with dimension. In your case, a BoxCast pushes a box along the ray and checks what it intersects, out to maxDistance (Note: the 'box' is not a real object).

What you're asking for is the Physics.Overlap^shape^(). In your case, the OverlapBox will return all colliders within that box as an array. Anything you need to know about the objects in the box you can then parse out in script.

See @Frederik Widerberg's answer for OverlapBox links =)

Immersive
  • 1,684
  • 1
  • 11
  • 9
0

. With arguments,

. _position: Position of the cube about to be casted

. Vector3(0.5f, 0.5f, 0.5f) : assuming you have unit cude sized 1.0

. _directionVector: cast's direction in 3D, for example
Vector3 directionVector = _positon - positionWhereFrom (cube's casting direction)

. Quaternion.LookRotation(_directionVector): look to direction as orientation

. 0.0f : No ray casting distance, in place box ****

. _movementControlLayerMaskValue : the layers which you want to interact your cast ex:_movementControlLayerMaskValue = LayerMask.GetMask("Default");

Physics.BoxCast(_position,new Vector3(0.5f, 0.5f, 0.5f),_directionVector, Quaternion.LookRotation(_directionVector),0.0f,_movementControlLayerMaskValue);

Deniz aydın
  • 91
  • 1
  • 4