I ran into an issue with box-casts while developing a 3D game for mobile.
I want to check the path between my player and his target, to avoid him passing through environmental objects (he doesn't have a rigidbody attached and movement is only possible between specific points).
This is the code that I used to check:
private bool CheckPath (Vector3 position, Vector3 target)
{
Vector3 center = Vector3.Lerp(transform.position, target, 0.5f);
Vector3 halfExtents = new Vector3(1, 1, (transform.position - target).magnitude) / 2;
Quaternion rotation = Quaternion.LookRotation((transform.position - target).normalized);
RaycastHit[] rhit = Physics.BoxCastAll(center, halfExtents, (transform.position - target).normalized, rotation);
bool result = rhit.All(r => r.collider.tag != "Environment");
DebugUtilities.BoxCastDebug.DrawBox(center, halfExtents, rotation, result ? Color.green : Color.red, 3);
return result;
}
This code works for the most situations:
But fails, for example for this situation:
To visualize the box-cast I used the script from this Unity Answers link.
I am not sure where the problem lies, although the most likely cause would be a flaw in the debugging script mentioned above, which made me believe that my box-cast call was correct.
I am grateful for every solution, although a simple one would be more appreciated.
Some further information (if needed):
- The objects that I want to make impassable are marked with a
Environment
tag. - I was not the one that wrote the debugging script, it seemed to work at first, so I was fine with it.
Three problems in your code
1.The
position
variable from the unction parameter is not used. You are instead usingtransform.position
which means that the starting point may be wrong.Replace all your
transform.position
withposition
.2.You are performing the raycast backwards. It should not be
transform.position - target
. That should betarget - transform.position
.3.Your
Physics.BoxCastAll
will not work properly since there is no ending to the raycast. Objects behind the starting will be detected by the raycast. Now, if you fix problem #2, the problem will reverse. Now, all the objects in behind the target will also be detected since you did not provide the raycast distance. You can provide the distance withVector3.Distance(position, target)
in the last parameter of thePhysics.BoxCastAll
function.Fixed function:
It's worth setting up 3 objects (Cubes) in order to easily test this function. The first one is from Object (Player). The middle one should be the obstacle with the "Environment" tag. The last one should be the target Object where player is moving to.
Then you can use the script below to test it. Run it and move the obstacle away between the player and the target and it should work as expected.
You will get the similar result below: