- Home /
Question by
neshat-bln · Jan 26, 2018 at 11:00 PM ·
c#collision detectionraycasthitvisibilityobstacle
Problem with ray tracing and obstacle detection
Hi, I wrote a code to find the coverage of a surface. But, I face two problems in my code. First, it cannot detect the collision with layer "Walls".
Second, I make 4 rays using one ray to make a pyramid which represents the camera FOV. The problem is when only a part of the surface is behind the wall and is not in pyramid and FoV, it considers all area as invisible area.
The part of my code relate to this issue is:
//making the layer mask
void Start(){
layerMask = LayerMask.GetMask("Walls");
}
public bool withinRoomBounds(){
//upper left, upper right, bottom left, bottom right rays
//making a pyramid from one ray
Ray rayUL = new Ray(), rayUR = new Ray(), rayBL = new Ray(), rayBR = new Ray(), rayTempU = new Ray(), rayTempB = new Ray();
RaycastHit hit1, hit2, hit3, hit4;
Camera camera = (Camera)objectToRotate.GetComponent (typeof(Camera));
float fieldOfViewVertical = camera.fieldOfView;
float fieldOfViewHorizontal = fieldOfViewVertical * camera.aspect;
rayTempB.origin=rayTempU.origin = rayUL.origin = rayUR.origin = rayBL.origin = rayBR.origin = objectToRotate.transform.position;
rayTempB.direction = rayTempU.direction = objectToRotate.transform.forward;
rayTempU.direction = Quaternion.AngleAxis (fieldOfViewVertical / 2f, objectToRotate.transform.right)*rayTempU.direction;
rayTempB.direction = Quaternion.AngleAxis (-fieldOfViewVertical / 2f, objectToRotate.transform.right)*rayTempB.direction;
rayUL.direction = Quaternion.AngleAxis (-fieldOfViewHorizontal / 2f, objectToRotate.transform.up)*rayTempU.direction;
rayUR.direction = Quaternion.AngleAxis (fieldOfViewHorizontal / 2f, objectToRotate.transform.up)*rayTempU.direction;
rayBL.direction = Quaternion.AngleAxis (-fieldOfViewHorizontal / 2f, objectToRotate.transform.up)*rayTempB.direction;
rayBR.direction = Quaternion.AngleAxis (fieldOfViewHorizontal / 2f, objectToRotate.transform.up)*rayTempB.direction;
bool flag = true;
if (Physics.Raycast (rayUL, out hit1, 100f, layerMask))
flag=flag&&hit1.distance>testPyramidSize;
if (Physics.Raycast (rayUR, out hit2, 100f, layerMask))
flag=flag&&hit2.distance>testPyramidSize;
if (Physics.Raycast (rayBL, out hit3, 100f, layerMask))
flag=flag&&hit3.distance>testPyramidSize;
if (Physics.Raycast (rayBR, out hit4, 100f, layerMask))
flag=flag&&hit4.distance>testPyramidSize;
if (!flag)
return false;
else {
Debug.DrawRay (rayUL.origin, rayUL.direction*testPyramidSize, Color.red, rayStayDuration);
Debug.DrawRay (rayUR.origin, rayUR.direction*testPyramidSize, Color.red, rayStayDuration);
Debug.DrawRay (rayBL.origin, rayBL.direction*testPyramidSize, Color.red, rayStayDuration);
Debug.DrawRay (rayBR.origin, rayBR.direction*testPyramidSize, Color.red, rayStayDuration);
}
return true;
}
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
RayCast hitting wrong layer? 1 Answer