Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.87 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class TaoistMovement : MonoBehaviour
  5. {
  6. //Animator anim
  7. bool left direction;
  8. bool right direction;
  9. bool front direction;
  10. bool back direction;
  11.  
  12. // Use this for initialization
  13. void Start()
  14. {
  15.  
  16. }
  17.  
  18.  
  19. void Update()
  20. {
  21. float MoveSpeed = 4.5f;
  22.  
  23. string charactersname = "taoistmoveobject";
  24. string charactername = "taoist";
  25.  
  26. //change the string to my parameter
  27. string charactersfrontwalk = "FrontWalk";
  28. string charactersbackwalk = "BackWalk";
  29. string charactersleftwalk = "LeftWalk";
  30. string charactersrightwalk = "RightWalk";
  31. string charactersfrontidle = "FrontIdle";
  32. string charactersbackidle = "BackIdle";
  33. string charactersrightidle = "RightIdle";
  34. string charactersleftidle = "LeftIdle";
  35.  
  36. GameObject characters = GameObject.Find(charactersname);
  37. GameObject theanim = GameObject.Find(charactername);
  38.  
  39.  
  40.  
  41. //something
  42. character.transform.localPosition = new Vector3(character.transform.localPosition.x, character.transform.localPosition.y + 0.05f * MoveSpeed, character.transform.localPosition.z);
  43. character.transform.localPosition = new Vector3(character.transform.localPosition.x - 0.05f * MoveSpeed, character.transform.localPosition.y, character.transform.localPosition.z);
  44.  
  45.  
  46. if (!Input.GetKey("up") && !Input.GetKey("down") && !Input.GetKey("left") && !Input.GetKey("right"))
  47. {
  48. if (FrontDirection == true)
  49. {
  50. theanim.GetComponent<Animator>().SetBool(charactersfrontwalk, false);
  51. theanim.GetComponent<Animator>().SetBool(charactersbackwalk, false);
  52. theanim.GetComponent<Animator>().SetBool(charactersleftwalk, false);
  53. theanim.GetComponent<Animator>().SetBool(charactersrightwalk, false);
  54. theanim.GetComponent<Animator>().SetBool(charactersfrontidle, true);
  55. theanim.GetComponent<Animator>().SetBool(charactersbackidle, false);
  56. theanim.GetComponent<Animator>().SetBool(charactersrightidle, false);
  57. theanim.GetComponent<Animator>().SetBool(charactersleftidle, false);
  58. }
  59. if (BackDirection == true)
  60. {
  61. theanim.GetComponent<Animator>().SetBool(charactersfrontwalk, false);
  62. theanim.GetComponent<Animator>().SetBool(charactersbackwalk, false);
  63. theanim.GetComponent<Animator>().SetBool(charactersleftwalk, false);
  64. theanim.GetComponent<Animator>().SetBool(charactersrightwalk, false);
  65. theanim.GetComponent<Animator>().SetBool(charactersfrontidle, false);
  66. theanim.GetComponent<Animator>().SetBool(charactersbackidle, true);
  67. theanim.GetComponent<Animator>().SetBool(charactersrightidle, false);
  68. theanim.GetComponent<Animator>().SetBool(charactersleftidle, false);
  69. }
  70. if (LeftDirection == true)
  71. {
  72. theanim.GetComponent<Animator>().SetBool(charactersfrontwalk, false);
  73. theanim.GetComponent<Animator>().SetBool(charactersbackwalk, false);
  74. theanim.GetComponent<Animator>().SetBool(charactersleftwalk, false);
  75. theanim.GetComponent<Animator>().SetBool(charactersrightwalk, false);
  76. theanim.GetComponent<Animator>().SetBool(charactersfrontidle, false);
  77. theanim.GetComponent<Animator>().SetBool(charactersbackidle, false);
  78. theanim.GetComponent<Animator>().SetBool(charactersrightidle, false);
  79. theanim.GetComponent<Animator>().SetBool(charactersleftidle, true);
  80. }
  81. if (RightDirection == true)
  82. {
  83. theanim.GetComponent<Animator>().SetBool(charactersfrontwalk, false);
  84. theanim.GetComponent<Animator>().SetBool(charactersbackwalk, false);
  85. theanim.GetComponent<Animator>().SetBool(charactersleftwalk, false);
  86. theanim.GetComponent<Animator>().SetBool(charactersrightwalk, false);
  87. theanim.GetComponent<Animator>().SetBool(charactersfrontidle, false);
  88. theanim.GetComponent<Animator>().SetBool(charactersbackidle, false);
  89. theanim.GetComponent<Animator>().SetBool(charactersrightidle, true);
  90. theanim.GetComponent<Animator>().SetBool(charactersleftidle, false);
  91. }
  92. }
  93.  
  94.  
  95.  
  96.  
  97.  
  98. if (Input.GetKey("up") || Input.GetKey("down") || Input.GetKey("left") || Input.GetKey("right"))
  99. {
  100. if (Input.GetKey("up") && !(Input.GetKey("down") || Input.GetKey("left") || Input.GetKey("right")))
  101. {
  102. theanim.GetComponent<Animator>().SetBool(charactersfrontwalk, false);
  103. theanim.GetComponent<Animator>().SetBool(charactersbackwalk, true);
  104. theanim.GetComponent<Animator>().SetBool(charactersleftwalk, false);
  105. theanim.GetComponent<Animator>().SetBool(charactersrightwalk, false);
  106. theanim.GetComponent<Animator>().SetBool(charactersfrontidle, false);
  107. theanim.GetComponent<Animator>().SetBool(charactersbackidle, false);
  108. theanim.GetComponent<Animator>().SetBool(charactersrightidle, false);
  109. theanim.GetComponent<Animator>().SetBool(charactersleftidle, false);
  110. LeftDirection = false;
  111. RightDirection = false;
  112. BackDirection = true;
  113. FrontDirection = false;
  114.  
  115. characters.transform.localPosition = new Vector3(characters.transform.localPosition.x, characters.transform.localPosition.y, characters.transform.localPosition.z + 0.05f * MoveSpeed);
  116. }
  117. if (Input.GetKey("down") && !(Input.GetKey("up") || Input.GetKey("left") || Input.GetKey("right")))
  118. {
  119. theanim.GetComponent<Animator>().SetBool(charactersfrontwalk, true);
  120. theanim.GetComponent<Animator>().SetBool(charactersbackwalk, false);
  121. theanim.GetComponent<Animator>().SetBool(charactersleftwalk, false);
  122. theanim.GetComponent<Animator>().SetBool(charactersrightwalk, false);
  123. theanim.GetComponent<Animator>().SetBool(charactersfrontidle, false);
  124. theanim.GetComponent<Animator>().SetBool(charactersbackidle, false);
  125. theanim.GetComponent<Animator>().SetBool(charactersrightidle, false);
  126. theanim.GetComponent<Animator>().SetBool(charactersleftidle, false);
  127. LeftDirection = false;
  128. RightDirection = false;
  129. BackDirection = false;
  130. FrontDirection = true;
  131. characters.transform.localPosition = new Vector3(characters.transform.localPosition.x, characters.transform.localPosition.y, characters.transform.localPosition.z - 0.05f * MoveSpeed);
  132. }
  133. if (Input.GetKey("left") && !(Input.GetKey("down") || Input.GetKey("up") || Input.GetKey("right")))
  134. {
  135. theanim.GetComponent<Animator>().SetBool(charactersfrontwalk, false);
  136. theanim.GetComponent<Animator>().SetBool(charactersbackwalk, false);
  137. theanim.GetComponent<Animator>().SetBool(charactersleftwalk, true);
  138. theanim.GetComponent<Animator>().SetBool(charactersrightwalk, false);
  139. theanim.GetComponent<Animator>().SetBool(charactersfrontidle, false);
  140. theanim.GetComponent<Animator>().SetBool(charactersbackidle, false);
  141. theanim.GetComponent<Animator>().SetBool(charactersrightidle, false);
  142. theanim.GetComponent<Animator>().SetBool(charactersleftidle, false);
  143. LeftDirection = true;
  144. RightDirection = false;
  145. BackDirection = false;
  146. FrontDirection = false;
  147. characters.transform.localPosition = new Vector3(characters.transform.localPosition.x - 0.05f * MoveSpeed, characters.transform.localPosition.y, characters.transform.localPosition.z);
  148. }
  149. if (Input.GetKey("right") && !(Input.GetKey("down") || Input.GetKey("up") || Input.GetKey("left")))
  150. {
  151. theanim.GetComponent<Animator>().SetBool(charactersfrontwalk, false);
  152. theanim.GetComponent<Animator>().SetBool(charactersbackwalk, false);
  153. theanim.GetComponent<Animator>().SetBool(charactersleftwalk, false);
  154. theanim.GetComponent<Animator>().SetBool(charactersrightwalk, true);
  155. theanim.GetComponent<Animator>().SetBool(charactersfrontidle, false);
  156. theanim.GetComponent<Animator>().SetBool(charactersbackidle, false);
  157. theanim.GetComponent<Animator>().SetBool(charactersrightidle, false);
  158. theanim.GetComponent<Animator>().SetBool(charactersleftidle, false);
  159. LeftDirection = false;
  160. RightDirection = true;
  161. BackDirection = false;
  162. FrontDirection = false;
  163. characters.transform.localPosition = new Vector3(characters.transform.localPosition.x + 0.05f * MoveSpeed, characters.transform.localPosition.y, characters.transform.localPosition.z);
  164. }
  165.  
  166.  
  167. }
  168.  
  169. }
  170.  
  171. public class JoystickMove : MonoBehaviour ,IDragHandler,IEndDragHandler
  172. {
  173.  
  174.  
  175. public static JoystickMove instance=null;
  176.  
  177. public float _speed=6f;
  178.  
  179. [Tooltip("the joystick radius ")]
  180. public float R=50f;
  181.  
  182. private float _r;
  183.  
  184. private Vector2 centerPos;
  185.  
  186. private float _h;
  187. private float _v;
  188.  
  189. public float H{
  190. get{return _h;}
  191. }
  192. public float V{
  193. get{return _v;}
  194. }
  195.  
  196. void Awake(){
  197.  
  198. if(instance!=null){
  199. Destroy(this.gameObject);
  200. }else{
  201. instance=this;
  202. }
  203.  
  204.  
  205.  
  206. }
  207. void Start(){
  208.  
  209. _r=1f*Screen.width/960f*R; //this to calculate the scale of screen
  210.  
  211. centerPos=GetComponent<RectTransform>().position;
  212.  
  213. }
  214.  
  215.  
  216.  
  217.  
  218.  
  219.  
  220. void SetHAndF(Vector2 pos){ //Horizontall and Vertical axes
  221.  
  222. Vector2 diff=pos-centerPos;
  223. float distance=diff.magnitude;
  224.  
  225.  
  226. if(distance>_r){
  227. pos=centerPos+diff/distance*_r;
  228.  
  229. }
  230. GetComponent<RectTransform>().position=pos;
  231.  
  232.  
  233. Vector2 move=pos-centerPos;
  234.  
  235. _h=move.x;
  236. _v=move.y;
  237.  
  238. }
  239.  
  240. public void OnDrag(PointerEventData data)
  241. {
  242.  
  243. Vector2 newPos =new Vector2(data.position.x-30f,data.position.y-30f);
  244.  
  245.  
  246.  
  247. SetHAndF(newPos);
  248.  
  249.  
  250.  
  251. }
  252.  
  253. public void OnEndDrag(PointerEventData data){
  254.  
  255. Debug.Log("End Drag"+centerPos);
  256. GetComponent<RectTransform>().position=centerPos;
  257. SetHAndF(centerPos);
  258.  
  259.  
  260. }
  261.  
  262. }
  263.  
  264. using UnityEngine;
  265. using System.Collections;
  266. using UnityEngine.EventSystems;
  267.  
  268.  
  269.  
  270. public class JoystickRotate : JoystickMove
  271. {
  272.  
  273. public static JoystickRotate instance=null;
  274.  
  275.  
  276.  
  277. void Awake(){
  278.  
  279. if(instance!=null){
  280. Destroy(this.gameObject);
  281. }else{
  282. instance=this;
  283. }
  284.  
  285. }
  286.  
  287. public class JoystickFire : MonoBehaviour,IPointerDownHandler,IPointerUpHandler
  288. {
  289.  
  290. public static JoystickFire instance=null;
  291.  
  292. private bool _fire=false;
  293. public bool Fire{
  294. get{return _fire;}
  295. }
  296.  
  297.  
  298. void Awake(){
  299.  
  300. if(instance!=null){
  301. Destroy(this.gameObject);
  302. }else{
  303. instance=this;
  304. }
  305.  
  306. }
  307.  
  308.  
  309.  
  310. public void OnPointerDown(PointerEventData data){
  311.  
  312. _fire=true;
  313. }
  314.  
  315. public void OnPointerUp(PointerEventData data){
  316.  
  317. _fire=false;
  318. }
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement