0
votes

Here is a Video also - http://tinypic.com/r/mmagki/9

Here is my start () function

void Start()
    {

        target = GameObject.FindGameObjectWithTag("Player").transform;

    }

and update() function

void Update()
    {

            transform.LookAt(target);
            float step = speed * Time.deltaTime;
            distance = (transform.position - target.position).magnitude;

            //Debug.Log("Now distance -" + distance);

            if (distance < 20)
            {

                //       print("In Range");
                transform.GetComponent<Animation>().Play("attack", PlayMode.StopAll);

                if (isAttacking == false)
                {
                    isAttacking = true;
                    Hit.playerHealth -= Random.Range(20f, 25f) * Time.deltaTime;
                    Hit.playerHealth -= Random.Range(20f, 25f);
                    //   StartCoroutine(MyCoroutine(4));
                    //      print("Player Health Status = " + Hit.playerHealth);               

                    if (Hit.playerHealth <= 0)
                    {
                        //                 print("Player dead");
                    }
                }
                else
                {

                }

            }
            else
            {
                //        print("Out of Range");
                transform.position = Vector3.MoveTowards(transform.position, target.position, step);
                transform.GetComponent<Animation>().Play("walk", PlayMode.StopAll);
            }

}

My Zombie (Enemy) is approaching Player, when Zombie gets hit with the wall, he should go to gate.

What i did, as soon as Zombie (Set Trigger = checked) hit with the wall, i have changed the reference of 'target' to Object with tag 'gate'. Now the Zombie is not moving towards gate object (i have set tag 'gate' also). he is still moving towards player only. Not able to change the reference of target.

void OnTriggerEnter(Collider col)
        {
               if (col.gameObject.tag == "wall")
            {

                target = GameObject.FindGameObjectWithTag("gate").transform;
                Debug.Log("Yes its a onTrigger Enter function , hitting with wall");
               }

        }
2

2 Answers

0
votes

It looks like you don't have a collider on your gate. Adding a collider to the gate should do the trick.

0
votes

first verify the tags of all are seted correctly, then you need to add a rigidbody component at least in one of the objects as Unity Docs say: OnTriggerEnter . I recomend to add it to the zombie and set UseGravity to false. and with this it should work... sorry for bad english.