Sunday, November 3, 2019

unity - Is Physics2D.BoxCast a safe alternative to a row of raycasting?


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?



Answer



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.


No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...