Guest User

Untitled

a guest
Mar 27th, 2025
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. using UnityEngine.AI;
  4. public class Ghost : MonoBehaviour
  5. {
  6.  
  7. public Animator animator;
  8. public NavMeshAgent agent;
  9.  
  10. public float eatDistance = 0.2f;
  11. public float speed = 1;
  12. // Start is called once before the first execution of Update after the MonoBehaviour is created
  13. void Start()
  14. {
  15.  
  16. }
  17.  
  18. public GameObject GetClosestOrb(){
  19. GameObject closest = null;
  20. float minDistance = Mathf.Infinity;
  21. List<GameObject> orbs = OrbsSpanwer.instance.spawnedOrbs;
  22. foreach(var item in orbs)
  23. {
  24. Vector3 ghostPosition = transform.position;
  25. ghostPosition.y = 0;
  26.  
  27. Vector3 orbPosition = item.transform.position;
  28. orbPosition.y = 0;
  29.  
  30.  
  31. float d = Vector3.Distance(ghostPosition, orbPosition);
  32. if(d < minDistance)
  33. {
  34. minDistance = d;
  35. closest = item;
  36. }
  37. }
  38.  
  39. if(minDistance < eatDistance)
  40. {
  41. OrbsSpanwer.instance.DestroyOrb(closest);
  42. }
  43. return closest;
  44. }
  45. // Update is called once per frame
  46. void Update()
  47. {
  48. if(!agent.enabled)
  49. return;
  50.  
  51. GameObject closest = GetClosestOrb();
  52. if(closest)
  53. {
  54. Vector3 targetPosition = closest.transform.position;
  55. //Camera.main.transform.position;
  56. agent.SetDestination(targetPosition);
  57. agent.speed = speed;
  58. }
  59. /*else{
  60. Vector3 targetPosition = Camera.main.transform.position;
  61.  
  62. agent.SetDestination(targetPosition);
  63. agent.speed = speed;
  64. }*/
  65. }
  66.  
  67. public void Kill()
  68. {
  69. agent.enabled = false;
  70. animator.SetTrigger("Death");
  71. }
  72.  
  73. public void Destroy()
  74. {
  75. Destroy(gameObject);
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment