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.

149 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
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. }
  67. public float RotateObject(float xInput, float zInput)
  68. {
  69. float to = model.transform.rotation.eulerAngles.y;
  70. //Debug.Log(to);
  71. if (receivedInput.x != 0 || receivedInput.y != 0)
  72. {
  73. if (zInput > 0 && xInput == 0)
  74. to = 0;
  75. else if (zInput < 0 && xInput == 0)
  76. to = 180;
  77. else if (zInput == 0 && xInput > 0)
  78. to = 90;
  79. else if (zInput == 0 && xInput < 0)
  80. to = 270;
  81. else if (zInput > 0 && xInput > 0)
  82. to = 45;
  83. else if (zInput < 0 && xInput > 0)
  84. to = 135;
  85. else if (zInput < 0 && xInput < 0)
  86. to = 225;
  87. else if (zInput > 0 && xInput < 0)
  88. to = 315;
  89. }
  90. return to;
  91. }
  92. public void MoveObject(float wait)
  93. {
  94. StartCoroutine(RandomWait(wait));
  95. }
  96. public IEnumerator RandomWait(float wait)
  97. {
  98. Vector3 input = receivedInput;
  99. yield return new WaitForSeconds(UnityEngine.Random.Range(0, wait / 2));
  100. if (cController.isGrounded)
  101. {
  102. Vector3 rotateDir = new Vector3(90 * Math.Sign(input.y), 0, -90 * Math.Sign(input.x));
  103. model.transform.Rotate(rotateDir, Space.World);
  104. }
  105. }
  106. public void DestroyOffCamera()
  107. {
  108. Vector3 viewPos = cam.WorldToViewportPoint(transform.position);
  109. if (viewPos.y < -0.1)
  110. herd.RemoveHorse(this);
  111. }
  112. // Update is called once per frame
  113. void Update()
  114. {
  115. speedMulitplier = ((Mathf.Sin(Time.time * UnityEngine.Random.Range(0.95f, 1.05f) + randomizer) + 2) * 0.25f);
  116. directionRandmoizer = Mathf.Cos(Time.time + randomizer / 2) * 0.0f;
  117. UpdatePosition();
  118. DestroyOffCamera();
  119. }
  120. public void AddForce(Vector3 direction)
  121. {
  122. moveDirection += direction;
  123. isRagdoll = true;
  124. }
  125. }