SwipeMove.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using DG.Tweening;
  5. using UnityEngine.AI;
  6. using UnityEngine.EventSystems;
  7. using System;
  8. public class SwipeMove : MonoBehaviour {
  9. public float MaxSpeed = 10;
  10. public float DefoultSpeed = 10;
  11. public float SPEED = 10;
  12. public float SwipeMoveSide = 0.5f;
  13. public float SwipeMoveForward = 0.5f;
  14. public float SwipeMoveBack = 0.5f;
  15. public RunnersController RunnerController;
  16. private NavMeshAgent Agent;
  17. public void Start()
  18. {
  19. RunnerController = GetComponent<RunnersController>();
  20. Agent = GetComponent<NavMeshAgent>();
  21. }
  22. void UpdateDestination()
  23. {
  24. //Agent.SetDestination(Core.Instance.GetComponent<LevelTargets>().ListLevelTargets[Core.Instance.GetComponent<LevelTargets>().CurTarget].position);
  25. RunnerController.Destination();
  26. }
  27. public void SlideBackward()
  28. {
  29. timer = defaultTimerValue;
  30. DirSwipe = DirectionSwipe.Back;
  31. Agent.speed -= 0.5f;
  32. }
  33. public void SlideForward()
  34. {
  35. timer = defaultTimerValue;
  36. DirSwipe = DirectionSwipe.Forward;
  37. Agent.speed += 0.5f;
  38. }
  39. public void SlideLeft()
  40. {
  41. timer = defaultTimerValue;
  42. DirSwipe = DirectionSwipe.Left;
  43. }
  44. public void SlideRight()
  45. {
  46. timer = defaultTimerValue;
  47. DirSwipe = DirectionSwipe.Right;
  48. }
  49. public enum DirectionSwipe
  50. {
  51. Left,
  52. Right,
  53. Forward,
  54. Back,
  55. None
  56. }
  57. public void SlideNone()
  58. {
  59. DirSwipe = DirectionSwipe.None;
  60. }
  61. public DirectionSwipe DirSwipe = DirectionSwipe.None;
  62. private float defaultTimerValue = 0.3f;
  63. private float timer;
  64. private bool GettingControlPlayer;
  65. private void Update()
  66. {
  67. if(Core.Instance.IsGameMode != Core.StateModeGame.IsGameMode )
  68. {
  69. return;
  70. }
  71. if(Agent.speed <=0)
  72. {
  73. UIManager.Instance.GameOverPanel.gameObject.SetActive(true);
  74. UIManager.Instance.GameOverPanel.DOFade(1, 0.5f);
  75. return;
  76. }
  77. Agent.speed -= Time.deltaTime * 0.1f;
  78. if(UIManager.Instance.RacePanel.gameObject.activeSelf && Core.Instance.IsGameMode== Core.StateModeGame.IsGameMode && RunnerController.gameObject.activeSelf)
  79. {
  80. timer -= Time.deltaTime;
  81. //switch(DirSwipe)
  82. //{
  83. // case DirectionSwipe.Left:
  84. // if(timer>0)
  85. // {
  86. // this.transform.position = Vector3.Slerp(this.transform.position, SwipeMoveSide * -transform.right + this.transform.position, 3f * Time.fixedDeltaTime);
  87. // return;
  88. // }
  89. // //this.transform.Translate(SPEED * Vector3.right * Time.deltaTime, Space.Self);
  90. // break;
  91. // case DirectionSwipe.Right:
  92. // if (timer > 0)
  93. // {
  94. // this.transform.position = Vector3.Slerp(this.transform.position, SwipeMoveSide * transform.right + this.transform.position, 3f * Time.fixedDeltaTime);
  95. // return;
  96. // }
  97. // //this.transform.Translate(SPEED * Vector3.left * Time.deltaTime, Space.Self);
  98. // break;
  99. // case DirectionSwipe.Forward:
  100. // if (timer > 0)
  101. // {
  102. // this.transform.position = Vector3.Slerp(this.transform.position, SwipeMoveForward * transform.forward + this.transform.position, 3f * Time.fixedDeltaTime);
  103. // return;
  104. // }
  105. // //this.transform.Translate(SPEED * Vector3.forward * Time.deltaTime, Space.Self);
  106. // break;
  107. // case DirectionSwipe.Back:
  108. // if (timer > 0)
  109. // {
  110. // this.transform.position = Vector3.Slerp(this.transform.position, SwipeMoveBack * -transform.forward + this.transform.position, 3f * Time.fixedDeltaTime);
  111. // return;
  112. // }
  113. // //this.transform.Translate(SPEED * Vector3.back * Time.deltaTime, Space.Self);
  114. // break;
  115. //}
  116. //UpdateDestination();
  117. //DirSwipe = DirectionSwipe.None;
  118. }
  119. if (Core.Instance.IsGameMode != Core.StateModeGame.Kiss && Core.Instance.IsGameMode != Core.StateModeGame.StartLevel)
  120. {
  121. if (RunnerController.IsPlayer)
  122. {
  123. if (Input.GetMouseButtonDown(0))
  124. {
  125. this.OnMouseDown();
  126. this.GettingControlPlayer = true;
  127. }
  128. if (Input.GetMouseButtonUp(0))
  129. {
  130. this.GettingControlPlayer = false;
  131. }
  132. if (GettingControlPlayer)
  133. {
  134. this.OnMouseDrag();
  135. }
  136. }
  137. }
  138. //if (Input.touchCount > 0)
  139. //{
  140. // Touch touch = Input.GetTouch(0); // get first touch since touch count is greater than zero
  141. // if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Moved)
  142. // {
  143. // // get the touch position from the screen touch to world point
  144. // Vector3 touchedPos = Camera.main.ScreenToWorldPoint(new Vector3(touch.position.x, touch.position.y, 10));
  145. // // lerp and set the position of the current object to that of the touch, but smoothly over time.
  146. // transform.position = Vector3.Lerp(transform.position, touchedPos, Time.deltaTime);
  147. // }
  148. //}
  149. }
  150. private Vector3 screenPoint;
  151. private Vector3 offset;
  152. void OnMouseDown()
  153. {
  154. screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
  155. offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, screenPoint.y, screenPoint.z));
  156. }
  157. void OnMouseDrag()
  158. {
  159. Vector3 cursorPoint = new Vector3(Input.mousePosition.x, screenPoint.y, screenPoint.z);
  160. Vector3 cursorPosition = Camera.main.ScreenToWorldPoint(cursorPoint) + offset;
  161. transform.position = Vector3.Lerp(transform.position,cursorPosition, 2* Time.deltaTime);
  162. //GetComponent<Animator>().SetFloat("Blend", 0.0f + );
  163. }
  164. private enum DraggedDirection
  165. {
  166. Up,
  167. Down,
  168. Right,
  169. Left
  170. }
  171. private DraggedDirection GetDragDirection(Vector3 dragVector)
  172. {
  173. float positiveX = Mathf.Abs(dragVector.x);
  174. float positiveY = Mathf.Abs(dragVector.y);
  175. DraggedDirection draggedDir;
  176. draggedDir = (dragVector.x > 0) ? DraggedDirection.Right : DraggedDirection.Left;
  177. //if (positiveX > positiveY)
  178. //{
  179. //}
  180. //else
  181. //{
  182. // draggedDir = (dragVector.y > 0) ? DraggedDirection.Up : DraggedDirection.Down;
  183. //}
  184. Debug.Log(draggedDir);
  185. return draggedDir;
  186. }
  187. public void OnEndDrag(PointerEventData eventData)
  188. {
  189. Debug.Log("Press position + " + eventData.pressPosition);
  190. Debug.Log("End position + " + eventData.position);
  191. Vector3 dragVectorDirection = (eventData.position - eventData.pressPosition).normalized;
  192. Debug.Log("norm + " + dragVectorDirection);
  193. GetDragDirection(dragVectorDirection);
  194. }
  195. }