Advertisement
Guest User

Untitled

a guest
Jul 17th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 62.30 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System;
  5. using UnityEngine.UI;
  6.  
  7. public class CurveController : MonoBehaviour
  8. {
  9.  
  10. public List<GameObject> points;
  11. public List<GameObject> holders;
  12. public GameObject pointPrefab;
  13. public MainRightPanel mainGUI;
  14.  
  15. public Transform tractor;
  16. public Transform holder;
  17.  
  18. public Text metricsText;
  19.  
  20. public List<Transform> allPoints;
  21.  
  22. public static CurveController Instance;
  23.  
  24. public GameObject trakedObject;
  25.  
  26. public bool tracking = false;
  27.  
  28. public float StartingRotation = 0;
  29.  
  30. public Material APlusMaterial;
  31.  
  32. public Button aButton;
  33. public Button abButton;
  34.  
  35. public Material lineMaterial;
  36. public Material sideLineMaterial;
  37.  
  38. public GameObject uiCounter;
  39. public List<ParalelsCounter> counters;
  40. public ParalelsCounter centerCounter;
  41. public int countDelta = 0;
  42. private bool finishTracking = false;
  43. private float deltaLast = 0;
  44.  
  45. public List<Vector3> testPoints;
  46.  
  47. private bool needToTest = true;
  48.  
  49. private Dictionary<int, List<Vector3>> curveSaver;
  50.  
  51. void Start()
  52. {
  53. Instance = this;
  54.  
  55. StartCoroutine(TrackPosition());
  56.  
  57. if (SaveController.Instance.data.curves != null && SaveController.Instance.data.curves.Count > 0)
  58. {
  59. SetFromSave();
  60. aButton.interactable = false;
  61. abButton.interactable = false;
  62. }
  63.  
  64. if(testPoints.Count > 0 && needToTest)
  65. {
  66. Invoke("SpawnTest",1);
  67. }
  68.  
  69. curveSaver = new Dictionary<int, List<Vector3>>();
  70. }
  71.  
  72. public void SpawnTest()
  73. {
  74. foreach (Vector3 vec in testPoints)
  75. {
  76. SetPosition(vec);
  77. }
  78.  
  79. FinishTracking();
  80. }
  81.  
  82. public void CheckAfterMove()
  83. {
  84. if (SaveController.Instance.data.curves == null || SaveController.Instance.data.curves.Count == 0)
  85. {
  86. aButton.interactable = true;
  87. }
  88. }
  89.  
  90. IEnumerator TrackPosition()
  91. {
  92. while (true)
  93. {
  94. if (tracking && TractorController.Instance.trailer[0].transform.parent == null)
  95. {
  96. SetPosition(tractor);
  97. if (CheckIfPlayerLookToStart())
  98. {
  99.  
  100. holder.transform.GetChild(holder.transform.childCount - 1).SetParent(null);
  101. holder.transform.GetChild(holder.transform.childCount - 1).SetParent(null);
  102. FinishTracking();
  103. }
  104. }
  105. else
  106. {
  107. /*Transform gm = GetClosestPoint(allPoints);
  108. if (gm != null)
  109. {
  110. trakedObject = gm.gameObject;
  111. }*/
  112. }
  113. yield return new WaitForSeconds(1.5f);
  114. }
  115. }
  116.  
  117. private bool CheckIfPlayerLookToStart()
  118. {
  119. if (points.Count > 0)
  120. {
  121. Vector3 first = points[0].transform.eulerAngles;
  122. Vector3 second = TractorController.Instance.transform.eulerAngles;
  123.  
  124. float res = first.y - second.y;
  125. if (res < -180)
  126. res += 180;
  127. if (res > 180)
  128. {
  129. res -= 180;
  130. }
  131. /*if ((res < -65 && res > -115) || (res > 65 && res < 115) || (res <=-170 && res >=-180))
  132. return true;*/
  133. }
  134. return false;
  135. }
  136.  
  137. private void SetFromSave()
  138. {
  139. for (int i = 0; i < SaveController.Instance.data.curves.Count - 1; i++)
  140. {
  141. GameObject obj = GameObject.Instantiate(pointPrefab) as GameObject;
  142. obj.transform.position = new Vector3(SaveController.Instance.data.curves[i].position.x, SaveController.Instance.data.curves[i].position.y, SaveController.Instance.data.curves[i].position.z);
  143. //obj.transform.eulerAngles = new Vector3(p.rotation.x, p.rotation.y, p.rotation.z);
  144. SetPosition(obj.transform);
  145. }
  146.  
  147. FinishTracking();
  148. }
  149.  
  150. public void SetPosition(Transform position)
  151. {
  152. if (points.Count > 0)
  153. {
  154. if (position.position != points[points.Count - 1].transform.position)
  155. {
  156. GameObject obj = GameObject.Instantiate(pointPrefab, position.position, position.rotation) as GameObject;
  157. points.Add(obj);
  158.  
  159. if (points.Count == 1)
  160. {
  161. holder.transform.position = position.position;
  162. holder.transform.rotation = position.rotation;
  163. }
  164. obj.transform.SetParent(holder);
  165. }
  166. }
  167. else
  168. {
  169. StartingRotation = position.eulerAngles.y;
  170. GameObject obj = GameObject.Instantiate(pointPrefab, position.position, position.rotation) as GameObject;
  171. points.Add(obj);
  172.  
  173. if (points.Count == 1)
  174. {
  175. holder.transform.position = position.position;
  176. holder.transform.rotation = position.rotation;
  177. }
  178. obj.transform.SetParent(holder);
  179.  
  180. GameObject objj = GameObject.Instantiate(pointPrefab, position.position, position.rotation) as GameObject;
  181. objj.transform.eulerAngles = new Vector3(0, objj.transform.eulerAngles.y - 180, 0);
  182. objj.transform.localScale = new Vector3(18.83506f, 0.01f, 18.83506f);
  183. objj.GetComponent<Renderer>().material = APlusMaterial;
  184. }
  185.  
  186.  
  187. //if (points.Count >= 2)
  188. //{
  189. // IndicatorPanel.Instance.gameObject.SetActive(true);
  190. // SetRenderer();
  191. //}
  192. }
  193.  
  194. public void SetPosition(Vector3 position)
  195. {
  196. if (points.Count > 0)
  197. {
  198. if (position != points[points.Count - 1].transform.position)
  199. {
  200. GameObject obj = GameObject.Instantiate(pointPrefab) as GameObject;
  201. obj.transform.position = position;
  202. points.Add(obj);
  203.  
  204. if (points.Count == 1)
  205. {
  206. holder.transform.position = position;
  207. }
  208. obj.transform.SetParent(holder);
  209. }
  210. }
  211. else
  212. {
  213. GameObject obj = GameObject.Instantiate(pointPrefab) as GameObject;
  214. obj.transform.position = position;
  215. points.Add(obj);
  216.  
  217. if (points.Count == 1)
  218. {
  219. holder.transform.position = position;
  220. }
  221. obj.transform.SetParent(holder);
  222.  
  223. GameObject objj = GameObject.Instantiate(pointPrefab) as GameObject;
  224. objj.transform.position = position;
  225. objj.transform.eulerAngles = new Vector3(0, objj.transform.eulerAngles.y - 180, 0);
  226. objj.transform.localScale = new Vector3(18.83506f, 0.01f, 18.83506f);
  227. objj.GetComponent<Renderer>().material = APlusMaterial;
  228. }
  229.  
  230.  
  231. //if (points.Count >= 2)
  232. //{
  233. // IndicatorPanel.Instance.gameObject.SetActive(true);
  234. // SetRenderer();
  235. //}
  236. }
  237.  
  238. public void ResetSave()
  239. {
  240. if(SaveController.Instance.data.curves != null)
  241. SaveController.Instance.data.curves.Clear();
  242. if (holder.transform.childCount > 0)
  243. {
  244. for (int i = 0; i < holder.transform.childCount; i++)
  245. {
  246. SavePoint(holder.transform.GetChild(i).gameObject);
  247. }
  248. }
  249.  
  250. SaveController.Instance.InsertCurves();
  251. }
  252.  
  253. private void SavePoint(GameObject obj)
  254. {
  255. CurvePoint point = new CurvePoint();
  256. Vector vector = new Vector();
  257. Rotation rot = new Rotation();
  258. vector.x = obj.transform.position.x;
  259. vector.y = obj.transform.position.y;
  260. vector.z = obj.transform.position.z;
  261.  
  262. rot.x = obj.transform.eulerAngles.x;
  263. rot.y = obj.transform.eulerAngles.y;
  264. rot.z = obj.transform.eulerAngles.z;
  265.  
  266. point.position = vector;
  267. point.rotation = rot;
  268. SaveController.Instance.data.curves.Add(point);
  269. }
  270.  
  271. public void DestroyAll()
  272. {
  273. for (int i = holder.transform.childCount - 1; i >= 0; i--)
  274. {
  275. Destroy(holder.transform.GetChild(i).gameObject);
  276. }
  277. for (int i = 0; i < holders.Count; i++)
  278. {
  279. Destroy(holders[i]);
  280. }
  281. points.Clear();
  282. allPoints.Clear();
  283. holders.Clear();
  284. }
  285.  
  286. public void ResetCurves( GameObject parent)
  287. {
  288. if (parent == holder.gameObject)
  289. {
  290. return;
  291. }
  292.  
  293. if (parent.GetComponent<CurveCounter>().side == CurveSide.Left)
  294. {
  295. countDelta--;
  296. }
  297. else
  298. {
  299. countDelta++;
  300. }
  301.  
  302. centerCounter.SetText("");
  303. Destroy(parent.transform.GetChild(0).GetComponent<BoxCollider>());
  304. Destroy(parent.transform.GetChild(parent.transform.childCount - 1).GetComponent<BoxCollider>());
  305.  
  306.  
  307.  
  308. GameObject objj = new GameObject("startingPos");
  309. objj.transform.position = parent.transform.position;
  310. objj.transform.eulerAngles = parent.transform.eulerAngles;
  311. startingPos = objj;
  312. Destroy(holder.gameObject);
  313. holder = parent.transform;
  314. holders.Remove(parent);
  315. foreach (GameObject obj in holders)
  316. {
  317. Destroy(obj);
  318. }
  319.  
  320.  
  321.  
  322.  
  323. holders.Clear();
  324. allPoints.Clear();
  325. counters.Clear();
  326. holder.transform.position = objj.transform.position;
  327.  
  328. for (int i = 0; i < holder.transform.childCount; i++)
  329. {
  330. allPoints.Add(holder.transform.GetChild(i));
  331. }
  332.  
  333. FinishTracking();
  334.  
  335.  
  336.  
  337. SetMaterials();
  338. }
  339.  
  340. public Vector3 frontForward;
  341. public Vector3 rearForward;
  342.  
  343. public Vector3 frontEuler;
  344. public Vector3 rearEuler;
  345.  
  346. public void SetTrackedPoint(GameObject obj)
  347. {
  348. int i = -1;
  349. i = allPoints.IndexOf(obj.transform);
  350. if (i == -1)
  351. {
  352. return;
  353. }
  354. List<Transform> temp = new List<Transform>();
  355. temp.Add(allPoints[i]);
  356. if (i > 0)
  357. {
  358. temp.Add(allPoints[i - 1]);
  359. }
  360. if (i + 1 < allPoints.Count)
  361. {
  362. temp.Add(allPoints[i + 1]);
  363. }
  364. Transform gm = GetClosestPoint(allPoints);
  365. if (gm != null)
  366. {
  367. trakedObject = gm.gameObject;
  368. }
  369. }
  370.  
  371. public bool firstTime = true;
  372.  
  373. public GameObject startingPos = null;
  374.  
  375. public void SetBezierCurve()
  376. {
  377. /* List<Vector3> ppp = GetDrawingPoints();
  378. List<GameObject> forDestroy = new List<GameObject>();
  379. //holder.DetachChildren();
  380. for (int i = 1; i < ppp.Count; i++)
  381. {
  382. if (i < points.Count)
  383. {
  384. points[i].transform.position = ppp[i-1];
  385. points[i].name = points[i].name + "New";
  386. }
  387. else
  388. {
  389. GameObject obj = new GameObject("point");
  390. obj.transform.position = ppp[i - 1];// points[i];
  391. SetPosition(obj.transform);
  392. //forDestroy.Add(obj);
  393. }
  394. }
  395.  
  396. for (int i = forDestroy.Count - 1; i >= 0; i--)
  397. {
  398. points.Remove(forDestroy[i]);
  399. Destroy(forDestroy[i]);
  400. }*/
  401.  
  402. StartCoroutine(StartPoints());
  403. }
  404.  
  405. public IEnumerator StartPoints()
  406. {
  407. yield return new WaitForSeconds(0.5f);
  408. FinishTracking();
  409. }
  410.  
  411. float lastDeltaa = 0;
  412. private GameObject originalLine;
  413. public void FinishTracking()
  414. {
  415.  
  416. counters.Clear();
  417. tracking = false;
  418. if (firstTime)
  419. {
  420. Vector3 first = points[0].transform.eulerAngles;
  421. int j = 0;
  422. holder.transform.GetChild(holder.transform.childCount - 1).eulerAngles = holder.transform.GetChild(0).eulerAngles;
  423. // holder.transform.GetChild(j - 1).gameObject.SetActive(false);// = first;
  424. if (j - 1 < holder.childCount)
  425. {
  426. // holder.GetChild(j - 1).eulerAngles = first;
  427. //holder.GetChild(j - 2).eulerAngles = first;
  428. }
  429.  
  430. /* List<Vector3> position = new List<Vector3>();
  431.  
  432. foreach (GameObject o in points)
  433. {
  434. position.Add(o.transform.position);
  435. }
  436.  
  437. position = MakeSmoothCurve(position, 32f);
  438. points.Clear();
  439. for (int i = 0; i < position.Count; i++)
  440. {
  441. GameObject ob = new GameObject();
  442. ob.transform.SetParent(holder);
  443. points.Add(ob);
  444. points[i].transform.position = position[i];
  445. }*/
  446.  
  447. //MakeSmoothCurve(10f);
  448. /*for (int i = 0; i < holder.transform.childCount; i++)
  449. {
  450. holder.GetChild(i).eulerAngles = first;
  451. }*/
  452. SetLines(holder.gameObject);
  453.  
  454. for (int i = 0; i < holder.transform.childCount; i++)
  455. {
  456. if (i + 1 < holder.transform.childCount && holder.transform.GetChild(i + 1).gameObject.activeSelf)
  457. {
  458. GameObject obj1 = holder.transform.GetChild(i).gameObject;
  459. GameObject obj2 = holder.transform.GetChild(i + 1).gameObject;
  460.  
  461. obj1.transform.LookAt(obj2.transform);
  462.  
  463. if (obj1.GetComponent<BoxCollider>() != null)
  464. Destroy(obj1.GetComponent<BoxCollider>());
  465.  
  466. if (i < holder.transform.childCount - 3)
  467. {
  468.  
  469. BoxCollider col = obj1.AddComponent<BoxCollider>();
  470. obj1.GetComponent<BoxCollider>().isTrigger = true;
  471. col.isTrigger = true;
  472.  
  473. col.size = new Vector3(4, 1, Vector3.Distance(obj1.transform.position, obj2.transform.position));
  474. col.center = new Vector3(0, 0, Vector3.Distance(obj1.transform.position, obj2.transform.position) / 2);
  475. }
  476.  
  477.  
  478. }
  479.  
  480. allPoints.Add(holder.transform.GetChild(i));
  481. SavePoint(holder.transform.GetChild(i).gameObject);
  482. }
  483. firstTime = false;
  484.  
  485. foreach (var g in allPoints)
  486. {
  487. g.transform.SetParent(null);
  488. }
  489. holder.eulerAngles = allPoints[0].eulerAngles;
  490. foreach (var g in allPoints)
  491. {
  492. g.transform.SetParent(holder);
  493. }
  494.  
  495. GameObject counter = GameObject.Instantiate(uiCounter, holder.transform.GetChild(0).transform.position, holder.transform.GetChild(0).transform.rotation) as GameObject;
  496. //counter.transform.SetParent(holder.transform);
  497. counter.transform.localEulerAngles = Vector3.zero;
  498. counter.GetComponent<ParalelsCounter>().SetText("");
  499. counter.GetComponent<ParalelsCounter>().curve = true;
  500. centerCounter = counter.GetComponent<ParalelsCounter>();
  501.  
  502. findFinish = true;
  503. Transform gm = GetClosestPoint(allPoints);
  504. if (gm != null)
  505. {
  506. trakedObject = gm.gameObject;
  507. }
  508.  
  509. List<Vector3> pointsPos = new List<Vector3>();
  510. foreach(Transform t in holder)
  511. {
  512. pointsPos.Add(t.position);
  513. }
  514. curveSaver.Add(0, pointsPos);
  515.  
  516. frontForward = holder.GetChild(holder.childCount - 1).forward;
  517. rearForward = holder.GetChild(0).forward;
  518.  
  519. frontEuler = holder.GetChild(holder.childCount - 1).eulerAngles;
  520. rearEuler = holder.GetChild(0).eulerAngles;
  521. }
  522.  
  523. SetLines(holder.gameObject);
  524.  
  525. if (startingPos == null)
  526. {
  527. GameObject obj = new GameObject("startingPos");
  528. obj.transform.position = holder.transform.position;
  529. obj.transform.eulerAngles = holder.transform.eulerAngles;
  530. startingPos = obj;
  531. }
  532.  
  533. float deltaa = ((float)MetricsWindow.Instance.metrics + (float)MetricsWindow.Instance.sm / 100) * 6.278354f;
  534. lastDeltaa = deltaa;
  535. deltaLast = ((float)MetricsWindow.Instance.metrics + (float) MetricsWindow.Instance.sm / 100f);
  536.  
  537. if (holder.GetChild(holder.childCount - 1).localPosition.x < 0)
  538. {
  539. //deltaLast *= -1;
  540. }
  541. GameObject lastPart = null;
  542.  
  543. for (int i = 0; i < holder.transform.childCount - 2; i++)
  544. {
  545. Transform tempTransform = holder.transform.GetChild(i);
  546. tempTransform.LookAt(holder.transform.GetChild(i + 1));
  547. }
  548.  
  549. /*if (testPoints.Count == 0)
  550. {
  551. foreach(Transform tr in holder.transform)
  552. {
  553. testPoints.Add(tr.position);
  554. }
  555. }*/
  556.  
  557.  
  558.  
  559. for (int i = 0; i < 1; i++)
  560. {
  561. GameObject obj = CreateLines(holder.gameObject, startingPos.transform, deltaa * (i + 1), countDelta + i + 1, lastPart, countDelta + i + 1);
  562. /* GameObject counter = GameObject.Instantiate(uiCounter, obj.transform.GetChild(0).transform.position, obj.transform.GetChild(0).transform.rotation) as GameObject;
  563. //counter.transform.SetParent(obj.transform);
  564. counter.transform.localEulerAngles = Vector3.zero;
  565. counter.GetComponent<ParalelsCounter>().SetText(countDelta + i+1);
  566. counter.GetComponent<ParalelsCounter>().curve = true;
  567. counters.Add(counter.GetComponent<ParalelsCounter>());
  568. obj.GetComponent<CurveCounter>().side = CurveSide.Right; */
  569. lastPart = obj;
  570. }
  571. lastPart = null;
  572. for (int i = 0; i < 1; i++)
  573. {
  574. GameObject obj = CreateLines(holder.gameObject, startingPos.transform, deltaa * -(i + 1), countDelta - (i + 1), lastPart,countDelta - (i + 1));
  575. /*GameObject counter = GameObject.Instantiate(uiCounter, obj.transform.GetChild(0).transform.position, obj.transform.GetChild(0).transform.rotation) as GameObject;
  576. //counter.transform.SetParent(obj.transform);
  577. counter.transform.localEulerAngles = Vector3.zero;
  578. counter.GetComponent<ParalelsCounter>().SetText(countDelta - (i+1));
  579. counter.GetComponent<ParalelsCounter>().curve = true;
  580. counters.Add(counter.GetComponent<ParalelsCounter>());*/
  581. obj.GetComponent<CurveCounter>().side = CurveSide.Left;
  582. lastPart = obj;
  583. }
  584.  
  585. CheckLines();
  586. //CheckForTrigger();
  587. IndicatorPanel.Instance.gameObject.SetActive(true);
  588. TogglesPanel.Instance.gameObject.SetActive(true);
  589. RightPanel.Instance.SetABFalse();
  590. SetMaterials();
  591. curveCount = points.Count / 3;
  592. //DrawCurve();
  593. Transform gmm = GetClosestPoint(allPoints);
  594. if (gmm != null)
  595. {
  596. trakedObject = gmm.gameObject;
  597. }
  598. }
  599.  
  600. private void CheckLines()
  601. {
  602. foreach (GameObject obj in holders)
  603. {
  604. List<GameObject> toDel = new List<GameObject>();
  605.  
  606. for (int i = 0; i < obj.transform.childCount; i++)
  607. {
  608.  
  609. for (int j = holders.Count - 1; j >= 0; j--)
  610. {
  611. if(holders[j] != obj)
  612. for (int k = 0; k < holders[j].transform.childCount; k++)
  613. {
  614. if (Vector3.Distance(obj.transform.GetChild(i).position, holders[j].transform.GetChild(k).position) < lastDeltaa * 0.99f)
  615. {
  616. toDel.Add(obj.transform.GetChild(i).gameObject);
  617. }
  618. }
  619. }
  620. }
  621.  
  622.  
  623. for (int i = 0; i < obj.transform.childCount; i++)
  624. {
  625.  
  626. for (int j = 0; j < holder.childCount; j++)
  627. {
  628. if (Vector3.Distance(obj.transform.GetChild(i).position, holder.GetChild(j).position) < lastDeltaa * 0.99f)
  629. {
  630. toDel.Add(obj.transform.GetChild(i).gameObject);
  631. }
  632.  
  633. }
  634. }
  635.  
  636.  
  637. foreach (GameObject go in toDel)
  638. {
  639. go.transform.SetParent(null);
  640. Destroy(go);
  641. }
  642. toDel.Clear();
  643.  
  644.  
  645.  
  646. try
  647. {
  648. if (holder.childCount / obj.transform.childCount >= 2)
  649. {
  650. obj.SetActive(false);
  651. }
  652. }
  653. catch (Exception ex)
  654. {
  655. obj.SetActive(false);
  656. }
  657. }
  658.  
  659.  
  660. foreach (GameObject obj in holders)
  661. {
  662. SetLines(obj);
  663. }
  664. }
  665.  
  666. private void CheckForTrigger()
  667. {
  668. GameObject holderLeft = holders[0];
  669. GameObject holderRight = holders[1];
  670.  
  671. GameObject leftRear = holderLeft.transform.GetChild(0).gameObject;
  672. GameObject leftFront = holderLeft.transform.GetChild(holderLeft.transform.childCount - 1).gameObject;
  673.  
  674. GameObject rightRear = holderRight.transform.GetChild(0).gameObject;
  675. GameObject rightFront = holderRight.transform.GetChild(holderRight.transform.childCount - 1).gameObject;
  676.  
  677. //leftRear.transform.eulerAngles = rearEuler;
  678. //rightRear.transform.eulerAngles = rearEuler;
  679.  
  680. //leftFront.transform.eulerAngles = frontEuler;
  681. //rightFront.transform.eulerAngles = frontEuler;
  682.  
  683. RaycastHit hit = new RaycastHit();
  684.  
  685. Debug.DrawRay(leftRear.transform.position + (-leftRear.transform.forward) , -leftRear.transform.forward * lastDeltaa, Color.blue, 15);
  686. Debug.DrawRay(rightRear.transform.position + (-rightRear.transform.forward), -rightRear.transform.forward * lastDeltaa, Color.blue, 15);
  687. Debug.DrawRay(leftFront.transform.position + leftFront.transform.forward, leftFront.transform.forward * lastDeltaa, Color.blue, 15);
  688. Debug.DrawRay(rightFront.transform.position + rightFront.transform.forward, rightFront.transform.forward * lastDeltaa, Color.blue, 15);
  689.  
  690. //Rear
  691. if (Physics.Raycast(new Ray(leftRear.transform.position+(-leftRear.transform.forward), -leftRear.transform.forward * lastDeltaa),out hit))
  692. {
  693. if(hit.collider.tag != "Curves" || hit.collider.transform.parent == holder)
  694. {
  695. SetCollider(leftRear, 5000, -2500, holder.GetChild(0).eulerAngles);
  696. }
  697. else
  698. {
  699. SetCollider(leftRear, -hit.distance, -hit.distance / 2, holder.GetChild(0).eulerAngles);
  700. }
  701. }
  702. else
  703. {
  704. SetCollider(leftRear, 5000, -2500, holder.GetChild(0).eulerAngles);
  705. }
  706.  
  707. if (Physics.Raycast(new Ray(rightRear.transform.position+ (-rightRear.transform.forward), -rightRear.transform.forward * lastDeltaa), out hit))
  708. {
  709. if (hit.collider.tag != "Curves" || hit.collider.transform.parent == holder)
  710. {
  711. SetCollider(rightRear, 5000, -2500, holder.GetChild(0).eulerAngles);
  712. }
  713. else
  714. {
  715. SetCollider(rightRear, -hit.distance, -hit.distance / 2, holder.GetChild(0).eulerAngles);
  716. }
  717. }
  718. else
  719. {
  720. SetCollider(rightRear, 5000, -2500, holder.GetChild(0).eulerAngles);
  721. }
  722.  
  723.  
  724. //Front
  725. if (Physics.Raycast(new Ray(leftFront.transform.position + leftFront.transform.forward, leftFront.transform.forward * lastDeltaa), out hit))
  726. {
  727. if (hit.collider.tag != "Curves" || hit.collider.transform.parent == holder)
  728. {
  729. SetCollider(leftFront, 5000, 2500, holder.GetChild(holder.childCount - 1).eulerAngles);
  730. }
  731. else
  732. {
  733. SetCollider(leftFront, hit.distance, hit.distance / 2, holder.GetChild(holder.childCount - 1).eulerAngles);
  734. }
  735. }
  736. else
  737. {
  738. SetCollider(leftFront, 5000, 2500, holder.GetChild(holder.childCount - 1).eulerAngles);
  739. }
  740.  
  741. if (Physics.Raycast(new Ray(rightFront.transform.position+ rightFront.transform.forward, rightFront.transform.forward * lastDeltaa), out hit))
  742. {
  743. if (hit.collider.tag != "Curves" || hit.collider.transform.parent == holder)
  744. {
  745. SetCollider(rightFront, 5000, 2500, holder.GetChild(holder.childCount - 1).eulerAngles);
  746. }
  747. else
  748. {
  749. SetCollider(rightFront, hit.distance, hit.distance / 2, holder.GetChild(holder.childCount - 1).eulerAngles);
  750. }
  751. }
  752. else
  753. {
  754. SetCollider(rightFront, 5000, 2500, holder.GetChild(holder.childCount - 1).eulerAngles);
  755. }
  756. }
  757.  
  758. private void SetCollider(GameObject obj, float size, float offset, Vector3 eulerAngles)
  759. {
  760. BoxCollider col = obj.AddComponent<BoxCollider>();
  761. obj.GetComponent<BoxCollider>().isTrigger = true;
  762. col.isTrigger = true;
  763. col.size = new Vector3(4, 1,size);
  764. col.center = new Vector3(0, 0, offset);
  765. //obj.transform.eulerAngles = eulerAngles;
  766. }
  767.  
  768.  
  769. public void SetMaterials()
  770. {
  771. for (int i = 0; i < holders.Count; i++)
  772. {
  773. if(holders[i].activeSelf)
  774. holders[i].transform.GetChild(0).GetComponent<LineRenderer>().material = sideLineMaterial;
  775. }
  776. holder.transform.GetChild(0).GetComponent<LineRenderer>().material = lineMaterial;
  777. }
  778.  
  779. public List<CRSpline> spines;
  780.  
  781.  
  782. private GameObject CreateLines(GameObject hold, Transform po, float delta = 0, int index = 0, GameObject lastPart = null, int positiveIndex = 0)
  783. {
  784. List<Vector3> pointsPos = new List<Vector3>();
  785. GameObject obj = new GameObject("Curve" + index);// GameObject.Instantiate(hold) as GameObject;
  786. obj.AddComponent<CurveCounter>();
  787. obj.transform.localScale = new Vector3(1, 1, 1);
  788.  
  789. if (curveSaver.ContainsKey(index))
  790. {
  791. pointsPos = curveSaver[index];
  792. for(int i = 0; i < pointsPos.Count; i++)
  793. {
  794. GameObject tempPoint = Instantiate(pointPrefab);
  795. tempPoint.tag = "Curves";
  796. tempPoint.transform.position = pointsPos[i];
  797. tempPoint.transform.SetParent(obj.transform);
  798. }
  799.  
  800. for (int i = 0; i < obj.transform.childCount - 2; i++)
  801. {
  802. Transform tempTransform = obj.transform.GetChild(i);
  803. tempTransform.LookAt(obj.transform.GetChild(i + 1));
  804. }
  805.  
  806. for (int i = 0; i < obj.transform.childCount; i++)
  807. {
  808. if (i + 1 < obj.transform.childCount && obj.transform.GetChild(i + 1).gameObject.activeSelf && i != 0)
  809. {
  810. GameObject obj1 = obj.transform.GetChild(i).gameObject;
  811. GameObject obj2 = obj.transform.GetChild(i + 1).gameObject;
  812.  
  813. obj1.transform.LookAt(obj2.transform);
  814.  
  815. if (obj1.GetComponent<BoxCollider>() != null)
  816. Destroy(obj1.GetComponent<BoxCollider>());
  817.  
  818. if (i < obj.transform.childCount - 1)
  819. {
  820. BoxCollider col = obj1.AddComponent<BoxCollider>();
  821. obj1.GetComponent<BoxCollider>().isTrigger = true;
  822. col.isTrigger = true;
  823. col.size = new Vector3(4, 1, Vector3.Distance(obj1.transform.position, obj2.transform.position) - 0.1f);
  824. col.center = new Vector3(0, 0, Vector3.Distance(obj1.transform.position, obj2.transform.position) / 2 - 0.05f);
  825. }
  826.  
  827. }
  828. /*if (i == obj.transform.childCount - 1)
  829. {
  830. GameObject objj = obj.transform.GetChild(i).gameObject;
  831. BoxCollider col = objj.AddComponent<BoxCollider>();
  832. objj.GetComponent<BoxCollider>().isTrigger = true;
  833. col.isTrigger = true;
  834. col.size = new Vector3(4, 1, 50000);
  835. col.center = new Vector3(0, 0, 50000 / 2);
  836. }
  837. if (i == 0)
  838. {
  839. GameObject objj = obj.transform.GetChild(i).gameObject;
  840. BoxCollider col = objj.AddComponent<BoxCollider>();
  841. objj.GetComponent<BoxCollider>().isTrigger = true;
  842. col.isTrigger = true;
  843. col.size = new Vector3(4, 1, 50000);
  844. col.center = new Vector3(0, 0, -50000 / 2);
  845. }*/
  846.  
  847.  
  848.  
  849. }
  850.  
  851. foreach(Transform t in holder)
  852. {
  853. SavePoint(t.gameObject);
  854. }
  855. }
  856. else
  857. {
  858.  
  859. float deltaa = ((float)MetricsWindow.Instance.metrics + (float)MetricsWindow.Instance.sm / 100) * 6.278354f;
  860.  
  861. for (int i = 0; i < holder.transform.childCount; i++)
  862. {
  863.  
  864. Transform tempTransform = holder.GetChild(i);
  865. if (!tempTransform.name.StartsWith("Paralels"))
  866. {
  867. GameObject tempPoint = Instantiate(tempTransform.gameObject);
  868. tempPoint.tag = "Curves";
  869. Vector3 tempPos = Vector3.zero;
  870. int vectCount = 0;
  871.  
  872. if (i == 0)
  873. {
  874. tempPoint.transform.position = holder.transform.GetChild(0).position + holder.transform.GetChild(0).right * (delta);
  875. obj.transform.position = tempPoint.transform.position;
  876. }
  877. else
  878. {
  879. tempPoint.transform.position = holder.transform.GetChild(i).position + holder.transform.GetChild(i).right * (delta);
  880. }
  881.  
  882. tempPoint.transform.SetParent(obj.transform);
  883. }
  884. }
  885.  
  886. List<GameObject> toDel = new List<GameObject>();
  887.  
  888. for (int i = 0; i < obj.transform.childCount; i++)
  889. {
  890.  
  891. for (int j = 0; j < holders.Count; j++)
  892. {
  893. for (int k = 0; k < holders[j].transform.childCount; k++)
  894. {
  895. if (Vector3.Distance(obj.transform.GetChild(i).position, holders[j].transform.GetChild(k).position) < deltaa * 0.99f)
  896. {
  897. toDel.Add(obj.transform.GetChild(i).gameObject);
  898. }
  899. }
  900. }
  901. }
  902.  
  903.  
  904. for (int i = 0; i < obj.transform.childCount; i++)
  905. {
  906.  
  907. for (int j = 0; j < holder.childCount; j++)
  908. {
  909. if (Vector3.Distance(obj.transform.GetChild(i).position, holder.GetChild(j).position) < deltaa * 0.99f)
  910. {
  911. toDel.Add(obj.transform.GetChild(i).gameObject);
  912. }
  913.  
  914. }
  915. }
  916.  
  917.  
  918. foreach (GameObject go in toDel)
  919. {
  920. go.transform.SetParent(null);
  921. Destroy(go);
  922. }
  923. toDel.Clear();
  924.  
  925. for (int i = 0; i < obj.transform.childCount; i++)
  926. {
  927. for (int j = 0; j < obj.transform.childCount ; j++)
  928. {
  929. GameObject obj1 = obj.transform.GetChild(i).gameObject;
  930. GameObject obj2 = obj.transform.GetChild(j).gameObject;
  931. obj1.transform.LookAt(obj2.transform);
  932. if (i != j && Vector3.Distance(obj.transform.GetChild(i).position, obj.transform.GetChild(j).position) < MinDist)
  933. {
  934. //obj.transform.GetChild(i).SetParent(obj.transform.GetChild(j));
  935. toDel.Add(obj.transform.GetChild(i).gameObject);
  936. }
  937. }
  938.  
  939. }
  940. foreach (GameObject go in toDel)
  941. {
  942. go.transform.SetParent(null);
  943. Destroy(go);
  944. }
  945. toDel.Clear();
  946.  
  947.  
  948. for (int i = 0; i < obj.transform.childCount; i++)
  949. {
  950. if (i + 1 < obj.transform.childCount && obj.transform.GetChild(i + 1).gameObject.activeSelf)
  951. {
  952. GameObject obj1 = obj.transform.GetChild(i).gameObject;
  953. GameObject obj2 = obj.transform.GetChild(i + 1).gameObject;
  954.  
  955. obj1.transform.LookAt(obj2.transform);
  956.  
  957. if (obj1.GetComponent<BoxCollider>() != null)
  958. Destroy(obj1.GetComponent<BoxCollider>());
  959.  
  960. if (i < obj.transform.childCount - 1)
  961. {
  962. BoxCollider col = obj1.AddComponent<BoxCollider>();
  963. obj1.GetComponent<BoxCollider>().isTrigger = true;
  964. col.isTrigger = true;
  965. col.size = new Vector3(4, 1, Vector3.Distance(obj1.transform.position, obj2.transform.position) - 0.1f);
  966. col.center = new Vector3(0, 0, Vector3.Distance(obj1.transform.position, obj2.transform.position) / 2 - 0.05f);
  967. }
  968.  
  969. }
  970.  
  971. SavePoint(obj.transform.GetChild(i).gameObject);
  972. pointsPos.Add(obj.transform.GetChild(i).position);
  973. }
  974.  
  975.  
  976. }
  977.  
  978. try
  979. {
  980. if (hold.transform.childCount / obj.transform.childCount >= 3)
  981. {
  982. obj.SetActive(false);
  983. Debug.Log("SetActive false index:" + index);
  984. }
  985. }
  986. catch (Exception ex)
  987. {
  988. obj.SetActive(false);
  989. Debug.Log("SetActive false index:" + index);
  990. }
  991.  
  992. if(!curveSaver.ContainsKey(index) && obj.activeSelf)
  993. {
  994. curveSaver.Add(index, pointsPos);
  995. }
  996.  
  997. holders.Add(obj);
  998. //SetLines(obj);
  999. return obj;
  1000. }
  1001.  
  1002. private float lastScale = 1;
  1003.  
  1004. Transform forRotate = null;
  1005. bool findFinish = false;
  1006. GameObject lastObj = null;
  1007.  
  1008. private float MinDist = 4f;
  1009.  
  1010. private void SetLines(GameObject hold)
  1011. {
  1012. try
  1013. {
  1014. if (!hold.gameObject.activeSelf)
  1015. {
  1016. return;
  1017. }
  1018.  
  1019. lastObj = null;
  1020. int k = 0;
  1021. for (int i = 0; i < hold.transform.childCount; i++)
  1022. {
  1023. if (hold.transform.GetChild(i).gameObject.activeSelf)
  1024. {
  1025. lastObj = hold.transform.GetChild(i).gameObject;
  1026. k = i;
  1027. }
  1028.  
  1029. }
  1030. hold.transform.GetChild(hold.transform.childCount - 2).LookAt(hold.transform.GetChild(hold.transform.childCount - 1));
  1031.  
  1032. hold.transform.GetChild(0).LookAt(hold.transform.GetChild(1));
  1033. hold.transform.GetChild(hold.transform.childCount - 1).eulerAngles = hold.transform.GetChild(0).eulerAngles;
  1034. hold.transform.GetChild(hold.transform.childCount - 1).eulerAngles = hold.transform.GetChild(hold.transform.childCount - 2).eulerAngles;
  1035. //lastObj.transform.eulerAngles = hold.transform.GetChild(0).eulerAngles;
  1036. LineRenderer renderer = hold.transform.GetChild(0).GetComponent<LineRenderer>();
  1037. if (renderer == null)
  1038. renderer = hold.transform.GetChild(0).gameObject.AddComponent<LineRenderer>();//.GetComponent<LineRenderer>();
  1039.  
  1040. renderer.positionCount = hold.transform.childCount;
  1041. renderer.material = lineMaterial;
  1042. renderer.endWidth = 1.5f;
  1043. renderer.startWidth = 1.5f;
  1044.  
  1045. if (CheckWorkType.Instance.typeId == 8)
  1046. {
  1047. renderer.endWidth = 1.5f * 3;
  1048. renderer.startWidth = 1.5f * 3;
  1049. }
  1050.  
  1051. if (firstTime)
  1052. {
  1053. //hold.transform.GetChild(0).position = hold.transform.GetChild(0).position - hold.transform.GetChild(0).forward * 50000;
  1054. hold.transform.GetChild(0).LookAt(hold.transform.GetChild(1));
  1055. for (int i = 0; i < hold.transform.childCount; i++)
  1056. {
  1057.  
  1058. renderer.SetPosition(i, hold.transform.GetChild(i).position);
  1059.  
  1060. }
  1061. }
  1062. else
  1063. {
  1064. for (int i = 0; i < hold.transform.childCount; i++)
  1065. {
  1066.  
  1067. renderer.SetPosition(i, hold.transform.GetChild(i).position);
  1068.  
  1069. }
  1070. }
  1071.  
  1072. if (!findFinish)
  1073. {
  1074. //hold.transform.GetChild(hold.transform.childCount - 1).position = hold.transform.GetChild(hold.transform.childCount - 1).position + hold.transform.GetChild(hold.transform.childCount - 1).forward * 10000;
  1075. }
  1076. //renderer.SetPosition(hold.transform.childCount - 1, hold.transform.GetChild(hold.transform.childCount - 1).position);
  1077. }
  1078. catch { }
  1079. }
  1080.  
  1081. public void StartTracking()
  1082. {
  1083. if (PlayerPrefs.GetInt("VelestorDevice") == 1)
  1084. {
  1085. tracking = true;
  1086. RightPanel.Instance.OnABClick();
  1087. aButton.interactable = false;
  1088. }
  1089. else
  1090. {
  1091. MainGUI.Instance.StartShowCantOpen();
  1092. }
  1093.  
  1094. }
  1095.  
  1096. // For tracking
  1097.  
  1098. public void SetTrackedTarget(GameObject obj)
  1099. {
  1100. foreach (GameObject objj in holders)
  1101. {
  1102. for (int i = 0; i < objj.transform.childCount; i++)
  1103. {
  1104. if (objj.transform.GetChild(i).gameObject == obj)
  1105. {
  1106. if (i + 1 < objj.transform.childCount)
  1107. {
  1108. trakedObject = objj.transform.GetChild(i + 1).gameObject;
  1109. }
  1110. else
  1111. {
  1112. trakedObject = objj.transform.GetChild(i).gameObject;
  1113. }
  1114. break;
  1115. }
  1116. }
  1117. }
  1118. }
  1119.  
  1120. Transform GetClosestPoint(List<Transform> enemies)
  1121. {
  1122. Transform tMin = null;
  1123. float minDist = Mathf.Infinity;
  1124. Vector3 currentPos = transform.position;
  1125. foreach (Transform t in enemies)
  1126. {
  1127. if (t != null && t.name != "ParalelsCounter(Clone)")
  1128. {
  1129. float dist = Vector3.Distance(t.position, tractor.position);
  1130. if (dist < minDist)
  1131. {
  1132. tMin = t;
  1133. minDist = dist;
  1134. }
  1135. }
  1136. }
  1137.  
  1138. if (tMin == null && allPoints.Count > 0)
  1139. return allPoints[0];
  1140. return tMin;
  1141. }
  1142.  
  1143. Transform GetClosestPointByDistance(List<Transform> enemies)
  1144. {
  1145. Transform tMin = null;
  1146. float minDist = Mathf.Infinity;
  1147. Vector3 currentPos = transform.position;
  1148. foreach (Transform t in enemies)
  1149. {
  1150. if (t != null && t.name != "ParalelsCounter(Clone)")
  1151. {
  1152. float dist = Vector3.Distance(t.position, tractor.position);
  1153. if (dist < minDist)
  1154. {
  1155. tMin = t;
  1156. minDist = dist;
  1157. }
  1158. }
  1159. }
  1160.  
  1161. if (tMin == null && allPoints.Count > 0)
  1162. return allPoints[0];
  1163. return tMin;
  1164. }
  1165.  
  1166. bool FrontTest(Transform otherTransform)
  1167. {
  1168. Vector3 fwd = TractorController.Instance.transform.forward;
  1169. Vector3 vec = otherTransform.position - TractorController.Instance.transform.position;
  1170. vec.Normalize();
  1171.  
  1172.  
  1173. float ang = Mathf.Acos(Vector3.Dot(fwd, vec)) * Mathf.Rad2Deg;
  1174.  
  1175. bool isFront = false;
  1176.  
  1177. if (ang <= 35f)
  1178. return true;
  1179.  
  1180. return false;
  1181. }
  1182.  
  1183. public Vector3 Ortho(Vector3 v1, Vector3 v2, Vector3 v3)
  1184. {
  1185. // Длины отрезков.
  1186. var c = Math.Sqrt(Math.Pow(v1.x - v2.x, 2) + Math.Pow(v1.y - v2.y, 2) + Math.Pow(v1.z - v2.z, 2));
  1187. var b = Math.Sqrt(Math.Pow(v2.x - v3.x, 2) + Math.Pow(v2.y - v3.y, 2) + Math.Pow(v2.z - v3.z, 2));
  1188. var a = Math.Sqrt(Math.Pow(v3.x - v1.x, 2) + Math.Pow(v3.y - v1.y, 2) + Math.Pow(v3.z - v1.z, 2));
  1189.  
  1190. // Полупериметр треугольника.
  1191. var p = (a + b + c) / 2;
  1192. // Высота (длина перпендикуляра).
  1193. var h = 2 * Math.Sqrt(p * (p - a) * (p - b) * (p - c)) / c;
  1194.  
  1195. float x, y, z = 0;
  1196.  
  1197. if (a / c > b / c)
  1198. {
  1199. var c1 = Math.Sqrt(a * a - h * h);
  1200. float k = (float)c1 / (float)c;
  1201. x = v1.x + (float)k * (v2.x - v1.x);
  1202. y = v1.y + k * (v2.y - v1.y);
  1203. z = v1.z + k * (v2.z - v1.z);
  1204. }
  1205. else
  1206. {
  1207. var c2 = Math.Sqrt(b * b - h * h);
  1208. float k = (float)c2 / (float)c;
  1209. x = v2.x + k * (v1.x - v2.x);
  1210. y = v2.y + k * (v1.y - v2.y);
  1211. z = v2.z + k * (v1.z - v2.z);
  1212. }
  1213.  
  1214. return new Vector3(x, y, z);
  1215. }
  1216.  
  1217. private List<Transform> GetNearestPoints()
  1218. {
  1219. List<Transform> p = new List<Transform>();
  1220.  
  1221. int index = allPoints.IndexOf(trakedObject.transform);
  1222. if (index > 0)
  1223. {
  1224. p.Add(allPoints[index - 1]);
  1225. }
  1226. else
  1227. {
  1228. p.Add(allPoints[index]);
  1229. }
  1230.  
  1231. if (index != allPoints.Count - 1)
  1232. {
  1233. p.Add(allPoints[index + 1]);
  1234. }
  1235. else
  1236. {
  1237. p.Add(allPoints[index]);
  1238. }
  1239.  
  1240. return p;
  1241. }
  1242.  
  1243. private List<Transform> GetNearestPointsByPoint(Vector3 pos)
  1244. {
  1245. List<Transform> p = new List<Transform>();
  1246.  
  1247. float minDist = 999;
  1248. Transform nearestTransform = null;
  1249.  
  1250. foreach (Transform t in holder.transform)
  1251. {
  1252. float dist = Vector3.Distance(t.position, pos);
  1253. if(dist < minDist)
  1254. {
  1255. minDist = dist;
  1256. nearestTransform = t;
  1257. }
  1258. }
  1259.  
  1260. int index = allPoints.IndexOf(nearestTransform);
  1261.  
  1262. if(index == -1)
  1263. {
  1264. return p;
  1265. }
  1266.  
  1267. if (index > 0)
  1268. {
  1269. p.Add(allPoints[index - 1]);
  1270. }
  1271. else
  1272. {
  1273. p.Add(allPoints[index]);
  1274. }
  1275.  
  1276. if (index != allPoints.Count - 1)
  1277. {
  1278. p.Add(allPoints[index + 1]);
  1279. }
  1280. else
  1281. {
  1282. p.Add(allPoints[index]);
  1283. }
  1284.  
  1285. return p;
  1286. }
  1287.  
  1288. private float GetNearestDistanceByPoint(Vector3 pos)
  1289. {
  1290. List<Transform> p = new List<Transform>();
  1291.  
  1292. float minDist = 999;
  1293.  
  1294. foreach (Transform t in holder.transform)
  1295. {
  1296. float dist = Vector3.Distance(t.position, pos);
  1297. if (dist < minDist)
  1298. {
  1299. minDist = dist;
  1300. }
  1301. }
  1302. return minDist;
  1303. }
  1304.  
  1305. void SpawnFirstPoint()
  1306. {
  1307. Transform tempTransform = holder.transform.GetChild(0);
  1308. GameObject tempPoint = Instantiate(tempTransform.gameObject);
  1309. tempPoint.name = "TestPoint";
  1310. tempPoint.transform.position = holder.transform.GetChild(0).position + holder.transform.GetChild(0).right * lastDeltaa;
  1311. }
  1312.  
  1313. Vector3 FindIntersection(Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4)
  1314. {
  1315. float xD1, yD1, xD2, yD2, xD3, yD3;
  1316. float dot, deg, len1, len2;
  1317. float segmentLen1, segmentLen2;
  1318. float ua, ub, div;
  1319.  
  1320. // calculate differences
  1321. xD1 = p2.x - p1.x;
  1322. xD2 = p4.x - p3.x;
  1323. yD1 = p2.y - p1.y;
  1324. yD2 = p4.y - p3.y;
  1325. xD3 = p1.x - p3.x;
  1326. yD3 = p1.y - p3.y;
  1327.  
  1328. // calculate the lengths of the two lines
  1329. len1 = Mathf.Sqrt(xD1 * xD1 + yD1 * yD1);
  1330. len2 = Mathf.Sqrt(xD2 * xD2 + yD2 * yD2);
  1331.  
  1332. // calculate angle between the two lines.
  1333. dot = (xD1 * xD2 + yD1 * yD2); // dot product
  1334. deg = dot / (len1 * len2);
  1335.  
  1336. // if abs(angle)==1 then the lines are parallell,
  1337. // so no intersection is possible
  1338. if (Mathf.Abs(deg) == 1) return Vector3.zero;
  1339.  
  1340. // find intersection Pt between two lines
  1341. Vector3 pt = new Vector3(0, 0, 0);
  1342. div = yD2 * xD1 - xD2 * yD1;
  1343. ua = (xD2 * yD3 - yD2 * xD3) / div;
  1344. ub = (xD1 * yD3 - yD1 * xD3) / div;
  1345. pt.x = p1.x + ua * xD1;
  1346. pt.y = p1.y + ua * yD1;
  1347.  
  1348. // calculate the combined length of the two segments
  1349. // between Pt-p1 and Pt-p2
  1350. xD1 = pt.x - p1.x;
  1351. xD2 = pt.x - p2.x;
  1352. yD1 = pt.y - p1.y;
  1353. yD2 = pt.y - p2.y;
  1354. segmentLen1 = Mathf.Sqrt(xD1 * xD1 + yD1 * yD1) + Mathf.Sqrt(xD2 * xD2 + yD2 * yD2);
  1355.  
  1356. // calculate the combined length of the two segments
  1357. // between Pt-p3 and Pt-p4
  1358. xD1 = pt.x - p3.x;
  1359. xD2 = pt.x - p4.x;
  1360. yD1 = pt.y - p3.y;
  1361. yD2 = pt.y - p4.y;
  1362. segmentLen2 = Mathf.Sqrt(xD1 * xD1 + yD1 * yD1) + Mathf.Sqrt(xD2 * xD2 + yD2 * yD2);
  1363.  
  1364. // if the lengths of both sets of segments are the same as
  1365. // the lenghts of the two lines the point is actually
  1366. // on the line segment.
  1367.  
  1368. // if the point isn't on the line, return null
  1369. if (Mathf.Abs(len1 - segmentLen1) > 0.01 || Mathf.Abs(len2 - segmentLen2) > 0.01)
  1370. return Vector3.zero;
  1371.  
  1372. // return the valid intersection
  1373. return pt;
  1374. }
  1375.  
  1376. CRSpline RemoveLoopsFromSpline(CRSpline spline)
  1377. {
  1378. Vector3 intersection;
  1379. bool intersected;
  1380. List<Vector3> points = new List<Vector3>();
  1381. for (int i = 0; i < spline.pts.Length - 1; i++)
  1382. {
  1383. intersected = false;
  1384. for (int j = i + 2; j < spline.pts.Length - 1; j++)
  1385. {
  1386. // I didn't have a nice FindIntersections code so hacked together.
  1387. // Returns Vector.zero when it can't find an intersection... obviously this
  1388. // could cause issues. Should return a struct or class.
  1389. // Good enough for illustrative purposes.
  1390. intersection = FindIntersection(spline.pts[i], spline.pts[i + 1], spline.pts[j], spline.pts[j + 1]);
  1391. if (intersection != Vector3.zero)
  1392. {
  1393. intersected = true;
  1394. points.Add(spline.pts[i]);
  1395. points.Add(intersection);
  1396. i = j;
  1397. break;
  1398. }
  1399. }
  1400. if (!intersected) points.Add(spline.pts[i]);
  1401. }
  1402. points.Add(spline.pts[spline.pts.Length - 1]);
  1403. return new CRSpline(points.ToArray());
  1404. }
  1405.  
  1406. private void DrawLine(float lenght)
  1407. {
  1408. for (int i = 0; i < spines.Count; i++)
  1409. {
  1410. List<Vector3> newPos = new List<Vector3>();
  1411. //debug draw bottom obstacle line
  1412. for (int j = 0; j < spines[i].pts.Length; j++)
  1413. {
  1414. float fPos = (1.0f / (spines[i].pts.Length)) * j;
  1415.  
  1416. Vector3 ptOnLine = spines[i].Interp(fPos);
  1417. Vector3 direction = spines[i].Velocity(fPos);
  1418. Vector3 sideways = Vector3.Cross(Vector3.up, direction).normalized;
  1419. Vector3 v3Pos1 = ptOnLine - sideways * (lastDeltaa * lenght);
  1420.  
  1421. fPos = (1.0f / (spines[i].pts.Length)) * (j + 1);
  1422.  
  1423. ptOnLine = spines[i].Interp(fPos);
  1424. direction = spines[i].Velocity(fPos);
  1425. sideways = Vector3.Cross(Vector3.up, direction).normalized;
  1426. Vector3 v3Pos2 = ptOnLine - sideways * (lastDeltaa * lenght);
  1427.  
  1428. newPos.Add(new Vector3(v3Pos1.x,0.5f, v3Pos1.z));
  1429. }
  1430. CRSpline spline = new CRSpline(newPos.ToArray());
  1431. spline = RemoveLoopsFromSpline(spline);
  1432. spline = RemoveLoopsFromSpline(spline);
  1433. spline = RemoveLoopsFromSpline(spline);
  1434. spline = RemoveLoopsFromSpline(spline);
  1435. spline = RemoveLoopsFromSpline(spline);
  1436. for (int j = 0; j < spline.pts.Length - 1; j++)
  1437. {
  1438. Debug.DrawLine(spline.pts[j], spline.pts[j + 1], Color.blue, 3);
  1439. }
  1440. }
  1441. }
  1442.  
  1443. private GameObject GetNearestHolder()
  1444. {
  1445. GameObject nearestHolder = null;
  1446. float minDistance = 9999;
  1447.  
  1448. for (int i = 0; i <= 1; i++)
  1449. {
  1450. foreach (Transform t in holders[i].transform)
  1451. {
  1452. float tempDist = Vector3.Distance(TractorController.Instance.trailer[0].transform.position, t.position);
  1453. if (tempDist < minDistance)
  1454. {
  1455. minDistance = tempDist;
  1456. nearestHolder = t.parent.gameObject;
  1457. }
  1458. }
  1459. }
  1460.  
  1461. return nearestHolder;
  1462. }
  1463.  
  1464. private void CheckForRebuildLines(List<Transform> nearestPoints,Vector3 ortho)
  1465. {
  1466. Transform forwardPoint = holder.GetChild(holder.childCount - 1);
  1467. Transform rearPoint = holder.GetChild(0);
  1468.  
  1469. if (nearestPoints[0] != forwardPoint && nearestPoints[1] != forwardPoint &&
  1470. nearestPoints[0] != rearPoint && nearestPoints[1] != rearPoint)
  1471. {
  1472. if (Vector3.Distance(ortho, TractorController.Instance.trailer[0].transform.position) > lastDeltaa)
  1473. {
  1474. ResetCurves(GetNearestHolder());
  1475. }
  1476. }else if (nearestPoints[0] == forwardPoint || nearestPoints[1] == forwardPoint)
  1477. {
  1478. Transform leftPoint = holders[0].transform.GetChild(holders[0].transform.childCount - 1);
  1479. Transform rightPoint = holders[1].transform.GetChild(holders[1].transform.childCount - 1);
  1480. float leftDistance = Vector3.Distance(leftPoint.position, TractorController.Instance.trailer[0].transform.position);
  1481. float rightDistance = Vector3.Distance(rightPoint.position, TractorController.Instance.trailer[0].transform.position);
  1482. float forwardDistance = Vector3.Distance(forwardPoint.position, TractorController.Instance.trailer[0].transform.position);
  1483.  
  1484. if(forwardDistance < lastDeltaa * 2)
  1485. {
  1486. return;
  1487. }
  1488.  
  1489. if (leftDistance < forwardDistance)
  1490. {
  1491. ResetCurves(leftPoint.parent.gameObject);
  1492. }
  1493. else if (rightDistance < forwardDistance)
  1494. {
  1495. ResetCurves(rightPoint.parent.gameObject);
  1496. }
  1497. }
  1498. else if (nearestPoints[0] == rearPoint || nearestPoints[1] == rearPoint)
  1499. {
  1500. Transform leftPoint = holders[0].transform.GetChild(0);
  1501. Transform rightPoint = holders[1].transform.GetChild(0);
  1502. float leftDistance = Vector3.Distance(leftPoint.position, TractorController.Instance.trailer[0].transform.position);
  1503. float rightDistance = Vector3.Distance(rightPoint.position, TractorController.Instance.trailer[0].transform.position);
  1504. float rearDistance = Vector3.Distance(rearPoint.position, TractorController.Instance.trailer[0].transform.position);
  1505.  
  1506. if (rearDistance < lastDeltaa * 2)
  1507. {
  1508. return;
  1509. }
  1510.  
  1511. if (leftDistance < rearDistance && leftPoint != null && leftPoint.parent != null && leftPoint.parent.gameObject != null)
  1512. {
  1513. ResetCurves(leftPoint.parent.gameObject);
  1514. }
  1515. else if (rightDistance < rearDistance && rightPoint != null && rightPoint.parent != null && rightPoint.parent.gameObject != null)
  1516. {
  1517. ResetCurves(rightPoint.parent.gameObject);
  1518. }
  1519. }
  1520. }
  1521.  
  1522. void Update()
  1523. {
  1524.  
  1525. if (tracking && aButton.interactable == false && points.Count > 0)
  1526. {
  1527. if (Vector3.Distance(points[0].transform.position, TractorController.Instance.trailer[0].transform.position) / 6.27f >
  1528. 30)
  1529. {
  1530. aButton.interactable = true;
  1531. }
  1532. }
  1533.  
  1534. if (trakedObject == null)
  1535. {
  1536. if (findFinish)
  1537. {
  1538. Transform gm = GetClosestPoint(allPoints);
  1539. if (gm != null)
  1540. {
  1541. trakedObject = gm.gameObject;
  1542. }
  1543. }
  1544. return;
  1545. }
  1546.  
  1547. if (tracking == false && holders.Count > 0)
  1548. {
  1549. Transform gm = GetClosestPoint(allPoints);
  1550. if (gm != null)
  1551. {
  1552. trakedObject = gm.gameObject;
  1553. }
  1554. }
  1555. CheckDistance();
  1556. List<Transform> nearestPoints = GetNearestPoints();
  1557.  
  1558. Vector3 ontho = Ortho(nearestPoints[0].position, nearestPoints[1].position, TractorController.Instance.trailer[0].transform.position + TractorController.Instance.trailer[0].transform.forward * 10);
  1559. Vector3 ontho2 = Ortho(nearestPoints[0].position, nearestPoints[1].position, TractorController.Instance.trailer[0].transform.position + TractorController.Instance.trailer[0].transform.forward * 50);
  1560.  
  1561. Vector3 ortho = Ortho(nearestPoints[0].position, nearestPoints[1].position, TractorController.Instance.trailer[0].transform.position);
  1562.  
  1563. try
  1564. {
  1565. CheckForRebuildLines(nearestPoints, ortho);
  1566. }
  1567. catch { }
  1568.  
  1569. if (centerCounter != null)
  1570. {
  1571. centerCounter.transform.position = new Vector3(ontho2.x, centerCounter.transform.position.y, ontho2.z);
  1572. }
  1573. //centerCounter.transform.localPosition = new Vector3(trakedObject.transform.localPosition.x, centerCounter.transform.localPosition.y, centerCounter.transform.localPosition.z);
  1574. for (int i = 0; i < counters.Count; i++)
  1575. {
  1576. if (i < 3)
  1577. {
  1578. if(centerCounter != null)
  1579. counters[i].transform.localPosition = centerCounter.transform.localPosition + (Vector3.forward * (-deltaLast * (i+1)));
  1580. }
  1581. else
  1582. {
  1583. if (centerCounter != null)
  1584. counters[i].transform.localPosition = centerCounter.transform.localPosition + (Vector3.forward * (deltaLast * (i - 3 + 1)));
  1585.  
  1586. }
  1587. }
  1588.  
  1589. Vector3 targetPostition = new Vector3(ontho.x,
  1590. TractorController.Instance.Arrow.transform.transform.position.y,
  1591. ontho.z);
  1592.  
  1593. TractorController.Instance.Arrow.transform.LookAt(targetPostition);
  1594. bool forward = true;
  1595. if ((TractorController.Instance.Arrow.transform.localEulerAngles.y < 100 && TractorController.Instance.Arrow.transform.localEulerAngles.y > -100) ||
  1596. (TractorController.Instance.Arrow.transform.localEulerAngles.y > 240 || TractorController.Instance.Arrow.transform.localEulerAngles.y < -240))
  1597. {
  1598. IndicatorPanel.Instance.SetArrowRotation(new Vector3(0, 0, -TractorController.Instance.Arrow.transform.localEulerAngles.y));
  1599. //arrow.localEulerAngles = new Vector3(0, 0, -TractorController.Instance.Arrow.transform.localEulerAngles.y);
  1600. }
  1601. else
  1602. {
  1603. forward = false;
  1604. IndicatorPanel.Instance.SetArrowRotation(new Vector3(0, 0, 180 - TractorController.Instance.Arrow.transform.localEulerAngles.y));
  1605. //arrow.localEulerAngles = ;
  1606. }
  1607.  
  1608. IndicatorPanel.Instance.SetIndicators(TractorController.Instance.Arrow.transform.localEulerAngles.y, forward);
  1609.  
  1610. }
  1611.  
  1612. private void CheckDistance()
  1613. {
  1614. List<Transform> nearestPoints = GetNearestPoints();
  1615. Vector3 onthoo = Ortho(nearestPoints[0].position, nearestPoints[1].position, tractor.position);
  1616. if (trakedObject != null)
  1617. {
  1618. //Vector3 ontho = Ortho(trakedObject.transform.position - trakedObject.transform.forward * 10000, trakedObject.transform.position + trakedObject.transform.forward * 10000, TractorController.Instance.transform.position + TractorController.Instance.transform.forward * 10);
  1619. float dist = Vector3.Distance(onthoo, tractor.position) / 6.278354f;
  1620. //dist -= dist / 5;
  1621.  
  1622. //double metrics = Math.Round(((dist) * 2), 2);
  1623. double metrics = Math.Round(dist,2);
  1624. if(dist > (float)MetricsWindow.Instance.metrics + ((float)(MetricsWindow.Instance.sm / 100)))
  1625. {
  1626. trakedObject = GetClosestPointByDistance(allPoints).gameObject;
  1627. nearestPoints = GetNearestPoints();
  1628. onthoo = Ortho(nearestPoints[0].position, nearestPoints[1].position, tractor.position);
  1629. dist = Vector3.Distance(onthoo, tractor.position) / 6.278354f;
  1630.  
  1631. if (dist > (float)MetricsWindow.Instance.metrics + ((float)(MetricsWindow.Instance.sm / 100)))
  1632. {
  1633. trakedObject = GetClosestPointByDistance(allPoints).gameObject;
  1634. dist = Vector3.Distance(trakedObject.transform.position, tractor.position) / 6.278354f;
  1635. }
  1636.  
  1637. if (dist > (float)MetricsWindow.Instance.metrics + ((float)(MetricsWindow.Instance.sm / 100)))
  1638. {
  1639. dist = ((float)MetricsWindow.Instance.metrics + ((float)(MetricsWindow.Instance.sm / 100)));
  1640. }
  1641.  
  1642. metrics = Math.Round(dist, 2);
  1643. }
  1644. metricsText.text = metrics.ToString();
  1645. IndicatorPanel.Instance.SetDistance(metrics);
  1646. }
  1647. }
  1648.  
  1649.  
  1650.  
  1651.  
  1652.  
  1653.  
  1654.  
  1655.  
  1656.  
  1657. //Beizer
  1658. private int curveCount = 0;
  1659. private int layerOrder = 0;
  1660. private int SEGMENT_COUNT = 20;
  1661. public LineRenderer lineRenderer;
  1662.  
  1663. void DrawCurve()
  1664. {
  1665. for (int j = 0; j < curveCount - 1; j++)
  1666. {
  1667. for (int i = 1; i <= SEGMENT_COUNT; i++)
  1668. {
  1669. float t = i / (float)SEGMENT_COUNT;
  1670. int nodeIndex = j * 3;
  1671. Vector3 pixel = CalculateCubicBezierPoint(t, points[nodeIndex].transform.position, points[nodeIndex + 1].transform.position, points[nodeIndex + 2].transform.position, points[nodeIndex + 3].transform.position);
  1672. lineRenderer.positionCount = (((j * SEGMENT_COUNT) + i));
  1673. lineRenderer.SetPosition((j * SEGMENT_COUNT) + (i - 1), pixel);
  1674. }
  1675. }
  1676.  
  1677. lineRenderer.positionCount = lineRenderer.positionCount + 2;
  1678. lineRenderer.SetPosition(lineRenderer.positionCount - 2, points[points.Count - 2].transform.position);
  1679. lineRenderer.SetPosition(lineRenderer.positionCount - 1, points[points.Count - 1].transform.position);
  1680. }
  1681. Vector3 CalculateCubicBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
  1682. {
  1683. float u = 1 - t;
  1684. float tt = t * t;
  1685. float uu = u * u;
  1686. float uuu = uu * u;
  1687. float ttt = tt * t;
  1688. Vector3 p = uuu * p0;
  1689. p += 3 * uu * t * p1;
  1690. p += 3 * u * tt * p2;
  1691. p += ttt * p3;
  1692. return p;
  1693. }
  1694.  
  1695.  
  1696. List<Vector3> GetDrawingPoints()
  1697. {
  1698. List<Vector3> drawingPoints = new List<Vector3>();
  1699. for (int i = 0; i < points.Count - 3; i += 3)
  1700. {
  1701. Vector3 p0 = points[i].transform.position;
  1702. Vector3 p1 = points[i + 1].transform.position;
  1703. Vector3 p2 = points[i + 2].transform.position;
  1704. Vector3 p3 = points[i + 3].transform.position;
  1705.  
  1706. if (i == 0) //Only do this for the first endpoint.
  1707. //When i != 0, this coincides with the end
  1708. //point of the previous segment
  1709. {
  1710. drawingPoints.Add(CalculateCubicBezierPoint(0, p0, p1, p2, p3));
  1711. }
  1712.  
  1713. for (int j = 1; j <= SEGMENT_COUNT; j++)
  1714. {
  1715. float t = j / (float)SEGMENT_COUNT;
  1716. drawingPoints.Add(CalculateCubicBezierPoint(t, p0, p1, p2, p3));
  1717. }
  1718. }
  1719.  
  1720. return drawingPoints;
  1721. }
  1722.  
  1723. }
  1724.  
  1725. [System.Serializable]
  1726. public class CRSpline
  1727. {
  1728. public Vector3[] pts;
  1729.  
  1730. public CRSpline(params Vector3[] pts)
  1731. {
  1732. this.pts = new Vector3[pts.Length];
  1733. System.Array.Copy(pts, this.pts, pts.Length);
  1734. }
  1735.  
  1736.  
  1737. public Vector3 Interp(float t)
  1738. {
  1739. int numSections = pts.Length - 3;
  1740. int currPt = Mathf.Min(Mathf.FloorToInt(t * (float)numSections), numSections - 1);
  1741. float u = t * (float)numSections - (float)currPt;
  1742.  
  1743. Vector3 a = pts[currPt];
  1744. Vector3 b = pts[currPt + 1];
  1745. Vector3 c = pts[currPt + 2];
  1746. Vector3 d = pts[currPt + 3];
  1747.  
  1748. return .5f * (
  1749. (-a + 3f * b - 3f * c + d) * (u * u * u)
  1750. + (2f * a - 5f * b + 4f * c - d) * (u * u)
  1751. + (-a + c) * u
  1752. + 2f * b
  1753. );
  1754. }
  1755.  
  1756.  
  1757. public Vector3 Velocity(float t)
  1758. {
  1759. int numSections = pts.Length - 3;
  1760. int currPt = Mathf.Min(Mathf.FloorToInt(t * (float)numSections), numSections - 1);
  1761. float u = t * (float)numSections - (float)currPt;
  1762.  
  1763. Vector3 a = pts[currPt];
  1764. Vector3 b = pts[currPt + 1];
  1765. Vector3 c = pts[currPt + 2];
  1766. Vector3 d = pts[currPt + 3];
  1767.  
  1768. return 1.5f * (-a + 3f * b - 3f * c + d) * (u * u)
  1769. + (2f * a - 5f * b + 4f * c - d) * u
  1770. + .5f * c - .5f * a;
  1771. }
  1772.  
  1773.  
  1774. public void GizmoDraw(float t)
  1775. {
  1776. Gizmos.color = Color.white;
  1777. Vector3 prevPt = Interp(0);
  1778.  
  1779. for (int i = 1; i <= 20; i++)
  1780. {
  1781. float pm = (float)i / 20f;
  1782. Vector3 currPt = Interp(pm);
  1783.  
  1784. prevPt = currPt;
  1785. }
  1786.  
  1787. Gizmos.color = Color.blue;
  1788. Vector3 pos = Interp(t);
  1789.  
  1790. }
  1791. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement