Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class PlayerController : MonoBehaviour {
  6. // Start is called before the first frame update
  7. public int speed = 1;
  8.  
  9.  
  10. private Animator anim;
  11.  
  12. private float timeSinceMovedLeft;
  13. private bool movingLeft = false;
  14. private bool movingRight = false;
  15. private bool rotatedLeftEnough = false;
  16. private bool rotatedRightEnough = false;
  17.  
  18. void Start() {
  19. anim = GetComponent<Animator>();
  20. }
  21.  
  22. void clearAnimations() {
  23. anim.SetBool("isWalking", false);
  24. }
  25.  
  26. void clearMovements() {
  27. transform.rotation = Quaternion.Euler(0, 0, 0);
  28. rotatedLeftEnough = false;
  29. rotatedRightEnough = false;
  30. }
  31.  
  32. void MoveFoward() {
  33. anim.SetBool("isWalking", true);
  34. transform.Translate(Vector3.forward * speed * Time.deltaTime);
  35. }
  36.  
  37. void MoveLeft() {
  38.  
  39. if (movingLeft) {
  40. timeSinceMovedLeft += Time.deltaTime;
  41. if (timeSinceMovedLeft > 0.2) {
  42. Quaternion rotateAmount = Quaternion.Euler(0, -90f, 0f);
  43. float angle = Quaternion.Angle(transform.rotation, rotateAmount);
  44. if (angle >= 0 && angle <= 10) {
  45. rotatedLeftEnough = true;
  46. }
  47.  
  48. if (rotatedLeftEnough) {
  49. MoveFoward();
  50. } else {
  51. transform.Rotate(0f, -5f, 0f);
  52. }
  53.  
  54. }
  55. }
  56. movingLeft = true;
  57. return;
  58. }
  59.  
  60. void MoveRight() {
  61. if (movingRight) {
  62. timeSinceMovedLeft += Time.deltaTime;
  63. if (timeSinceMovedLeft > 0.2) {
  64. Quaternion rotateAmount = Quaternion.Euler(0, 90f, 0f);
  65. float angle = Quaternion.Angle(transform.rotation, rotateAmount);
  66. if (angle >= 0 && angle <= 10) {
  67. rotatedRightEnough = true;
  68. }
  69.  
  70. if (rotatedRightEnough) {
  71. MoveFoward();
  72. } else {
  73. transform.Rotate(0f, 5f, 0f);
  74. }
  75. }
  76. }
  77. movingRight = true;
  78. return;
  79. }
  80.  
  81. void SetAnimations() {
  82. if (Input.GetKey("w")) {
  83. MoveFoward();
  84. return;
  85. }
  86. if (Input.GetKey("d")) {
  87. MoveRight();
  88. return;
  89. }
  90. if (Input.GetKey("a")) {
  91. MoveLeft();
  92. return;
  93. }
  94. clearAnimations();
  95. clearMovements();
  96. return;
  97. }
  98.  
  99. void Update() {
  100. SetAnimations();
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement