Advertisement
Guest User

pekka koodi

a guest
Jun 19th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class Touch2 : MonoBehaviour
  6. {
  7. private Rigidbody2D rbPlayer;
  8. public GameObject Player;
  9.  
  10. Vector2 swipeSuuntaVektori;
  11. Vector2 startPos;
  12. Vector2 endPos;
  13.  
  14. public float touchDistance;
  15. public float maxSpeed = 100f;
  16. float distance = 0;
  17.  
  18. float distance2 = 0;
  19.  
  20. public Text m_Text;
  21. public Text m_Text2;
  22.  
  23. float kulmaRad = 0;
  24. int liike;
  25.  
  26. bool move = false;
  27. bool HasPlayerTouchedChara = false;
  28. bool DoesPlayerMove = false;
  29. bool boolstop = true;
  30.  
  31. void Start()
  32. {
  33. rbPlayer = GetComponent<Rigidbody2D>();
  34.  
  35. }
  36. void Update()
  37. {
  38. if (DoesPlayerMove == true)
  39. {
  40. move = true;
  41. }
  42.  
  43. //m_Text2.text = " " + distance;
  44.  
  45. if (Input.touchCount > 0)
  46. {
  47. Touch touch = Input.GetTouch(0);
  48. Vector2 clickedPosition = Camera.main.ScreenToWorldPoint(touch.position);
  49. distance = Vector2.Distance(clickedPosition, transform.position);
  50. //m_Text2.text = "" + distance;
  51.  
  52. //laskee kosketuksen kulmaa
  53. swipeSuuntaVektori = startPos - touch.position;
  54. kulmaRad = Mathf.Atan2(swipeSuuntaVektori.x, swipeSuuntaVektori.y);
  55. float kulma = kulmaRad * Mathf.Rad2Deg;
  56.  
  57. if (kulma < 0)
  58. {
  59. kulma = 360 + kulma;
  60. }
  61.  
  62. TouchIntoEvent(kulma);
  63.  
  64.  
  65. //m_Text.text = "Suunta: " + liike;
  66. //m_Text.text = "Kulma: " + kulma;
  67. //m_Text.text = "Mag: " + swipeSuuntaVektori.magnitude;
  68.  
  69.  
  70. switch (touch.phase)
  71. {
  72.  
  73. case TouchPhase.Began:
  74. touchplayer(touch);
  75. if (HasPlayerTouchedChara == false) {
  76. shoot();
  77. }
  78. if (HasPlayerTouchedChara == true){
  79. boolstop = true;
  80. }
  81.  
  82. break;
  83.  
  84. case TouchPhase.Moved:
  85. if (HasPlayerTouchedChara == true)
  86. {
  87. distance2 = Vector2.Distance(clickedPosition, transform.position);
  88. move = true;
  89. }
  90. break;
  91.  
  92. case TouchPhase.Stationary:
  93.  
  94. break;
  95.  
  96. case TouchPhase.Ended:
  97. if (HasPlayerTouchedChara == true)
  98. {
  99. distance2 = Vector2.Distance(clickedPosition, transform.position);
  100. }
  101. HasPlayerTouchedChara = false;
  102. break;
  103.  
  104. case TouchPhase.Canceled:
  105.  
  106. break;
  107.  
  108.  
  109. }
  110. m_Text.text = "" + HasPlayerTouchedChara;
  111. }
  112. }
  113.  
  114. void FixedUpdate()
  115. {
  116.  
  117. if (boolstop == true)
  118. {
  119. stop();
  120. }
  121.  
  122. if (move == true)
  123. {
  124. liikuta();
  125. }
  126.  
  127. }
  128.  
  129. void liikuta()
  130. {
  131.  
  132. if (swipeSuuntaVektori.magnitude >= 40.0f)
  133. {
  134.  
  135. if (liike == 4)
  136. {
  137. rbPlayer.velocity = Vector2.right * (distance2);
  138. }
  139. if (liike == 2)
  140. {
  141. rbPlayer.velocity = Vector2.left * (distance2);
  142. }
  143. }
  144. }
  145.  
  146. void shoot()
  147. {
  148.  
  149. }
  150.  
  151. void TouchIntoEvent(float suunta)
  152. {
  153.  
  154. if (suunta > 315 || suunta <= 45)
  155. {
  156. liike = 1; //alas
  157. }
  158.  
  159. if (suunta > 45 && suunta <= 135)
  160. {
  161. liike = 2; //vasen
  162. }
  163.  
  164. if (suunta > 135 && suunta <= 225)
  165. {
  166. liike = 3;//ylös
  167. }
  168.  
  169. if (suunta > 225 && suunta <= 315)
  170. {
  171. liike = 4; //oikea
  172. }
  173. }
  174.  
  175. void liikkuukopelaaja()
  176. {
  177. //tarkista liikkuuko pelaaja
  178. if (rbPlayer.velocity.magnitude >= 0)
  179. DoesPlayerMove = true;
  180. }
  181.  
  182. void stop()
  183. {
  184. rbPlayer.velocity = Vector2.zero;
  185. DoesPlayerMove = false;
  186. }
  187.  
  188. void touchplayer(Touch touch)
  189. {
  190.  
  191. //jos kosketus on hahmon kosketusalueen sisällä
  192. if (distance <= touchDistance)
  193. {
  194. HasPlayerTouchedChara = true;
  195. startPos = touch.position;
  196. }
  197. else
  198. {
  199. HasPlayerTouchedChara = false;
  200. }
  201. }
  202.  
  203.  
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement