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.

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