followCamera.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class followCamera : MonoBehaviour
  5. {
  6. private List<Vector3> pos;
  7. Transform[] objList;
  8. List<Transform> objZList=new List<Transform>();
  9. public float _dicCount;
  10. private float nDicCount;
  11. // Start is called before the first frame update
  12. void Start()
  13. {
  14. // Invoke("checkMainPos", 2f);
  15. checkMainPos();
  16. }
  17. private float DICCOUNT
  18. {
  19. get
  20. {
  21. if(this.transform.localScale.x==0)
  22. {
  23. return 0;
  24. }
  25. else
  26. {
  27. return _dicCount / this.transform.localScale.x;
  28. }
  29. }
  30. }
  31. private void OnEnable()
  32. {
  33. if (GSXRManager.Instance)
  34. {
  35. this.transform.position = GSXRManager.Instance.head.position;
  36. this.transform.eulerAngles = new Vector3(0, GSXRManager.Instance.head.eulerAngles.y, 0);
  37. }
  38. }
  39. void checkMainPos()
  40. {
  41. if (objList==null)
  42. {
  43. this.nDicCount = DICCOUNT;
  44. pos = new List<Vector3>();
  45. objList = this.GetComponentsInChildren<Transform>(true);
  46. for (int i = 0; i < objList.Length; i++)
  47. {
  48. if(objList[i].parent==this.transform)
  49. {
  50. pos.Add(new Vector3(objList[i].transform.localPosition.x, objList[i].transform.localPosition.y, objList[i].transform.localPosition.z));
  51. objList[i].transform.localPosition = new Vector3(objList[i].localPosition.x, objList[i].localPosition.y, objList[i].localPosition.z + DICCOUNT);
  52. objZList.Add(objList[i]);
  53. }
  54. }
  55. }
  56. if (this.nDicCount != DICCOUNT)
  57. {
  58. for (int i = 0; i < objZList.Count; i++)
  59. {
  60. objZList[i].transform.localPosition = new Vector3(pos[i].x, pos[i].y, pos[i].z + DICCOUNT);
  61. }
  62. this.nDicCount = DICCOUNT;
  63. }
  64. }
  65. float delayTime = 0f;
  66. float time = 0;
  67. bool isMove;
  68. Vector3 v3;
  69. float MOVETIME = 0.5f;
  70. float DEALYTIME = 1f;
  71. public float DISTANCE = 45;
  72. LeanTweenType leanTween = LeanTweenType.easeOutSine;
  73. // Update is called once per frame
  74. float jiaoduA;
  75. float jiaoduB;
  76. void checkEul()
  77. {
  78. /*
  79. /* if (isMove)
  80. {
  81. time = time + Time.deltaTime;
  82. if (time > MOVETIME*2)
  83. {
  84. time = 0;
  85. isMove = false;
  86. }
  87. }*/
  88. jiaoduA = this.transform.eulerAngles.y;
  89. jiaoduB = OpenXRCamera.Instance.head.transform.eulerAngles.y;
  90. float ty = jiaoduA;
  91. float hty = jiaoduB;
  92. bool check = false;
  93. bool checkzf = false;
  94. float jiaodu;
  95. Vector3 ppv3 = this.transform.position;
  96. Vector3 hpv3 = OpenXRCamera.Instance.head.transform.position;
  97. float distancex = Mathf.Abs(ppv3.x - hpv3.x);
  98. float dis = DISTANCE / (distancex + 1);
  99. if (Mathf.Abs(ty - hty) < 180 && Mathf.Abs(ty - hty) > dis)
  100. {
  101. jiaodu = Mathf.Abs(ty - hty);
  102. check = true;
  103. if (ty > hty)
  104. {
  105. checkzf = true;
  106. }
  107. else
  108. {
  109. checkzf = false;
  110. }
  111. }
  112. else if (Mathf.Abs(ty - hty) > 180 && (360 - Mathf.Abs(ty - hty)) > dis)
  113. {
  114. jiaodu = (360 - Mathf.Abs(ty - hty));
  115. check = true;
  116. if (ty > hty)
  117. {
  118. checkzf = false;
  119. }
  120. else
  121. {
  122. checkzf = true;
  123. }
  124. }
  125. else
  126. {
  127. if (Mathf.Abs(ty - hty) < 180)
  128. {
  129. jiaodu = Mathf.Abs(ty - hty);
  130. }
  131. else
  132. {
  133. jiaodu = (360 - Mathf.Abs(ty - hty));
  134. }
  135. }
  136. // GetAngle(GSXRManager.Instance.head, this.transform);
  137. // this.transform.position = GSXRManager.Instance.head.transform.position;
  138. if (check && !isMove)
  139. {
  140. // delayTime = delayTime + Time.deltaTime;
  141. // if (delayTime > DEALYTIME)
  142. // {
  143. // MovePos();
  144. if (checkzf)
  145. {
  146. // Debug.Log("角度差值===》" + jiaodu);
  147. v3 = new Vector3(0, OpenXRCamera.Instance.head.transform.eulerAngles.y + dis, 0);
  148. }
  149. else
  150. {
  151. // Debug.Log("角度差值===》" + -jiaodu);
  152. v3 = new Vector3(0, OpenXRCamera.Instance.head.transform.eulerAngles.y - dis, 0);
  153. }
  154. // float a= Mathf.Lerp(this.transform.eulerAngles.y, v3.y, Time.time);
  155. this.transform.rotation = Quaternion.Lerp(this.transform.rotation,Quaternion.Euler(v3),0.1f);
  156. // this.transform.Rotate(0,a,0);
  157. // LTDescr lt = LeanTween.rotate(this.gameObject, v3, ((jiaodu - dis) / 150f));
  158. // Invoke("isEndRotate", 0.3f);
  159. // Debug.Log((this.transform.eulerAngles.y-v3.y));
  160. // this.transform.eulerAngles = v3;
  161. // this.transform.eulerAngles = v3;
  162. // MoveEul();
  163. // }
  164. // isMove = true;
  165. }
  166. else
  167. {
  168. delayTime = 0;
  169. }
  170. }
  171. void isEndRotate()
  172. {
  173. isMove = false;
  174. }
  175. bool ispMove;
  176. float pdelayTime = 0f;
  177. float ptime = 0;
  178. bool pisMove;
  179. Vector3 pv3;
  180. float PMOVETIME = 0.5f;
  181. float PDEALYTIME = 0.5f;
  182. float PDISTANCEX = 0.2f;
  183. public float PDISTANCEY = 0f;
  184. public float PDISTANCEYY = -0.3f;
  185. float PDISTANCEZ = 0.2f;
  186. float PDISTANCEZZ = 0.2f;
  187. LeanTweenType pleanTween = LeanTweenType.easeOutSine;
  188. float timeD = 0.1f;
  189. void checkPos()
  190. {
  191. Vector3 ppv3 = this.transform.position;
  192. Vector3 hpv3 = OpenXRCamera.Instance.head.transform.position;
  193. float distancey = ppv3.y - hpv3.y;
  194. float distancex = Mathf.Abs(ppv3.x - hpv3.x);
  195. float distancez = ppv3.z - hpv3.z;
  196. if (distancey > (PDISTANCEY+0.001f))
  197. {
  198. pv3 = new Vector3(ppv3.x, hpv3.y - PDISTANCEY, ppv3.z);
  199. // LeanTween.move(this.gameObject, pv3, PMOVETIME).setEase(pleanTween);
  200. // this.transform.position = pv3;
  201. this.transform.position = Vector3.Lerp(this.transform.position, pv3, timeD);
  202. // MoveEul();
  203. // MovePos();
  204. }
  205. else if(distancey < (PDISTANCEYY-0.001f))
  206. {
  207. // Debug.Log("4444444");
  208. pv3 = new Vector3(ppv3.x, (hpv3.y + PDISTANCEYY), ppv3.z);
  209. this.transform.position = Vector3.Lerp(this.transform.position, pv3, timeD);
  210. // MoveEul();
  211. }
  212. ppv3 = this.transform.position;
  213. hpv3 = OpenXRCamera.Instance.head.transform.position;
  214. distancey = ppv3.y - hpv3.y;
  215. distancex = Mathf.Abs(ppv3.x - hpv3.x);
  216. distancez = ppv3.z - hpv3.z;
  217. if (distancex > PDISTANCEX)
  218. {
  219. // Debug.Log("333333");
  220. if (ppv3.x > hpv3.x)
  221. pv3 = new Vector3(ppv3.x - (distancex - PDISTANCEX), ppv3.y , ppv3.z);
  222. else
  223. pv3 = new Vector3(ppv3.x + (distancex - PDISTANCEX), ppv3.y, ppv3.z);
  224. this.transform.position = Vector3.Lerp(this.transform.position, pv3, timeD);
  225. // LeanTween.move(this.gameObject, pv3, PMOVETIME).setEase(pleanTween);
  226. // this.transform.position = pv3;
  227. // MovePos();
  228. // MoveEul();
  229. }
  230. ppv3 = this.transform.position;
  231. hpv3 = OpenXRCamera.Instance.head.transform.position;
  232. distancey = ppv3.y - hpv3.y;
  233. distancex = Mathf.Abs(ppv3.x - hpv3.x);
  234. distancez = ppv3.z - hpv3.z;
  235. if (distancez > PDISTANCEZ)
  236. {
  237. // Debug.Log("111111111");
  238. pv3 = new Vector3(ppv3.x, ppv3.y, ppv3.z - (distancez - PDISTANCEZ));
  239. this.transform.position = Vector3.Lerp(this.transform.position, pv3, timeD);
  240. // LeanTween.move(this.gameObject, pv3, PMOVETIME).setEase(pleanTween);
  241. // this.transform.position = pv3;
  242. // MovePos();
  243. // MoveEul();
  244. }
  245. else if (hpv3.z > ppv3.z && Mathf.Abs(distancez) > PDISTANCEZZ)
  246. {
  247. // Debug.Log("22222222");
  248. pv3 = new Vector3(ppv3.x, ppv3.y, ppv3.z + (Mathf.Abs(distancez) - PDISTANCEZZ));
  249. this.transform.position = Vector3.Lerp(this.transform.position, pv3, timeD);
  250. // this.transform.position = pv3;
  251. // MoveEul();
  252. }
  253. }
  254. void Update()
  255. {
  256. checkEul();
  257. checkPos();
  258. checkMainPos();
  259. //pv3 = new Vector3(GSXRManager.Instance.head.transform.position.x, 0, GSXRManager.Instance.head.transform.position.z);
  260. // LeanTween.move(this.gameObject, pv3, PMOVETIME).setEase(pleanTween);
  261. // this.transform.position = pv3;
  262. }
  263. void MovePos()
  264. {
  265. // pdelayTime = 0;
  266. pv3 = new Vector3(OpenXRCamera.Instance.head.transform.position.x, 0, OpenXRCamera.Instance.head.transform.position.z);
  267. LeanTween.move(this.gameObject, pv3, PMOVETIME).setEase(pleanTween);
  268. // this.transform.position = pv3;
  269. // pisMove = true;
  270. }
  271. void MoveEul()
  272. {
  273. if (!isMove)
  274. {
  275. // MovePos();
  276. delayTime = 0;
  277. v3 = new Vector3(0, OpenXRCamera.Instance.head.transform.eulerAngles.y, 0);
  278. // LeanTween.rotateY(this.gameObject, v3.y, MOVETIME).setEase(leanTween);
  279. // isMove = true;
  280. }
  281. }
  282. }