Guest User

Untitled

a guest
Oct 22nd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1.  
  2. using UnityEngine;
  3. using System.Collections;
  4.  
  5. public class TouchSpin: MonoBehaviour {
  6.  
  7. public bool touched;
  8.  
  9. public float touchBegin;
  10. public float touchEnd;
  11. public float touchDir;
  12.  
  13. public float clickBegin;
  14. public float clickEnd;
  15. public float clickDir;
  16.  
  17. public float spin;
  18.  
  19. // Use this for initialization
  20. void Start ()
  21. {
  22. touched = false;
  23.  
  24. }
  25.  
  26. // Update is called once per frame
  27. void Update ()
  28. {
  29. transform.Rotate(0, 0, spin);
  30.  
  31. if(spin > .1f && !touched)
  32. {
  33. spin-=.05f;
  34. }
  35. else if(spin < 0 && !touched)
  36. {
  37. spin+=.05f;
  38. }
  39. else if(!touched)
  40. {
  41. spin = 0;
  42. }
  43.  
  44.  
  45. if(Input.GetButtonDown("Fire1"))
  46. {
  47. clickBegin = Input.mousePosition.x;
  48. touched = true;
  49. }
  50.  
  51. if(Input.GetButton ("Fire1") && touched)
  52. {
  53. clickEnd = Input.mousePosition.x;
  54. clickDir = clickEnd - clickBegin;
  55. clickBegin = clickEnd;
  56. }
  57.  
  58. if(Input.GetButtonUp("Fire1"))
  59. touched = false;
  60.  
  61. if(clickDir > 0)
  62. {
  63. //rigidbody.AddTorque(new Vector3(0,0, 1) * clickDir);
  64. spin = -clickDir/10;
  65. }
  66. else if(clickDir < 0)
  67. {
  68. //rigidbody.AddTorque(new Vector3(0,0, 1) * clickDir);
  69. spin = -clickDir/10;
  70. }
  71.  
  72. clickDir = 0;
  73.  
  74. foreach (Touch touch in Input.touches)
  75. {
  76. if(touch.phase == TouchPhase.Began)
  77. {
  78. touchBegin = touch.position.x;
  79. touched = true;
  80. }
  81.  
  82. if(touch.phase == TouchPhase.Moved)
  83. {
  84. touchEnd = touch.position.x;
  85. touchDir = touchEnd - touchBegin;
  86. touchBegin = touchEnd;
  87.  
  88. if(touchDir > 0)
  89. {
  90. //gameObject.rigidbody.AddTorque(new Vector3(1,0,0) * touchDir);
  91. spin = touchDir/10;
  92. }
  93. else if(touchDir < 0)
  94. {
  95. //gameObject.rigidbody.AddTorque(new Vector3(1,00) * touchDir);
  96. spin = touchDir/10;
  97. }
  98. }
  99. }
  100. }
  101. }
Add Comment
Please, Sign In to add comment