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.

111 lines
3.0 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
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. public float speedMulitplier;
  11. private float randomizer;
  12. private float directionRandmoizer;
  13. private Vector2 receivedInput;
  14. private float moveDelta;
  15. private float lastMoveTime;
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. randomizer = UnityEngine.Random.Range(0,10);
  20. }
  21. public void SetMovement(Vector2 input)
  22. {
  23. receivedInput = input;
  24. }
  25. public void UpdatePosition()
  26. {
  27. moveDelta = Time.time - lastMoveTime;
  28. lastMoveTime = Time.time;
  29. if (receivedInput.magnitude == 0)
  30. return;
  31. float HorseX, HorseZ;
  32. HorseZ = receivedInput.y;
  33. HorseX = receivedInput.x;
  34. if (Mathf.Abs(HorseZ) == Mathf.Min(Mathf.Abs(HorseZ), Mathf.Abs(HorseX)))
  35. HorseZ += directionRandmoizer;
  36. else
  37. HorseX += directionRandmoizer;
  38. float rotateTo = RotateObject(HorseX, HorseZ);
  39. HorseZ *= walkSpeed * speedMulitplier * moveDelta;
  40. HorseX *= walkSpeed * speedMulitplier * moveDelta;
  41. transform.Translate(HorseX, 0, HorseZ);
  42. //Vector3 dir = Quaternion.Euler(-90, rotateTo, 0) * Vector3.forward;
  43. //model.transform.forward = dir;
  44. //LeanTween.rotateY(model, rotateTo, 0.1f);
  45. }
  46. public float RotateObject(float xInput, float zInput)
  47. {
  48. float to = model.transform.rotation.eulerAngles.y;
  49. //Debug.Log(to);
  50. if (receivedInput.x != 0 || receivedInput.y != 0)
  51. {
  52. if (zInput > 0 && xInput == 0)
  53. to = 0;
  54. else if (zInput < 0 && xInput == 0)
  55. to = 180;
  56. else if (zInput == 0 && xInput > 0)
  57. to = 90;
  58. else if (zInput == 0 && xInput < 0)
  59. to = 270;
  60. else if (zInput > 0 && xInput > 0)
  61. to = 45;
  62. else if (zInput < 0 && xInput > 0)
  63. to = 135;
  64. else if (zInput < 0 && xInput < 0)
  65. to = 225;
  66. else if (zInput > 0 && xInput < 0)
  67. to = 315;
  68. }
  69. return to;
  70. }
  71. public void MoveObject(float wait)
  72. {
  73. StartCoroutine(RandomWait(wait));
  74. }
  75. public IEnumerator RandomWait(float wait)
  76. {
  77. yield return new WaitForSeconds(UnityEngine.Random.Range(0, wait * randomizer/10));
  78. Vector3 rotateDir = new Vector3(90 * Math.Sign(receivedInput.y), 0, -90 * Math.Sign(receivedInput.x));
  79. model.transform.Rotate(rotateDir, Space.World);
  80. }
  81. // Update is called once per frame
  82. void Update()
  83. {
  84. speedMulitplier = (Mathf.Sin(Time.time * UnityEngine.Random.Range(0.9f,1.1f) + randomizer) + 2);
  85. directionRandmoizer = Mathf.Cos(Time.time + randomizer / 2) * 0.3f;
  86. UpdatePosition();
  87. }
  88. }