- Home /
BoxCast does not work at Runtime, but in Game Mode (sometime strange results...)
Hi all,
I'm struggling with the BoxCastAll function for a couple of days and finally got something working. However, quite a bad surprise when I realized that what was working like a charm in Game Mode does not work once built.
All the debug says that everything is fine, but the BoxCastAll is just failing me. I drew a box to debug and it's always aligned where it should be and centered. I also tried with a LineRenderer (rend object in the code). The CastBox is just off by few units and starts acting even weirder when the camera is rotated.
What is strange is that the behavior is completly different from the Game Mode where everything works fine. Or almost fine. I've realized that the BoxCast does not work after a moment of inactivity (see https://forum.unity.com/threads/boxcast-stops-working-after-a-while.701732/ or https://www.reddit.com/r/Unity3D/comments/c5q2yf/boxcast_stops_working_after_a_while/ ). I've already found similar reports without solution. And occasionally, the BoxCast just produces different result from a run to another (in Game Mode). Sometimes it works all right and very precisely how it should and sometimes I have strange behavior like the BoxCast is completly off. However the "Debug Box" I'm drawing and which serves as base to cast the box is always positioned correctly.
What I am doing wrong?
void SelectUnitsBoxCast()
{
//Resetting the cube scale
testCube.transform.localScale = Vector3.one;
//align the cube with the camera
testCube.transform.rotation = Camera.main.transform.rotation;
//Retrieve the two edges of the selection rectangle and find the center point
Vector3 startSelection = Camera.main.ScreenToWorldPoint(new Vector3(mousePosStart.x, mousePosStart.y, Camera.main.nearClipPlane));
Vector3 endSelection = Camera.main.ScreenToWorldPoint(new Vector3(mousePosEnd.x, mousePosEnd.y, Camera.main.nearClipPlane));
Vector3 center = (startSelection + endSelection) / 2.0f;
testCube.transform.position = center;
//Calculate the Cube width and Cube Height
float cubeWidth = Mathf.Abs(testCube.transform.InverseTransformPoint(Camera.main.ScreenToWorldPoint(new Vector3(mousePosEnd.x, mousePosEnd.y, Camera.main.nearClipPlane))).x
- testCube.transform.InverseTransformPoint(Camera.main.ScreenToWorldPoint(new Vector3(mousePosStart.x, mousePosStart.y, Camera.main.nearClipPlane))).x);
float cubeHeight = Mathf.Abs(testCube.transform.InverseTransformPoint(Camera.main.ScreenToWorldPoint(new Vector3(mousePosEnd.x, mousePosEnd.y, Camera.main.nearClipPlane))).y
- testCube.transform.InverseTransformPoint(Camera.main.ScreenToWorldPoint(new Vector3(mousePosStart.x, mousePosStart.y, Camera.main.nearClipPlane))).y);
Vector3 cubSize = new Vector3(cubeWidth, cubeHeight, 150f);
testCube.transform.localScale = cubSize;
//Raycast
RaycastHit[] results = Physics.BoxCastAll(testCube.transform.position, new Vector3(cubeWidth, cubeHeight, 1f) / 2f, testCube.transform.forward, testCube.transform.rotation, 150f);
rend.SetPosition(0, testCube.transform.position);
rend.SetPosition(1, testCube.transform.position + testCube.transform.forward * 150f);
}
Your answer
Follow this Question
Related Questions
Bouncing ball never becomes stationary 1 Answer
Multiple Cars not working 1 Answer
How to check if an object is lit by a light? 1 Answer
Distribute terrain in zones 3 Answers
2d game platform physics 1 Answer