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.

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