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.

122 lines
3.2 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 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 void MoveObject(float wait)
  68. {
  69. StartCoroutine(RandomWait(wait));
  70. }
  71. public IEnumerator RandomWait(float wait)
  72. {
  73. Vector3 input = receivedInput;
  74. yield return new WaitForSeconds(UnityEngine.Random.Range(0, wait / 2));
  75. if (cController.isGrounded)
  76. {
  77. Vector3 rotateDir = new Vector3(90 * Math.Sign(input.y), 0, -90 * Math.Sign(input.x));
  78. model.transform.Rotate(rotateDir, Space.World);
  79. }
  80. }
  81. public void DestroyOffCamera()
  82. {
  83. Vector3 viewPos = cam.WorldToViewportPoint(transform.position);
  84. if (viewPos.y < -0.1)
  85. herd.RemoveHorse(this);
  86. }
  87. // Update is called once per frame
  88. void Update()
  89. {
  90. speedMulitplier = ((Mathf.Sin(Time.time * UnityEngine.Random.Range(0.95f, 1.05f) + randomizer) + 2) * 0.25f);
  91. directionRandmoizer = Mathf.Cos(Time.time + randomizer / 2) * 0.0f;
  92. UpdatePosition();
  93. DestroyOffCamera();
  94. }
  95. public void AddForce(Vector3 direction)
  96. {
  97. moveDirection += direction;
  98. isRagdoll = true;
  99. }
  100. }