Beispiel #1
0
    void Update()
    {
        if (state == stateEnemy.dead)
        {
            return;
        }
        RaycastHit hit;
        Vector3    forward = transform.TransformDirection(Vector3.forward) * limitRay;

        Debug.DrawRay(transform.position, forward, Color.green);
        if (Physics.Raycast(transform.position, forward, out hit, limitRay))
        {
            if (hit.collider.tag.Contains("Tower"))
            {
                state = stateEnemy.attack;
            }
        }

        if (state == stateEnemy.walk)
        {
            navMeshAgent.SetDestination(target.position);
        }
        else if (state == stateEnemy.attack)
        {
            navMeshAgent.isStopped = true;
            anim.SetBool("Attack", true);
            anim.SetBool("Walk", false);
        }
    }
Beispiel #2
0
 protected bool changeState(bool condition, stateEnemy newState)
 {
     if (condition)
     {
         state = newState;
         return(true);
     }
     return(false);
 }
Beispiel #3
0
 void ListenBullet(Vector3 pos, float dist)
 {
     // Debug.Log("ListenBullet : " + pos + dist);
     if (Vector3.Distance(pos, transform.position) <= dist)
     {
         // Debug.Log("Enemi " + name + " heard the bullet shot.");
         state         = stateEnemy.sound_alert;
         soundPosition = pos;
         _timerRun     = 2f;
     }
 }
Beispiel #4
0
 void OnTriggerEnter(Collider col)
 {
     if (col.tag.Contains("Arrow"))
     {
         if (life > 0)
         {
             life--;
         }
         else if (life <= 0)
         {
             state = stateEnemy.dead;
             GameObject.FindObjectOfType <PoolEnemy> ().SetEnemy(this.gameObject);
         }
     }
 }
Beispiel #5
0
 public void patrolling()
 {
     if (_checkPoints == null || _checkPoints.Count == 0)
     {
         state = stateEnemy.routine;
         return;
     }
     runToPos(_checkPoints[_index].position);
     if (onPosition(transform.position, _checkPoints[_index].position))
     {
         // Debug.Log("Goto next check point");
         _index++;
         if (_index == _checkPoints.Count)
         {
             _index = 0;
         }
     }
 }
Beispiel #6
0
    // Use this for initialization
    void Start()
    {
        state   = stateEnemy.routine;
        _player = player.instance;
        _rb     = gameObject.GetComponent <Rigidbody2D>();
        if (player.instance != null)
        {
            player.instance.OnGunShooted += ListenBullet;
            // Debug.Log("listen to bullet in start");
        }
        // string path = "Assets/Sprites/characters/body/" + Random.Range(1, 4).ToString() + ".png";
        // Debug.Log("body = " + path);
        // body.sprite = (Sprite)AssetDatabase.LoadAssetAtPath(path, typeof(Sprite));
        body.sprite = bodySprites[Random.Range(0, bodySprites.Length)];

        // path = "Assets/Sprites/characters/head/" + Random.Range(1, 13).ToString() + ".png";
        // Debug.Log("head = " + path);
        // head.sprite = (Sprite)AssetDatabase.LoadAssetAtPath(path, typeof(Sprite));
        head.sprite = headSprites[Random.Range(0, headSprites.Length)];

        _audioSrc = GetComponent <AudioSource>();
    }
Beispiel #7
0
    void Update()
    {
        if (_isAlive == false)
        {
            return;
        }

        Vector3   heading = _player.transform.position - transform.position;
        LayerMask mask    = LayerMask.GetMask("door", "Wall", "player");
        float     angle   = Vector2.Angle(transform.up, heading);
        float     dist    = 1f;

        if (isWithin(angle, 155f, 205f))
        {
            dist = 7f;
        }
        RaycastHit2D hit = Physics2D.Raycast(transform.position, heading, dist, mask);

        if (hit && hit.collider.gameObject.layer == LayerMask.NameToLayer("player"))
        {
            state = stateEnemy.view_player;
            if (_routine == null)
            {
                _routine = fireEverySeconds();
                StartCoroutine(_routine);
            }
            _timerRun = 3f;
        }
        else if (_routine != null)
        {
            StopCoroutine(_routine);
            _routine = null;
        }
        // return;
        switch (state)
        {
        case stateEnemy.stay:
            if (_checkPoints != null)
            {
                state = stateEnemy.routine;
            }
            break;

        case stateEnemy.sound_alert:
            _timerRun -= Time.deltaTime;
            if (changeState(_timerRun < 0, stateEnemy.stay))
            {
                break;
            }
            runToPos(soundPosition);
            changeState(soundPosition == transform.position, stateEnemy.stay);
            break;

        case stateEnemy.view_player:
            _timerRun -= Time.deltaTime;
            if (changeState(_timerRun < 0, stateEnemy.stay))
            {
                break;
            }
            runToPos(_player.transform.position);
            break;

        case stateEnemy.routine:
            patrolling();
            break;
        }
        ;
    }
Beispiel #8
0
 public void StartParameterEnemy()
 {
     state = stateEnemy.walk;
 }