// Update is called once per frame
    void Update()
    {
        switch (_FriendlyCurrentState)
        {
        case Friendlystate.Idle:
            if (_timeridle < 0)
            {
                //idle
                _FriendlyAnimation.PlayIdle();
                _timeridle = timeridle;
            }
            else
            {
                _timeridle -= Time.deltaTime;
            }
            break;

        case Friendlystate.Walking:
            if (_timerwalking < 0)
            {
                //walking
                _FriendlyAnimation.PlayWalking();
                _timerwalking = timerwalking;
            }
            else
            {
                _timerwalking -= Time.deltaTime;
            }
            break;

        case Friendlystate.Atacking:

            break;

        case Friendlystate.Hit:

            break;

        case Friendlystate.Jumping:

            break;
        }

        targetx = GameObject.Find("Player").transform.position.x;
        x       = transform.position.x;

        if (x >= targetx - 3f || x <= targetx - 4f)        //link
        {
            _FriendlyCurrentState = Friendlystate.Walking;
        }
        else
        {
            _FriendlyCurrentState = Friendlystate.Idle;
        }
    }
Ejemplo n.º 2
0
    // Update is called once per frame
    void Update()
    {
        x = gameObject.transform.position.x;
        y = gameObject.transform.position.y;

        targetx = player.transform.position.x;
        targety = player.transform.position.y;

        switch (_EnemyCurrentState)
        {
        case Enemystate.Idle:
            if (_timeridle < 0)
            {
                //idle
                _EnemyAnimation.PlayIdle();
                _timeridle = timeridle;
            }
            else
            {
                _timeridle -= Time.deltaTime;
            }
            break;

        case Enemystate.Walking:
            if (_timerwalking < 0)
            {
                //walking
                _EnemyAnimation.PlayWalking();
                _timerwalking = timerwalking;
            }
            else
            {
                _timerwalking -= Time.deltaTime;
            }
            break;

        case Enemystate.Atacking:

            break;

        case Enemystate.Hit:

            break;

        case Enemystate.Jumping:
            if (_timerjumping < 0)
            {
                _EnemyAnimation.PlayJump();
                _timerjumping = timerjumping;
            }
            else
            {
                _timerjumping -= Time.deltaTime;
            }
            break;
        }



        if (flip)
        {
            gameObject.transform.rotation = Quaternion.Euler(0, 180, 0);
        }
        else if (!flip)
        {
            gameObject.transform.rotation = Quaternion.Euler(0, 0, 0);
        }

        if (x >= targetx + 0.5f && targetx >= x + lookingfield * -1)        //link
        {
            _EnemyCurrentState = Enemystate.Walking;
            flip = false;
            //print("hallo");
        }
        else if (x <= targetx - 0.5f && targetx <= x + lookingfield)        //rechts
        {
            //print("hallo 2");
            _EnemyCurrentState = Enemystate.Walking;
            flip = true;
        }
        else
        {
            _EnemyCurrentState = Enemystate.Idle;
        }

        if (y < targetx)
        {
            //_EnemyCurrentState = Enemystate.Jumping;
        }



        //print(flip);
    }
    void FixedUpdate()
    {
        if (target == null)
        {
            //TODO: Insert a player search here.
            return;
        }

        //TODO: Always look at player?


        if (path == null)
        {
            return;
        }


        if (currentWaypoint >= path.vectorPath.Count)
        {
            if (pathIsEnded)
            {
                return;
            }

            Debug.Log("End of path reached.");
            enemyAnimation.PlayIdle();
            pathIsEnded = true;
            return;
        }

        pathIsEnded = false;

        //Direction to the next waypoint
        Vector3 dir = (path.vectorPath[currentWaypoint] - transform.position).normalized;

        dir *= speed * Time.fixedDeltaTime;
        if (dir.x > 0)
        {
            isFacingLeft = false;
        }
        if (dir.x < 0)
        {
            isFacingLeft = true;
        }


        //Move the AI
        rb.AddForce(dir, fMode);

        float dist = Vector3.Distance(transform.position, path.vectorPath[currentWaypoint]);

        if (dist < nextWaypointDistance)
        {
            currentWaypoint++;
            return;
        }
        else if (dist == nextWaypointDistance)
        {
            enemyAnimation.PlayWalk(false);
        }

        if (isFacingLeft)
        {
            Debug.Log("Facing Left");
            transform.localScale = new Vector3(2f, 2f, 0);
            enemyAnimation.PlayWalk(true);
        }

        if (!isFacingLeft)
        {
            Debug.Log("Facing Right");
            transform.localScale = new Vector3(-2f, 2f, 0);
            enemyAnimation.PlayWalk(true);
        }
    }