Ejemplo n.º 1
0
    public override Type Tick()
    {
        _rangedEnemy.animator.SetTrigger("Walk");

        if (_rangedEnemy._target)
        {
            if (!_rangedEnemy.CheckObstacles(_rangedEnemy._target))
            {
                if (_rangedEnemy.melee)
                {
                    return(typeof(ChaseState));
                }
                else
                {
                    return(typeof(AttackState));
                }
            }
        }

        _rangedEnemy.transform.position = Vector3.MoveTowards(_rangedEnemy.transform.position, nextWaypoint.transform.position, speed * Time.deltaTime);

        distance = Vector2.Distance(_rangedEnemy.transform.position, nextWaypoint.transform.position);
        if (distance < stopDistance)
        {
            if (nextWaypoint == _waypoints[0])
            {
                nextWaypoint = _waypoints[1];
            }
            else
            {
                nextWaypoint = _waypoints[0];
            }
        }

        if (_rangedEnemy.transform.position.x < nextWaypoint.transform.position.x)
        {
            _rangedEnemy.transform.rotation = Quaternion.Euler(0, 0, 0);
        }
        else
        {
            _rangedEnemy.transform.rotation = Quaternion.Euler(0, 180, 0);
        }

        _rangedEnemy.eyes.transform.rotation = _rangedEnemy.transform.rotation;

        //palauttaa silmän reunalta paluun jälkeen

        /*  if (!_rangedEnemy.eyes.activeInHierarchy)
         * {
         *    delayLeft--;
         *    if (delayLeft < 0)
         *    {
         *        _rangedEnemy.eyes.SetActive(true);
         *        delayLeft = delay;
         *    }
         *
         * }*/

        return(null);
    }
Ejemplo n.º 2
0
    public override Type Tick()
    {
        if (_rangedEnemy._target)
        {
            if (!_rangedEnemy.CheckObstacles(_rangedEnemy._target))
            {
                return(typeof(ChaseState));
            }
        }

        if (transform.position.x == _rangedEnemy.startPos.x)
        {
            _rangedEnemy.animator.SetTrigger("Idle");
        }

        if (transform.position.x != _rangedEnemy.startPos.x)
        {
            _rangedEnemy.animator.SetTrigger("Walk");
            if (transform.position.x > _rangedEnemy.startPos.x)
            {
                transform.rotation = Quaternion.Euler(0, 180, 0);
            }
            else
            {
                transform.rotation = Quaternion.Euler(0, 0, 0);
            }
            _rangedEnemy.eyes.transform.rotation = transform.rotation;
            transform.position = Vector2.MoveTowards(transform.position, _rangedEnemy.startPos, 5f * Time.deltaTime);
        }

        else if (_rangedEnemy.turning)
        {
            timeLeft -= Time.deltaTime;
            if (timeLeft < 0)
            {
                _rangedEnemy.Flip();
                timeLeft = _rangedEnemy.turnInterval;
            }
        }

        if (_rangedEnemy.eyes.transform.rotation.z != 0)
        {
            _rangedEnemy.eyes.transform.rotation = Quaternion.Euler(_rangedEnemy.transform.rotation.x, _rangedEnemy.transform.rotation.y, 0);
        }



        //palauttaa silmän reunalta paluun jälkeen

        /*   if (!_rangedEnemy.eyes.activeInHierarchy)
         * {
         *     delayLeft--;
         *     if (delayLeft < 0)
         *     {
         *         _rangedEnemy.eyes.SetActive(true);
         *         delayLeft = delay;
         *     }
         *
         * }*/

        return(null);
    }
Ejemplo n.º 3
0
    public override Type Tick()
    {
        if (!_rangedEnemy._target || _rangedEnemy.CheckObstacles(_rangedEnemy._target))
        {
            if (_rangedEnemy.patrolling)
            {
                _rangedEnemy.animator.SetTrigger("Walk");
                return(typeof(PatrolState));
            }
            else
            {
                _rangedEnemy.animator.SetTrigger("Idle");
                return(typeof(WatchState));
            }
        }

        float distance = Vector2.Distance(transform.position, _rangedEnemy._target.transform.position);

        if (_rangedEnemy.melee && distance > _rangedEnemy.meleeAttackRange)
        {
            _rangedEnemy.animator.SetTrigger("Walk");
            return(typeof(ChaseState));
        }
        //katsoo pelaajaa kohti
        Vector3    vectorToTarget = _rangedEnemy._target.transform.position - _rangedEnemy.eyes.transform.position;
        Vector3    gunVector      = _rangedEnemy._target.transform.position - _rangedEnemy.gunRotator.transform.position;
        float      angle          = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
        float      gunAngle       = Mathf.Atan2(gunVector.y, gunVector.x) * Mathf.Rad2Deg;
        Quaternion q  = Quaternion.AngleAxis(angle, Vector3.forward);
        Quaternion q2 = Quaternion.AngleAxis(gunAngle, Vector3.forward);

        _rangedEnemy.eyes.transform.rotation       = Quaternion.Slerp(_rangedEnemy.eyes.transform.rotation, q, Time.deltaTime * 100);
        _rangedEnemy.gunRotator.transform.rotation = Quaternion.Slerp(_rangedEnemy.gunRotator.transform.rotation, q2, Time.deltaTime * 100);

        //kääntää vihollisen kohti pelaajaa
        if (_rangedEnemy._target.transform.position.x < _rangedEnemy.transform.position.x)
        {
            _rangedEnemy.transform.rotation = Quaternion.Euler(0, 180, 0);
        }
        else
        {
            _rangedEnemy.transform.rotation = Quaternion.Euler(0, 0, 0);
        }


        if (_rangedEnemy == null)
        {
            if (_rangedEnemy.patrolling)
            {
                _rangedEnemy.animator.SetTrigger("Walk");
                return(typeof(PatrolState));
            }
            else
            {
                _rangedEnemy.animator.SetTrigger("Idle");
                return(typeof(WatchState));
            }
        }

        // _attackCooldown -= Time.deltaTime;
        if (_rangedEnemy.melee)
        {
            _rangedEnemy.firstMelee -= Time.deltaTime;
            if (_rangedEnemy.firstMelee < 0)
            {
                _rangedEnemy.Attack(_rangedEnemy._target);
                _rangedEnemy.firstMelee = _rangedEnemy.meleeCD;
            }
        }
        else
        {
            _rangedEnemy.firstShot -= Time.deltaTime;
            if (_rangedEnemy.firstShot < 0)
            {
                _rangedEnemy.Attack(_rangedEnemy._target);
                _rangedEnemy.firstShot = _rangedEnemy.reloadTime;
            }
        }

        /*if (_attackCooldown <= 0f)
         * {
         * // Debug.Log("ATTACK!");
         *  _rangedEnemy.Attack(_rangedEnemy._target);
         *  _attackCooldown = _rangedEnemy.reloadTime;
         * }*/


        return(null);
    }
Ejemplo n.º 4
0
    public override Type Tick()
    {
        _rangedEnemy.animator.SetTrigger("Walk");
        int layerMask = 1 << 13;
        //vihollinen ei seuraa pelaajaa alas tasanteilta
        RaycastHit2D hit = Physics2D.Linecast(_rangedEnemy.transform.position, _rangedEnemy.edgeCheck.transform.position, layerMask);

        if (hit)
        {
            Debug.Log(hit.transform.name);
        }
        if (!hit)
        {
            //  _rangedEnemy.eyes.SetActive(false);
            Debug.Log("reunalla");
            if (_rangedEnemy.patrolling)
            {
                return(typeof(PatrolState));
            }
            else
            {
                return(typeof(WatchState));
            }
        }



        if (!_rangedEnemy._target || _rangedEnemy.CheckObstacles(_rangedEnemy._target))
        {
            // Make enemy to wait for few seconds before going back to patrol - Wait Function doesnt work yet.
            _rangedEnemy.StartCoroutine("Wait");

            Debug.Log("Target lost. Going to Patrol");
            if (_rangedEnemy.patrolling)
            {
                return(typeof(PatrolState));
            }
            else
            {
                return(typeof(WatchState));
            }
        }

        /* if (!_rangedEnemy._target || _rangedEnemy.CheckObstacles())
         *
         * {
         *  return typeof(PatrolState);
         * }
         */
        if (_rangedEnemy._target.transform.position.x < _rangedEnemy.transform.position.x)
        {
            _rangedEnemy.transform.rotation = Quaternion.Euler(0, 180, 0);
        }
        else
        {
            _rangedEnemy.transform.rotation = Quaternion.Euler(0, 0, 0);
        }


        // Vector3 objectPos = _rangedEnemy.eyes.transform.position;
        if (_rangedEnemy._target != null)
        {
            Vector3    vectorToTarget = _rangedEnemy._target.transform.position - _rangedEnemy.eyes.transform.position;
            float      angle          = Mathf.Atan2(vectorToTarget.y, vectorToTarget.x) * Mathf.Rad2Deg;
            Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
            _rangedEnemy.eyes.transform.rotation = Quaternion.Slerp(_rangedEnemy.eyes.transform.rotation, q, Time.deltaTime * 100);
        }

        if (_rangedEnemy == null)
        {
            Debug.Log("We didnt have a target");
            if (_rangedEnemy.patrolling)
            {
                return(typeof(PatrolState));
            }
            else
            {
                return(typeof(WatchState));
            }
        }

        if ((_rangedEnemy.transform.position.x - _rangedEnemy._target.transform.position.x) < 0)
        {
            direction = Vector2.right;
        }
        //tämä ei toimi jos pelaaja on vihollisen vasemmalla puolella( vihollinen lähtee pakittamaan).
        //else
        // direction = Vector2.right * -1;

        _rangedEnemy.transform.position = Vector3.MoveTowards(_rangedEnemy.transform.position, new Vector3(_rangedEnemy._target.transform.position.x, _rangedEnemy.transform.position.y, _rangedEnemy.transform.position.z), _rangedEnemy.chaseSpeed * Time.deltaTime);

        //  _rangedEnemy.transform.Translate(direction * chaseSpeed * Time.deltaTime);

        //jos vihollinen ei ole melee, siirtyy suoraan attackstateen
        if (!_rangedEnemy.melee)
        {
            return(typeof(AttackState));
        }
        else
        {
            float distance = Vector2.Distance(transform.position, _rangedEnemy._target.transform.position);


            if (distance < _rangedEnemy.meleeAttackRange)
            {
                return(typeof(AttackState));
            }
        }



        return(null);
    }