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.

119 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
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class BallController : MonoBehaviour
  5. {
  6. public bool EatHorse;
  7. public int HorseCount;
  8. public float size = 1;
  9. public GameObject horsePrefab;
  10. public HerdController herd;
  11. public float Speed = 10;
  12. private List<PlayerController> disabledHorses = new List<PlayerController>();
  13. private Rigidbody rigid;
  14. private Camera cam;
  15. private Vector2 recievdInput;
  16. private SphereCollider collider;
  17. private void Start()
  18. {
  19. herd = FindObjectOfType<HerdController>();
  20. rigid = GetComponent<Rigidbody>();
  21. collider = GetComponent<SphereCollider>();
  22. cam = Camera.main;
  23. Debug.Log(herd.Centre.position);
  24. }
  25. // Update is called once per frame
  26. void FixedUpdate()
  27. {
  28. if (EatHorse)
  29. {
  30. rigid.isKinematic = false;
  31. collider.enabled = true;
  32. foreach (PlayerController horse in herd.Herd)
  33. {
  34. if (horse.GetComponent<PlayerController>().enabled)
  35. {
  36. horse.AddForce((transform.position - horse.transform.position).normalized * 20);
  37. if (Vector3.Distance(horse.transform.position, transform.position) < size * 2)
  38. AddHorse(horse);
  39. }
  40. }
  41. }
  42. else
  43. {
  44. rigid.isKinematic = true;
  45. collider.enabled = false;
  46. size = 1;
  47. if (disabledHorses.Count != 0)
  48. {
  49. foreach (PlayerController horse in disabledHorses)
  50. {
  51. horse.enabled = true;
  52. horse.cController.enabled = true;
  53. horse.transform.parent = herd.transform;
  54. horse.moveDirection = Vector3.zero;
  55. var vec = horse.transform.eulerAngles;
  56. vec.x = Mathf.Round(vec.x / 90) * 90;
  57. vec.y = Mathf.Round(vec.y / 90) * 90;
  58. vec.z = Mathf.Round(vec.z / 90) * 90;
  59. horse.transform.eulerAngles = vec;
  60. }
  61. disabledHorses.Clear();
  62. }
  63. }
  64. DoMove();
  65. }
  66. public void DoMove()
  67. {
  68. Vector3 direction = cam.transform.rotation * new Vector3(recievdInput.x, 0.0f, recievdInput.y);
  69. rigid.AddForce(direction * Speed * Time.fixedDeltaTime,ForceMode.VelocityChange);
  70. }
  71. public void GetInput(Vector2 input)
  72. {
  73. recievdInput = input;
  74. }
  75. private void AddHorse(PlayerController horse)
  76. {
  77. size += 0.5f / (size*10);
  78. collider.radius = size;
  79. foreach(Transform child in transform)
  80. {
  81. child.localPosition = child.localPosition.normalized * size;
  82. }
  83. horse.GetComponent<PlayerController>().enabled = false;
  84. horse.GetComponent<CharacterController>().enabled = false;
  85. Vector3 position = Random.onUnitSphere * size + transform.position;
  86. horse.transform.position = position;
  87. horse.transform.rotation = Random.rotation;
  88. horse.transform.parent = transform;
  89. disabledHorses.Add(horse);
  90. }
  91. }