达到敌人一旦达到目标点?

时间:2018-04-07 08:15:52

标签: c# unity3d

我制造了一个AI Enemy系统让敌人走向我想攻击的目标。目标(玩家收集点数)。我想要的是当敌人到达目标时它会从玩家身上夺走-1点。我想知道的是如何从游戏经理中扣除这一点。我已经创建了一个公共游戏对象,其中包含我的点的脚本。当我尝试这一点时,敌人一旦到达玩家就不会消失。我应该在我的Enemy AI脚本中创建一个名为TargetIsReached()的函数吗?

public class aiEnemy : MonoBehaviour, IPooledObject {

    public float lookRadius = 40f;


    Transform target;
    UnityEngine.AI.NavMeshAgent agent;

    Rigidbody theRigidBody;

    public Casting castingScript;

    void Start(){
        target = PlayerManager.instance.player.transform;
        agent = GetComponent<UnityEngine.AI.NavMeshAgent>();

    }


    void Update(){
        float distance = Vector3.Distance (target.position, transform.position);
        if (distance <= lookRadius)
        {
            agent.SetDestination (target.position);

            if (distance <= agent.stoppingDistance)
            {
                FaceTarget ();
                castingScript.RemovePoint ();
            }
        }

    }

    void FaceTarget()
    {
        Vector3 direction = (target.position - transform.position).normalized;
        Quaternion lookRotation = Quaternion.LookRotation (new Vector3 (direction.x, 0, direction.z));
        transform.rotation = Quaternion.Slerp (transform.rotation, lookRotation, Time.deltaTime * 5f);
    }


    // Use this for initialization
    public void OnObjectSpawn () {


        //myRender = GetComponent<Renderer> ();
        theRigidBody = GetComponent<Rigidbody>();

    }

    void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere (transform.position, lookRadius);
    }

}

// Casting Script //

public GameObject Float;

public Transform target;

public GameObject disableFish;
public float waitTime= 3f;

public int scoreValue = 1;
int waterMask;

public float maxCastDist = 1000f;
public float fishDist = 1f;

public bool hitObject;

void Awake ()
{

    waterMask = LayerMask.GetMask ("Water"); // will only cast in water

}



void Update ()
{
    if (EventSystem.current.IsPointerOverGameObject ()) {
        return;

    } else {

        if (Input.GetMouseButtonDown (0)) {

            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);

            if (Physics.Raycast (ray, out hit, maxCastDist, waterMask))
            if (hit.collider != null) {

                float dist = Vector3.Distance (hit.point, target.position);
                Debug.Log (dist);
                Destroy (GameObject.FindWithTag ("Float"));

                Instantiate (Float, hit.point, Quaternion.identity);
                if (dist < fishDist) {

                    disableFish.SetActive(false);
                    StartCoroutine (fishWaiter ()); 
                    StartCoroutine (DestroyOb(waitTime));

                } 

            }
        }
    }
}




IEnumerator fishWaiter ()
{
    FishLoader.SetActive (true);

    FishSlider.value = waitTime;

    Debug.Log ("Timer Started");
    yield return new WaitForSeconds (waitTime);
    print ("I waited" + waitTime + "Sec");

    FishSlider.value = 0f;


    print ("You Caught The Fish!");
    ScoreManager.score += scoreValue;
}


IEnumerator DestroyOb (float waitTime){
    yield return new WaitForSeconds (waitTime);
    Destroy (GameObject.FindWithTag ("Float"));
}

}

0 个答案:

没有答案