CameraController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. [Serializable]
  6. //public struct CameraRect
  7. //{
  8. // public Rect Up;
  9. // public Rect Down;
  10. // public Rect Left;
  11. // public Rect Right;
  12. //}
  13. public class CameraController : MonoBehaviour
  14. {
  15. public float offsetX = 2, offsetY = 2, offsetZ = -17,totalOffsetYDown = -10;
  16. //public float splitOffsetZ = -20;
  17. public float lerpValue = 3.5f;
  18. //[HideInInspector]
  19. //public float offsetYDown;
  20. [HideInInspector]
  21. public Vector3 targetPos;
  22. //[HideInInspector]
  23. //public Vector3 player0TargetPos;
  24. //[HideInInspector]
  25. //public Vector3 player1TargetPos;
  26. [HideInInspector]
  27. public Vector3 cameraPos;
  28. public Camera mainCamera;
  29. //public Camera player0Camera;
  30. //public Camera player1Camera;
  31. //[HideInInspector]
  32. //public float maxView = 20;
  33. //[HideInInspector]
  34. //public float view;
  35. //public float splitDistance = 30;
  36. //public float mergeDistanceX = 16;
  37. //public float mergeDistanceY = 8;
  38. [HideInInspector]
  39. public float distanceX;
  40. [HideInInspector]
  41. public float distanceY;
  42. //[HideInInspector]
  43. //public float ratio;
  44. public float ratioY = 1;
  45. //public float lineMaxScale = 0.5f;
  46. //public GameObject lineInCamera;
  47. //public bool isSplit;
  48. //public bool isSplitY;
  49. //public float minDis;
  50. private int once;
  51. //[HideInInspector]
  52. //public Quaternion targetRotation;
  53. //[HideInInspector]
  54. //public Rect targetViewport0;
  55. //[HideInInspector]
  56. //public Rect targetViewport1;
  57. //public CameraRect cameraRect;
  58. private void Start()
  59. {
  60. //maxView = offsetZ - splitOffsetZ;
  61. //ratio = maxView / (splitDistance - mergeDistanceX);
  62. //offsetYDown = totalOffsetYDown;
  63. }
  64. private void Update()
  65. {
  66. //调参用
  67. //ratio = maxView / (splitDistance - minDistance);
  68. //
  69. PlayerController player0 = PlayersInput.instance[0];
  70. //PlayerController player1 = PlayersInput.instance[1];
  71. //if (player0!= null && player0!=player1)
  72. if (player0 != null)
  73. {
  74. Vector3 player0Pos = player0.transform.position;
  75. ////玩家距离接近且都不动时回蓝速度加快
  76. //if (once < 2 && distanceX <= minDis && distanceY <= minDis)
  77. //{
  78. // if (once == 0 && player0.state == CharacterState.Idle && player1.state == CharacterState.Idle)
  79. // {
  80. // once = 1;
  81. // player0.RapidReplyMp();
  82. // player1.RapidReplyMp();
  83. // }
  84. // else if (once == 1 && (player0.state != CharacterState.Idle || player1.state != CharacterState.Idle))
  85. // {
  86. // once = 2;
  87. // player0.NormalReplyMp();
  88. // player1.NormalReplyMp();
  89. // }
  90. //}
  91. //else if (once != 0 && distanceX <= minDis && distanceY <= minDis)
  92. //{
  93. // once = 0;
  94. // player0.NormalReplyMp();
  95. // player1.NormalReplyMp();
  96. //}
  97. ////根据玩家距离分屏/合屏
  98. //if (distanceX < mergeDistanceX)
  99. //{
  100. // if (isSplit)
  101. // {
  102. // view = 0;
  103. // CameraSplit(true);
  104. // }
  105. // else
  106. // {
  107. // if (distanceY > mergeDistanceY)
  108. // {
  109. // view = (distanceY - mergeDistanceY) * ratioY;
  110. // //isSplitY = true;
  111. // }
  112. // else
  113. // {
  114. // //if (isSplitY)
  115. // //{
  116. // // isSplitY = false;
  117. // //}
  118. // if(player0.isRevive || player1.isRevive)
  119. // {
  120. // offsetYDown = 0;
  121. // }
  122. // else
  123. // {
  124. // offsetYDown = Mathf.Clamp(totalOffsetYDown / (2 * mergeDistanceY) * distanceY,
  125. // totalOffsetYDown, 0);
  126. // }
  127. // }
  128. // }
  129. //}
  130. //else if (distanceX > splitDistance)
  131. //{
  132. // if (!isSplit)
  133. // {
  134. // if (player0Pos.x < player1Pos.x)
  135. // {
  136. // player0Camera.rect = new Rect(0, 0, 0.5f, 1);
  137. // player1Camera.rect = new Rect(0.5f, 0, 0.5f, 1);
  138. // }
  139. // else
  140. // {
  141. // player0Camera.rect = new Rect(0.5f, 0, 0.5f, 1);
  142. // player1Camera.rect = new Rect(0, 0, 0.5f, 1);
  143. // }
  144. // CameraSplit(false);
  145. // }
  146. //}
  147. //else
  148. //{
  149. // if (isSplit)
  150. // {
  151. // view = 0;
  152. // }
  153. // else
  154. // {
  155. // float viewX = Mathf.Clamp((distanceX - mergeDistanceX) * ratio, 0, maxView);
  156. // float viewY = (distanceY - mergeDistanceY) * ratioY;
  157. // if(viewX > viewY)
  158. // {
  159. // view = viewX;
  160. // //if (isSplitY)
  161. // //{
  162. // // isSplitY = false;
  163. // //}
  164. // }
  165. // else
  166. // {
  167. // view = viewY;
  168. // //isSplitY = true;
  169. // }
  170. // if (player0.isRevive || player1.isRevive)
  171. // {
  172. // offsetYDown = 0;
  173. // }
  174. // else
  175. // {
  176. // offsetYDown = Mathf.Clamp(totalOffsetYDown / (2 * mergeDistanceY) * distanceY,
  177. // totalOffsetYDown, 0);
  178. // }
  179. // }
  180. //}
  181. //if (isSplit)
  182. //{
  183. // lineInCamera.transform.localScale = new Vector3(
  184. // Mathf.Clamp((distanceX - mergeDistanceX) * 0.01f, 0,
  185. // lineMaxScale), 20, 1);
  186. //}
  187. }
  188. }
  189. private void FixedUpdate()
  190. {
  191. PlayerController player0 = PlayersInput.instance[0];
  192. //PlayerController player1 = PlayersInput.instance[1];
  193. if (player0 == null)
  194. {
  195. return;
  196. }
  197. else
  198. {
  199. if (player0.isRevive)
  200. {
  201. targetPos = new Vector3(140, offsetY, offsetZ);
  202. }
  203. else
  204. {
  205. targetPos = CameraTargetMove(player0);
  206. }
  207. if(Time.timeScale == 1)
  208. {
  209. mainCamera.transform.position =
  210. Vector3.Lerp(mainCamera.transform.position, targetPos, lerpValue * Time.deltaTime);
  211. }
  212. }
  213. //else if (player1 == player0)
  214. //{
  215. // if (player0.isRevive)
  216. // {
  217. // targetPos = new Vector3(140, offsetY, offsetZ);
  218. // }
  219. // else
  220. // {
  221. // targetPos = CameraTargetMove(player0);
  222. // }
  223. // mainCamera.transform.position =
  224. // Vector3.Lerp(mainCamera.transform.position, targetPos, lerpValue * Time.deltaTime);
  225. //}
  226. //else
  227. //{
  228. // player0TargetPos = CameraTargetMove(player0);
  229. // player1TargetPos = CameraTargetMove(player1);
  230. // if (player0.isRevive && player1.isRevive)
  231. // {
  232. // player0TargetPos = new Vector3(140, offsetY, offsetZ);
  233. // player1TargetPos = new Vector3(140, offsetY, offsetZ);
  234. // view = 0;
  235. // if (isSplit)
  236. // {
  237. // CameraSplit(true);
  238. // }
  239. // }
  240. // else if (player0.isRevive)
  241. // {
  242. // player0TargetPos = CameraTargetMove(player1);
  243. // view = 0;
  244. // if (isSplit)
  245. // {
  246. // CameraSplit(true);
  247. // }
  248. // }
  249. // else if (player1.isRevive)
  250. // {
  251. // player1TargetPos = CameraTargetMove(player0);
  252. // view = 0;
  253. // if (isSplit)
  254. // {
  255. // CameraSplit(true);
  256. // }
  257. // }
  258. // if (player0TargetPos.y > player1TargetPos.y)
  259. // {
  260. // player0Camera.transform.position = Vector3.Lerp(player0Camera.transform.position,
  261. // player0TargetPos + Vector3.up * offsetYDown, lerpValue * 2 * Time.deltaTime);
  262. // player1Camera.transform.position = Vector3.Lerp(player1Camera.transform.position,
  263. // player1TargetPos, lerpValue * 2 * Time.deltaTime);
  264. // }
  265. // else
  266. // {
  267. // player0Camera.transform.position = Vector3.Lerp(player0Camera.transform.position,
  268. // player0TargetPos, lerpValue * 2 * Time.deltaTime);
  269. // player1Camera.transform.position = Vector3.Lerp(player1Camera.transform.position,
  270. // player1TargetPos + Vector3.up * offsetYDown, lerpValue * 2 * Time.deltaTime);
  271. // }
  272. // mainCamera.transform.position = Vector3.Lerp(player0Camera.transform.position,
  273. // player1Camera.transform.position, 0.5f);
  274. //}
  275. }
  276. //public void CameraSplit(bool flag)
  277. //{
  278. // if (flag)
  279. // {
  280. // mainCamera.enabled = true;
  281. // player0Camera.enabled = false;
  282. // player1Camera.enabled = false;
  283. // lineInCamera.SetActive(false);
  284. // isSplit = false;
  285. // }
  286. // else
  287. // {
  288. // if(PlayersInput.instance[0].isRevive || PlayersInput.instance[1].isRevive)
  289. // {
  290. // return;
  291. // }
  292. // offsetYDown = 0;
  293. // mainCamera.enabled = false;
  294. // player0Camera.enabled = true;
  295. // player1Camera.enabled = true;
  296. // lineInCamera.SetActive(true);
  297. // isSplit = true;
  298. // }
  299. //}
  300. Vector3 CameraTargetMove(PlayerController player)
  301. {
  302. Vector3 skewPos;
  303. if (player.bodyTrans.localScale.x > 0)
  304. {
  305. skewPos = player.transform.position + new Vector3(-offsetX, offsetY, offsetZ);
  306. }
  307. else
  308. {
  309. skewPos = player.transform.position + new Vector3(offsetX, offsetY, offsetZ);
  310. }
  311. //if (isSplit)
  312. //{
  313. // if (player.bodyTrans.localScale.x > 0)
  314. // {
  315. // skewPos = player.transform.position + new Vector3(-offsetX / 2, offsetY, splitOffsetZ);
  316. // }
  317. // else
  318. // {
  319. // skewPos = player.transform.position + new Vector3(offsetX / 2, offsetY, splitOffsetZ);
  320. // }
  321. //}
  322. //else
  323. //{
  324. // skewPos = player.transform.position + new Vector3(0, offsetY, offsetZ - view);
  325. //}
  326. //skewPos = player.transform.position + new Vector3(0, offsetY, offsetZ);
  327. return skewPos;
  328. }
  329. }