- Home /
Question by
rhinos74 · Apr 24, 2015 at 05:56 AM ·
physics2dedge detection
Check if a gameobject surrounded by colliders
I need to know if my gameobject is surrounded by 2D edgeColliders in order to know if its trapped or not. I tried to send raycast around gameobject 360 degree but i couldn't solve the issue!
Here is the code for raycasting
isTrapped = true;
for(int i =0;i<=360;i++)
{
Vector3 direction = new Vector3(Mathf.Cos(i*0.0174f),Mathf.Sin(i*0.0174f),0);
RaycastHit2D hit = Physics2D.Raycast(raycastObject.transform.position,direction,35f,layermask);
Debug.DrawRay(raycastObject.transform.position,direction,Color.green);
if(hit.collider==null && hit.collider==null)
isTrapped=false;
}
if(isTrapped)
Debug.Log("isTrapped = true");
Basically what i wanted to check is below image. X,Y,Z are dynamically changing so i cannot use sphere cast
and
untitled-1.png
(19.8 kB)
Comment
You could check if gameObject is in XYZ by checking
1) Are point X and gameObject are on the same side of line YZ?
2) Are point Y and gameObject are on the same side of line XZ?
3) Are point Z and gameObject are on the same side of line XY?
If 1-3 are true then gameObject is inside xyz
Your answer
