Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.AI;
- public class Ghost : MonoBehaviour
- {
- public Animator animator;
- public NavMeshAgent agent;
- public float eatDistance = 0.2f;
- public float speed = 1;
- // Start is called once before the first execution of Update after the MonoBehaviour is created
- void Start()
- {
- }
- public GameObject GetClosestOrb(){
- GameObject closest = null;
- float minDistance = Mathf.Infinity;
- List<GameObject> orbs = OrbsSpanwer.instance.spawnedOrbs;
- foreach(var item in orbs)
- {
- Vector3 ghostPosition = transform.position;
- ghostPosition.y = 0;
- Vector3 orbPosition = item.transform.position;
- orbPosition.y = 0;
- float d = Vector3.Distance(ghostPosition, orbPosition);
- if(d < minDistance)
- {
- minDistance = d;
- closest = item;
- }
- }
- if(minDistance < eatDistance)
- {
- OrbsSpanwer.instance.DestroyOrb(closest);
- }
- return closest;
- }
- // Update is called once per frame
- void Update()
- {
- if(!agent.enabled)
- return;
- GameObject closest = GetClosestOrb();
- if(closest)
- {
- Vector3 targetPosition = closest.transform.position;
- //Camera.main.transform.position;
- agent.SetDestination(targetPosition);
- agent.speed = speed;
- }
- /*else{
- Vector3 targetPosition = Camera.main.transform.position;
- agent.SetDestination(targetPosition);
- agent.speed = speed;
- }*/
- }
- public void Kill()
- {
- agent.enabled = false;
- animator.SetTrigger("Death");
- }
- public void Destroy()
- {
- Destroy(gameObject);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment