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.

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