// can be used to find the nearest obstacle being tracked public static bool nearestObstacle(Dictionary <int, VRObstacle> obs, out VRObstacle nearest) { nearest = new VRObstacle(); if (obs.Count == 0) { return(false); } float min = Mathf.Infinity; foreach (KeyValuePair <int, VRObstacle> o in obs) { if (o.Value.Distance < min) { min = o.Value.Distance; nearest = o.Value; } } return(true); }
public bool Scan(out Dictionary <int, VRObstacle> obstacles) { obstacles = new Dictionary <int, VRObstacle> (); if (m_shortRangeSensorsEnable) { RaycastHit shortSensorsHit; Vector3 shortRangeSensorsStart = transform.position; shortRangeSensorsStart.y += shortRangeSensorsHeight; InitShortRangeSensors(); // Scan for nearby obstacles foreach (VRSensor scan in shortRangeSensorsArray) { Debug.DrawRay(shortRangeSensorsStart, scan.Direction * m_shortSensorLength, Color.green); if (Physics.Raycast(shortRangeSensorsStart, scan.Direction, out shortSensorsHit, m_shortSensorLength, layerMask)) { if (shortSensorsHit.collider.CompareTag(VRStrings.Obstacle) || shortSensorsHit.collider.CompareTag(VRStrings.AI_Car) || shortSensorsHit.collider.CompareTag(VRStrings.Crazy_AI_Car)) { if (!obstacles.ContainsKey(scan.ID)) { Debug.DrawLine(shortRangeSensorsStart, shortSensorsHit.point, Color.yellow); VRObstacle vo = new VRObstacle(); vo.obstacle = shortSensorsHit; vo.obstacleTag = shortSensorsHit.collider.tag; vo.Distance = shortSensorsHit.distance; obstacles.Add(scan.ID, vo); } } } } } if (m_longRangeSensorsEnable) { RaycastHit longSensorsHit; Vector3 longRangeSensorsStart = transform.position; longRangeSensorsStart.y += longRangeSensorsHeight; // long sensors InitLongRangeSensors(); for (uint i = 0; i < numLongRangeSensors; ++i) { Vector3 sensorStart = longRangeSensorsStart; if (i == 1) { sensorStart += transform.right * m_lateralSensorShift; } else if (i == 2) { sensorStart -= transform.right * m_lateralSensorShift; } Debug.DrawRay(sensorStart, longRangeSensorsArray [i].Direction * m_longSensorLength, Color.red); if (Physics.Raycast(sensorStart, longRangeSensorsArray [i].Direction, out longSensorsHit, m_longSensorLength, layerMask)) { if (longSensorsHit.collider.CompareTag(VRStrings.Obstacle) || longSensorsHit.collider.CompareTag(VRStrings.AI_Car)) { if (!obstacles.ContainsKey(longRangeSensorsArray[i].ID)) { Debug.DrawLine(sensorStart, longSensorsHit.point, Color.yellow); VRObstacle vo = new VRObstacle(); vo.obstacle = longSensorsHit; vo.obstacleTag = longSensorsHit.collider.tag; vo.Distance = (longSensorsHit.point - transform.position).magnitude; obstacles.Add(longRangeSensorsArray[i].ID, vo); } } } } } return(obstacles.Count == 0 ? false : true); }