You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using Random = System.Random;
  6. public class PlayerController : MonoBehaviour
  7. {
  8. public float walkSpeed;
  9. public GameObject model;
  10. private float speedMulitplier;
  11. private Vector2 receivedInput;
  12. // Start is called before the first frame update
  13. void Start()
  14. {
  15. speedMulitplier = UnityEngine.Random.Range(0.8f, 1.2f);
  16. }
  17. public void SetMovement(Vector2 input)
  18. {
  19. receivedInput = input;
  20. }
  21. public void UpdatePosition()
  22. {
  23. float HorseX, HorseZ;
  24. HorseZ = receivedInput.y;
  25. HorseX = receivedInput.x;
  26. float rotateTo = RotateObject(HorseX, HorseZ);
  27. HorseZ *= Time.deltaTime * walkSpeed * speedMulitplier;
  28. HorseX *= Time.deltaTime * walkSpeed * speedMulitplier;
  29. transform.Translate(HorseX, 0, HorseZ);
  30. //Vector3 dir = Quaternion.Euler(-90, rotateTo, 0) * Vector3.forward;
  31. //model.transform.forward = dir;
  32. //LeanTween.rotateY(model, rotateTo, 0.1f);
  33. }
  34. public float RotateObject(float xInput, float zInput)
  35. {
  36. float to = model.transform.rotation.eulerAngles.y;
  37. //Debug.Log(to);
  38. if (receivedInput.x != 0 || receivedInput.y != 0)
  39. {
  40. if (zInput > 0 && xInput == 0)
  41. to = 0;
  42. else if (zInput < 0 && xInput == 0)
  43. to = 180;
  44. else if (zInput == 0 && xInput > 0)
  45. to = 90;
  46. else if (zInput == 0 && xInput < 0)
  47. to = 270;
  48. else if (zInput > 0 && xInput > 0)
  49. to = 45;
  50. else if (zInput < 0 && xInput > 0)
  51. to = 135;
  52. else if (zInput < 0 && xInput < 0)
  53. to = 225;
  54. else if (zInput > 0 && xInput < 0)
  55. to = 315;
  56. }
  57. return to;
  58. }
  59. public void MoveObject(float wait)
  60. {
  61. StartCoroutine(RandomWait(wait));
  62. }
  63. public IEnumerator RandomWait(float wait)
  64. {
  65. yield return new WaitForSeconds(UnityEngine.Random.Range(0, wait/2));
  66. Vector3 rotateDir = new Vector3(90 * Math.Sign(receivedInput.y), 0, -90 * Math.Sign(receivedInput.x));
  67. model.transform.Rotate(rotateDir, Space.World);
  68. }
  69. // Update is called once per frame
  70. void Update()
  71. {
  72. UpdatePosition();
  73. }
  74. }