Segment.cs 1.0 KB

1234567891011121314151617181920212223242526272829
  1. // Traffic Simulation
  2. // https://github.com/mchrbn/unity-traffic-simulation
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. namespace TrafficSimulation {
  6. public class Segment : MonoBehaviour {
  7. public List<Segment> nextSegments;
  8. [HideInInspector] public int id;
  9. [HideInInspector] public List<Waypoint> waypoints;
  10. public bool IsOnSegment(Vector3 _p){
  11. TrafficSystem ts = GetComponentInParent<TrafficSystem>();
  12. for(int i=0; i < waypoints.Count - 1; i++){
  13. float d1 = Vector3.Distance(waypoints[i].transform.position, _p);
  14. float d2 = Vector3.Distance(waypoints[i+1].transform.position, _p);
  15. float d3 = Vector3.Distance(waypoints[i].transform.position, waypoints[i+1].transform.position);
  16. float a = (d1 + d2) - d3;
  17. if(a < ts.segDetectThresh && a > -ts.segDetectThresh)
  18. return true;
  19. }
  20. return false;
  21. }
  22. }
  23. }