Run.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using UnityEngine;
  2. using System.Collections;
  3. public class Run : MonoBehaviour
  4. {
  5. public float moveSpeed;
  6. Animator animtor;
  7. Vector3 moveDir = Vector3.zero;
  8. KeyCode curKeyCode = KeyCode.W;
  9. float intervalTime = 0;
  10. // Use this for initialization
  11. void Start()
  12. {
  13. animtor = GetComponent<Animator>();
  14. }
  15. // Update is called once per frame
  16. void Update()
  17. {
  18. if (Input.GetKeyDown(KeyCode.W))
  19. {
  20. curKeyCode = KeyCode.W;
  21. moveDir = Vector3.forward;
  22. this.transform.localRotation = Quaternion.Euler(0,0,0);
  23. if(animtor!=null)
  24. animtor.Play("f_run_1");
  25. }
  26. else if (Input.GetKeyDown(KeyCode.S))
  27. {
  28. curKeyCode = KeyCode.S;
  29. moveDir = Vector3.back;
  30. this.transform.localRotation = Quaternion.Euler(0,180,0);
  31. if (animtor != null)
  32. animtor.Play("f_run_1");
  33. }
  34. else if (Input.GetKeyDown(KeyCode.A))
  35. {
  36. curKeyCode = KeyCode.A;
  37. moveDir = Vector3.left;
  38. this.transform.localRotation = Quaternion.Euler(0, 270, 0);
  39. if (animtor != null)
  40. animtor.Play("f_run_1");
  41. }
  42. else if (Input.GetKeyDown(KeyCode.D))
  43. {
  44. curKeyCode = KeyCode.D;
  45. moveDir = Vector3.right;
  46. this.transform.localRotation = Quaternion.Euler(0, 90, 0);
  47. if (animtor != null)
  48. animtor.Play("f_run_1");
  49. }
  50. if (Input.GetKeyUp(curKeyCode))
  51. {
  52. if (animtor != null)
  53. animtor.Play("f_idle");
  54. moveDir = Vector3.zero;
  55. }
  56. intervalTime -= Time.deltaTime;
  57. this.transform.localPosition += moveDir * moveSpeed;
  58. }
  59. }