Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using GeekGame.Input;
  4.  
  5. public class CubeControl : MonoBehaviour {
  6.  
  7. public float speed=.1f;
  8.  
  9. // Use this for initialization
  10. void Start () {
  11.  
  12. }
  13.  
  14. // Update is called once per frame
  15. void Update () {
  16. transform.Translate(new Vector3(JoystickMove.instance.H,0f,JoystickMove.instance.V)*speed*Time.deltaTime);
  17.  
  18.  
  19. /*
  20. transform.LookAt(transform.position+new Vector3(JoystickRotate.instance.H,0f,JoystickRotate.instance.V));
  21. */
  22. transform.LookAt(transform.position + new Vector3(JoystickRotate.instance.H, 0f, JoystickRotate.instance.V));
  23.  
  24. if (JoystickFire.instance.Fire){
  25. Debug.Log("fire");
  26. }
  27. }
  28.  
  29. }
  30.  
  31. using UnityEngine;
  32. using System.Collections;
  33. using UnityEngine.EventSystems;
  34.  
  35. namespace GeekGame.Input{
  36. public class JoystickMove : MonoBehaviour ,IDragHandler,IEndDragHandler
  37. {
  38. // remember turning is with joystick rotate
  39. // calculate the h and the v, then give it to PlayerMovement
  40.  
  41. public static JoystickMove instance=null;
  42.  
  43. public float _speed=6f;
  44.  
  45. [Tooltip("the joystick radius ")]
  46. public float R=50f;
  47.  
  48. private float _r;
  49.  
  50. private Vector2 centerPos;
  51.  
  52. private float _h;
  53. private float _v;
  54.  
  55. public float H{
  56. get{return _h;}
  57. }
  58. public float V{
  59. get{return _v;}
  60. }
  61.  
  62. void Awake()
  63. {
  64.  
  65. if(instance!=null)
  66. {
  67. Destroy(this.gameObject);
  68. }
  69. else
  70. {
  71. instance=this;
  72. }
  73. }
  74.  
  75.  
  76. void Start(){
  77.  
  78. _r=1f*Screen.width/960f*R; //this to calculate the scale of screen
  79.  
  80. centerPos=GetComponent<RectTransform>().position;
  81.  
  82. }
  83.  
  84.  
  85. void SetHAndF(Vector2 pos){ //Horizontall and Vertical axes
  86.  
  87. Vector2 diff = pos-centerPos;
  88. float distance = diff.magnitude;
  89.  
  90. if(distance>_r)
  91. {
  92. pos = centerPos+diff / distance*_r;
  93. }
  94.  
  95. GetComponent<RectTransform>().position=pos;
  96.  
  97. Vector2 move = pos-centerPos;
  98.  
  99. _h = move.x;
  100. _v = move.y;
  101.  
  102. }
  103.  
  104. public void OnDrag(PointerEventData data)
  105. {
  106.  
  107. Vector2 newPos =new Vector2(data.position.x-30f,data.position.y-30f);
  108.  
  109. //clamp the sprite
  110.  
  111. SetHAndF(newPos);
  112.  
  113.  
  114.  
  115. }
  116.  
  117. public void OnEndDrag(PointerEventData data){
  118.  
  119. Debug.Log("End Drag"+centerPos);
  120. GetComponent<RectTransform>().position=centerPos;
  121. SetHAndF(centerPos);
  122.  
  123.  
  124. }
  125.  
  126. // handle joystick motion events
  127. case SDL_JOYAXISMOTION:
  128.  
  129.  
  130.  
  131. // check left-right movement
  132. if( event->jaxis.axis == 0 )
  133.  
  134. {
  135. // if joystick moving left
  136. if ( event->jaxis.value < -800 )
  137.  
  138. {
  139.  
  140. // move left
  141. eventStates[JOYSTICK_LEFT] = 1;
  142. }
  143.  
  144. else
  145.  
  146. {
  147.  
  148. eventStates[JOYSTICK_LEFT] = 0;
  149. }
  150.  
  151. // if joystick moving right
  152. if ( event->jaxis.value > 800 )
  153.  
  154. {
  155.  
  156. // move right
  157. eventStates[JOYSTICK_RIGHT] = 1;
  158. }
  159.  
  160. else
  161.  
  162. {
  163.  
  164. eventStates[JOYSTICK_RIGHT] = 0;
  165. }
  166. }
  167.  
  168. // check up-down movement
  169. if( event->jaxis.axis == 1 )
  170.  
  171. {
  172. // if joystick moving up
  173. if ( event->jaxis.value < -800 )
  174.  
  175. {
  176.  
  177. // move up
  178. eventStates[JOYSTICK_UP] = 1;
  179. }
  180.  
  181. else
  182.  
  183. {
  184.  
  185. eventStates[JOYSTICK_UP] = 0;
  186. }
  187.  
  188. // if joystick moving down
  189. if ( event->jaxis.value > 800 )
  190.  
  191. {
  192.  
  193. // move down
  194. eventStates[JOYSTICK_DOWN] = 1;
  195. }
  196.  
  197. else
  198.  
  199. {
  200.  
  201. eventStates[JOYSTICK_DOWN] = 0;
  202. }
  203. }
  204.  
  205. eventStates[JOYSTICK_SCALE] = event->jaxis.value;
  206.  
  207. break;
  208.  
  209. }
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement