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.

125 lines
3.4 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
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 += cam.transform.rotation * 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 = new Vector3(receivedInput.x, 0.0f, receivedInput.y);
  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.z),0.0f, -90 * Math.Sign(input.x));
  78. //model.transform.rotation *= Quaternion.AngleAxis(cam.transform.eulerAngles.y, Vector3.up) * Quaternion.Euler(rotateDir);
  79. model.transform.Rotate(rotateDir, Space.World);
  80. }
  81. }
  82. public void DestroyOffCamera()
  83. {
  84. Vector3 viewPos = cam.WorldToViewportPoint(transform.position);
  85. if (viewPos.y < -0.1)
  86. herd.RemoveHorse(this);
  87. }
  88. // Update is called once per frame
  89. void Update()
  90. {
  91. speedMulitplier = ((Mathf.Sin(Time.time * UnityEngine.Random.Range(0.95f, 1.05f) + randomizer) + 2) * 0.25f);
  92. directionRandmoizer = Mathf.Cos(Time.time + randomizer / 2) * 0.0f;
  93. UpdatePosition();
  94. DestroyOffCamera();
  95. }
  96. public void AddForce(Vector3 direction)
  97. {
  98. moveDirection += direction;
  99. isRagdoll = true;
  100. }
  101. }