Пример #1
0
 void OnCollisionEnter2D(Collision2D col)
 {
     if (col.collider.CompareTag("Player"))
     {
         enemyAttackObj.canAttack = true;
         enemyAttackObj.GetComponent <Animator> ().SetBool("collideWithObj", true);
     }
 }
Пример #2
0
    // Update is called once per frame
    void FixedUpdate()
    {
        if (einfo.thisInfo.IsDead)
        {
            InventoryMG.instance.Goodsbag.coin.itemHeld += 10;
            if (collectObject != null)
            {
                GameObject collect = Instantiate(collectObject, transform.position, new Quaternion(0, 0, 0, 1));
                collectObject = null;
            }
            Destroy(this.gameObject, 1.0f);
        }
        //寻路
        if (path == null | target == null)
        {
            return;
        }
        float dis = Vector2.Distance(target.position, rigidbody2d.position);

        if (dis > maxSearchDis)
        {
            return;
        }
        if (currentWayPoint >= path.vectorPath.Count || dis <= nextWayPointDistance)
        {
            reachEndofPath = true;
            return;
        }
        else
        {
            reachEndofPath = false;
        }
        if (reachEndofPath)
        {
            return;
        }

        Vector2 dir = ((Vector2)(path.vectorPath[currentWayPoint]) - rigidbody2d.position).normalized;

        rigidbody2d.velocity = dir * moveSpeed;

        dis = Vector2.Distance(path.vectorPath[currentWayPoint], rigidbody2d.position);
        if (dis <= nextWayPointDistance)
        {
            currentWayPoint++;
            //Debug.LogFormat("current way point {0} distance {1}", currentWayPoint, dis);
        }


        if (einfo.thisInfo.Type != enemyInfo.Type.boss)
        {
            return;
        }
        if (Time.time - lastAttackTime > attackHoldTime)
        {
            isAttack = false;
        }
        if (Time.time - lastAttackTime > attackRestTime && !isAttack)
        {
            atk   = Instantiate(Resources.Load("enemy/enemyAttack", typeof(enemyAttack))) as enemyAttack;
            atk.E = this;
            Vector2 atkPos = collider.ClosestPoint((Vector2)(target.position));
            atk.GetComponent <Transform>().position = new Vector3(atkPos.x, atkPos.y, 0);
            atkDir         = (target.position - this.transform.position).normalized;
            isAttack       = true;
            lastAttackTime = Time.time;
            Destroy(atk.gameObject, attackHoldTime);
        }
        if (isAttack)
        {
            atk.GetComponent <Rigidbody2D>().velocity = rigidbody2d.velocity + atkMoveSpeed * (Vector2)atkDir;
            //Debug.LogFormat("enemy attack velocity {0}", atk.GetComponent<Rigidbody2D>().velocity);
        }
        else if (atk)
        {
            Destroy(atk.gameObject, 0.3f);
        }
        animator.SetBool("is_attack", isAttack);
        animator.SetBool("is_dead", einfo.thisInfo.IsDead);


        //攻击
        //atk.Attack(target);
    }