Physics.Boxcast, how does it work?
I've read the documentation but i don't really understand it. I'm trying to use it for a rpg combat system because the raycast seems to be too thin. Or is there a way to visualize it with a cube?
Answer by Bunny83 · Feb 28, 2016 at 04:23 PM
Well here's the signature (at least the most important parameters):
public static bool BoxCast(Vector3 center, Vector3 halfExtents, Vector3 direction, Quaternion orientation = Quaternion.identity)
The parameters are used like this:
center: Like any raycast method this is the position from where the box cast starts.
halfExtents: Is a vector that defines the size of the cuboid in "local space". So each component represents half the size in that dimension.
direction: Is the casting direction just like in all other raycast method.
orientation: The orientation defines the rotation of the cube. If you don't pass a value or pass "Quaternion.identity" manually the cube will be aligned with the world axis. You can pass any Quaternion to rotate the cube the way you want.
So for example if you want to cast a cube that is rotated so it's z axis points along your casting direction you can simply do:
BoxCast(pos, new Vector3(1,2,3), dir, Quaternion.LookRotation(dir));
This will calculate a rotation so the z axis is aligned with your casting direction.
The cube will be 2 units wide (1 half in x), it will be 4 units high (2 half in y) and 6 units long (3 half in z).
Let say if I have a BoxCollider.size. How would I set the exact BoxCast size so I only want to check the bottom face of the BoxCollider.
Answer by tanoshimi · Feb 28, 2016 at 04:14 PM
Can't say I've ever used BoxCast - think it must be a very recent addition. However, I have used SphereCast, which provides a "fat raycast" and sounds like it might be more what you're looking for. There's examples on the documentation page.
Yeah I' ve tried that but the sphere isn't good because i want to detect the enemy in front of the player and that detects them behind it as well
It shouldn't do - sounds like you're starting the ray from the wrong location.
I started the ray from the position of the player