- Home /
 
Checking for box collider
I'd like to check for a box at the end of a raycast (hit point). The problem is that the only function that I know of that can do this is Physics.CheckBox, but that returns a bool, and i want the object that has collided with this box. Anyone know how to do this?
Do you want to check colliders inside of a box at the raycast position?
yes, and I'd like to know what objects are in it.
Answer by $$anonymous$$ · Nov 13, 2020 at 06:29 AM
You can get the raycast hit position and cast a box there, like this:
         #region Raycast Variables
 
         Ray raycast = new Ray(startPosition, direction);
         RaycastHit raycastHit;
 
         float raycastLength = 10;
 
         #endregion
 
         // You can access the gameobject of a collider easily
         Collider[] gameobjects;
 
         if (Physics.Raycast(raycast, out raycastHit, raycastLength, layerMask))
             gameobjects = 
                 Physics.OverlapBox(raycastHit.point, boxHalfExtents, boxRotation, layerMask);
 
              Your answer
 
             Follow this Question
Related Questions
My Raycast acts like it's hitting something when it isn't 0 Answers
Raycast from inside an object 2 Answers
How to get bullets to hit crosshair 2 Answers
Layered Collisions 1 Answer
Creating custom character controller collision checking 0 Answers