Advertisement
AndrewRosyaev

Player 0.77

Jan 31st, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.08 KB | None | 0 0
  1.  
  2. public class Player : MonoBehaviour
  3. {
  4. public LayerMask rayMask;
  5. public float Speed;
  6. public enum Direction {Left, Right, Forward, Back, Stay}
  7. public Direction MyDirection;
  8. public Vector3 nextDest;
  9. public Vector3 currentPos;
  10. public Vector3 lastPos;
  11. public float RotationTimer;
  12. public Animator anim;
  13. public bool disableRunAnimState;
  14. public bool StopRotation;
  15. public float rotationDelay;
  16. public Transform visibleTrEnemy;
  17. //
  18. public static float rotatePlayerSpeed = 5; //только визуально
  19. public Text rotSpeedText;
  20. //
  21.  
  22. float steptime;
  23. //inactive
  24. public bool freeze;
  25. //[System.NonSerialized]
  26. public float Teleport;
  27. public Transform Cam;
  28. float movementtimer;
  29. bool changedir;
  30.  
  31. //Для квестов
  32. [System.NonSerialized]
  33. public float ForwardSteps;
  34.  
  35. public bool Digger;
  36.  
  37.  
  38. void Start ()
  39. {
  40. nextDest = transform.position;
  41. currentPos = transform.position;
  42. }
  43.  
  44.  
  45. public void MoveForward()
  46. {
  47. if (CheckNextMove (transform.forward) == true) {
  48. if (MyDirection == Direction.Back) {
  49. rotationDelay = 0.3f;
  50. changedir = true;
  51. }
  52.  
  53. MyDirection = Direction.Forward;
  54. disableRunAnimState = false;
  55. anim.SetBool ("Run", true);
  56. visibleTrEnemy.GetComponent<Animator> ().SetBool ("Run", true);
  57. } else
  58. {
  59. if (RotationTimer < 0.1f) {
  60. if (MyDirection == Direction.Right || MyDirection == Direction.Left) {
  61. if (CheckCurrentMove (transform.forward) == true) {
  62. MyDirection = Direction.Forward;
  63. nextDest = currentPos + new Vector3 (0, 0, 1);
  64. if (Settings.RotatePlayer==false) {
  65. visibleTrEnemy.rotation = Quaternion.Euler (0, 0, 0);
  66. }
  67. RotationTimer = 0;
  68. disableRunAnimState = false;
  69. anim.SetBool ("Run", true);
  70. visibleTrEnemy.GetComponent<Animator> ().SetBool ("Run", true);
  71. }
  72. }
  73. }
  74. }
  75. }
  76. public void MoveBack()
  77. {
  78. if (CheckNextMove (-transform.forward) == true)
  79. {
  80. if (MyDirection == Direction.Forward)
  81. {
  82. rotationDelay = 0.3f;
  83. changedir = true;
  84. }
  85.  
  86.  
  87. MyDirection = Direction.Back;
  88. disableRunAnimState = false;
  89. anim.SetBool ("Run", true);
  90. visibleTrEnemy.GetComponent<Animator> ().SetBool ("Run", true);
  91. }
  92. else
  93. {
  94. if (RotationTimer < 0.1f) {
  95. if (MyDirection == Direction.Right || MyDirection == Direction.Left) {
  96. if (CheckCurrentMove (-transform.forward) == true) {
  97. MyDirection = Direction.Back;
  98. nextDest = currentPos + new Vector3 (0, 0, -1);
  99. if (Settings.RotatePlayer==false) {
  100. visibleTrEnemy.rotation = Quaternion.Euler (0, 180, 0);
  101. }
  102. RotationTimer = 0;
  103. disableRunAnimState = false;
  104. anim.SetBool ("Run", true);
  105. visibleTrEnemy.GetComponent<Animator> ().SetBool ("Run", true);
  106. }
  107. }
  108. }
  109. }
  110. }
  111. public void MoveRight()
  112. {
  113. if (CheckNextMove (transform.right) == true) {
  114. if (MyDirection == Direction.Left) {
  115. rotationDelay = 0.3f;
  116. changedir = true;
  117. }
  118. MyDirection = Direction.Right;
  119. disableRunAnimState = false;
  120. anim.SetBool ("Run", true);
  121. visibleTrEnemy.GetComponent<Animator> ().SetBool ("Run", true);
  122. } else
  123. {
  124. if (RotationTimer < 0.1f)
  125. {
  126. if (MyDirection == Direction.Forward || MyDirection == Direction.Back) {
  127. if (CheckCurrentMove (transform.right) == true) {
  128. MyDirection = Direction.Right;
  129. nextDest = currentPos + new Vector3 (1, 0, 0);
  130. if (Settings.RotatePlayer==false) {
  131. visibleTrEnemy.rotation = Quaternion.Euler (0, 90, 0);
  132. }
  133. RotationTimer = 0;
  134. disableRunAnimState = false;
  135. anim.SetBool ("Run", true);
  136. visibleTrEnemy.GetComponent<Animator> ().SetBool ("Run", true);
  137. }
  138. }
  139. }
  140. }
  141. }
  142. public void MoveLeft()
  143. {
  144. if (CheckNextMove (-transform.right) == true)
  145. {
  146. if (MyDirection == Direction.Right)
  147. {
  148. rotationDelay = 0.3f;
  149. changedir = true;
  150. }
  151. MyDirection = Direction.Left;
  152. disableRunAnimState = false;
  153. anim.SetBool ("Run", true);
  154. visibleTrEnemy.GetComponent<Animator> ().SetBool ("Run", true);
  155. }else
  156. {
  157. if (RotationTimer < 0.1f)
  158. {
  159. if (MyDirection == Direction.Forward || MyDirection == Direction.Back) {
  160. if (CheckCurrentMove (-transform.right) == true) {
  161. MyDirection = Direction.Left;
  162. nextDest = currentPos + new Vector3 (-1, 0, 0);
  163. if (Settings.RotatePlayer==false) {
  164. visibleTrEnemy.rotation = Quaternion.Euler (0, -90, 0);
  165. }
  166. RotationTimer = 0;
  167. disableRunAnimState = false;
  168. anim.SetBool ("Run", true);
  169. visibleTrEnemy.GetComponent<Animator> ().SetBool ("Run", true);
  170. }
  171. }
  172. }
  173. }
  174. }
  175.  
  176. public void SwipeDirection(int dirNumber) //1-forward, 2-back, 3-right, 4-left
  177. {
  178. if (RotationTimer < 1.5f)
  179. {
  180. if (MyDirection == Direction.Forward || MyDirection == Direction.Back)
  181. {
  182. if (dirNumber==3&&CheckCurrentMove (transform.right) == true)
  183. {
  184. MyDirection = Direction.Right;
  185. nextDest = currentPos + new Vector3 (1, 0, 0);
  186. if (Settings.RotatePlayer==false) {
  187. visibleTrEnemy.rotation = Quaternion.Euler (0, 90, 0);
  188. }
  189. RotationTimer = 0;
  190. // anim.SetBool ("Run", true);
  191. }
  192. if (dirNumber==4&&CheckCurrentMove (-transform.right) == true)
  193. {
  194. MyDirection = Direction.Left;
  195. nextDest = currentPos + new Vector3 (-1, 0, 0);
  196. if (Settings.RotatePlayer==false) {
  197. visibleTrEnemy.rotation = Quaternion.Euler (0, -90, 0);
  198. }
  199. RotationTimer = 0;
  200. // anim.SetBool ("Run", true);
  201. }
  202. }
  203. if (MyDirection == Direction.Right || MyDirection == Direction.Left)
  204. {
  205. if (dirNumber==1&&CheckCurrentMove (transform.forward) == true)
  206. {
  207. MyDirection = Direction.Forward;
  208. nextDest = currentPos + new Vector3 (0, 0, 1);
  209. if (Settings.RotatePlayer==false) {
  210. visibleTrEnemy.rotation = Quaternion.Euler (0, 0, 0);
  211. }
  212. RotationTimer = 0;
  213. // anim.SetBool ("Run", true);
  214. }
  215. if (dirNumber==2&&CheckCurrentMove (-transform.forward) == true)
  216. {
  217. MyDirection = Direction.Back;
  218. nextDest = currentPos + new Vector3 (0, 0, -1);
  219. if (Settings.RotatePlayer==false) {
  220. visibleTrEnemy.rotation = Quaternion.Euler (0, 180, 0);
  221. }
  222. RotationTimer = 0;
  223. // anim.SetBool ("Run", true);
  224. }
  225. }
  226. }
  227. }
  228.  
  229.  
  230.  
  231.  
  232.  
  233. void Update ()
  234. {
  235. if (rotationDelay > 0)
  236. {
  237. rotationDelay -= Time.deltaTime;
  238. }
  239.  
  240. RotationTimer += Time.deltaTime;
  241. //Forward
  242. if (Input.GetKeyDown (KeyCode.W)||Input.GetKeyDown(KeyCode.UpArrow))
  243. {
  244. MoveForward ();
  245. }
  246. //Back
  247. if (Input.GetKeyDown (KeyCode.S)||Input.GetKeyDown(KeyCode.DownArrow))
  248. {
  249. MoveBack ();
  250. }
  251.  
  252. //Left
  253. if (Input.GetKeyDown (KeyCode.A)||Input.GetKeyDown(KeyCode.LeftArrow))
  254. {
  255. MoveLeft ();
  256. }
  257.  
  258. //Right
  259. if (Input.GetKeyDown (KeyCode.D)||Input.GetKeyDown(KeyCode.RightArrow))
  260. {
  261. MoveRight ();
  262. }
  263.  
  264. if(transform.position==nextDest)
  265. {
  266. if (MyDirection==Direction.Forward)
  267. {
  268. if (CheckNextMove (transform.forward) == true)
  269. {
  270. if (Settings.RotatePlayer==false) {
  271. visibleTrEnemy.rotation = Quaternion.Euler (0, 0, 0);
  272. }
  273. nextDest = currentPos + new Vector3 (0, 0, 1);
  274. RotationTimer = 0;
  275. }
  276. else
  277. {
  278. disableRunAnimState = true;
  279. }
  280. }
  281. if (MyDirection==Direction.Back)
  282. {
  283. if (CheckNextMove (-transform.forward) == true)
  284. {
  285. if (Settings.RotatePlayer==false) {
  286. visibleTrEnemy.rotation = Quaternion.Euler (0, 180, 0);
  287. }
  288. nextDest = currentPos + new Vector3 (0, 0, -1);
  289. RotationTimer = 0;
  290. }
  291. else
  292. {
  293. disableRunAnimState = true;
  294. }
  295. }
  296. if (MyDirection==Direction.Right)
  297. {
  298. if (CheckNextMove (transform.right) == true)
  299. {
  300. if (Settings.RotatePlayer==false) {
  301. visibleTrEnemy.rotation = Quaternion.Euler (0, 90, 0);
  302. }
  303. nextDest = currentPos + new Vector3 (1, 0, 0);
  304. RotationTimer = 0;
  305. }
  306. else
  307. {
  308. disableRunAnimState = true;
  309. }
  310. }
  311. if (MyDirection==Direction.Left)
  312. {
  313. if (CheckNextMove (-transform.right) == true)
  314. {
  315. if (Settings.RotatePlayer==false) {
  316. visibleTrEnemy.rotation = Quaternion.Euler (0, -90, 0);
  317. }
  318. nextDest = currentPos + new Vector3 (-1, 0, 0);
  319. RotationTimer = 0;
  320. }
  321. else
  322. {
  323. disableRunAnimState = true;
  324. }
  325. }
  326.  
  327. if (Teleport != 0)
  328. {
  329. Vector3 pos = transform.position;
  330. pos.x += Teleport;
  331. transform.position = pos;
  332. Vector3 pos2 = anim.transform.parent.position;
  333. pos2.x += Teleport;
  334. anim.transform.parent.position = pos2;
  335. Vector3 pos3 = Cam.position;
  336. pos3.x += Teleport;
  337. Cam.GetComponent<CameraFollow> ().curpos.x += Teleport;
  338. Cam.position = pos3;
  339.  
  340. Teleport = 0;
  341. nextDest = transform.position;
  342. }
  343. steptime = 0;
  344. lastPos = currentPos;
  345. currentPos = nextDest;
  346.  
  347. //Для квеста
  348. ForwardSteps = transform.position.z + 2.5f;
  349.  
  350.  
  351. if (disableRunAnimState)
  352. {
  353. anim.SetBool ("Run", false);
  354. visibleTrEnemy.GetComponent<Animator> ().SetBool ("Run", false);
  355. }
  356. }
  357.  
  358. transform.position = Vector3.MoveTowards (transform.position, nextDest, Speed * Time.deltaTime);
  359. visibleTrEnemy.position = Vector3.MoveTowards (visibleTrEnemy.position, transform.position, Time.deltaTime * Speed);
  360.  
  361.  
  362. if (Settings.RotatePlayer) {
  363. {
  364. Vector3 vector = visibleTrEnemy.position - currentPos;
  365. if (vector != Vector3.zero) {
  366. var targetRotation = Quaternion.LookRotation (vector, Vector3.up);
  367.  
  368. if (rotationDelay > 0)
  369. {
  370. visibleTrEnemy.rotation = Quaternion.Lerp (visibleTrEnemy.rotation, Quaternion.LookRotation (vector), 25 * Time.deltaTime);
  371. }
  372. else
  373. {
  374. visibleTrEnemy.rotation = Quaternion.Lerp (visibleTrEnemy.rotation, Quaternion.LookRotation (vector), Time.deltaTime * rotatePlayerSpeed);
  375. }
  376. }
  377. }
  378. }
  379. }
  380.  
  381.  
  382. public bool CheckNextMove(Vector3 destination)
  383. {
  384. RaycastHit hit;
  385. //if (Physics.Raycast (transform.position, destination, out hit, 1))
  386.  
  387. Vector3 RayStartPos = nextDest + destination + new Vector3 (0,1,0);
  388. Debug.DrawRay(RayStartPos, -Vector3.up*3);
  389. if (Physics.Raycast (RayStartPos, -Vector3.up, out hit, 3,rayMask))
  390. {
  391. if (hit.transform.CompareTag ("Obstacle")&&Digger==false)
  392. {
  393. return false;
  394. }
  395. else
  396. {
  397. return true;
  398. }
  399. }
  400. else
  401. {
  402. return false;
  403. }
  404. }
  405.  
  406. public bool CheckCurrentMove(Vector3 destination)
  407. {
  408. RaycastHit hit;
  409. //if (Physics.Raycast (transform.position, destination, out hit, 1))
  410.  
  411. Vector3 RayStartPos = currentPos + destination+ new Vector3 (0,1,0);
  412.  
  413. if (Physics.Raycast (RayStartPos, -Vector3.up, out hit, 3, rayMask)) {
  414. if (hit.transform.CompareTag ("Obstacle")&&Digger==false) {
  415. return false;
  416. } else {
  417. return true;
  418. }
  419. } else
  420. {
  421. return false;
  422. }
  423. }
  424.  
  425.  
  426. public void RotateSpeedControlViaSettings(float rotSpeed)
  427. {
  428. rotatePlayerSpeed = rotSpeed;
  429. rotSpeedText.text = rotatePlayerSpeed.ToString ("F0");
  430. }
  431.  
  432. public void OnDrawGizmos()
  433. {
  434. // Gizmos.DrawSphere (nextDest + transform.forward + new Vector3 (0,1,0),0.5f);
  435. }
  436. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement