Example #1
0
    // Update is called once per frame
    void Update()
    {
        // Check if character just landed on the ground
        if (!m_grounded && m_groundSensor.State())
        {
            m_grounded = true;
            m_animator.SetBool("Grounded", m_grounded);
        }

        // Check if character just started falling
        if (m_grounded && !m_groundSensor.State())
        {
            m_grounded = false;
            m_animator.SetBool("Grounded", m_grounded);
        }

        if (isDead)
        {
            m_animator.SetTrigger("Death");
            if (timeIsDead < 0)
            {
                Destroy(gameObject);
            }
            else
            {
                timeIsDead -= Time.deltaTime;
            }
        }
    }
Example #2
0
    void Update()
    {
        updateBars();

        // Check if player is dead
        if (isDead)
        {
            m_animator.SetTrigger("Death");
            isEnraged = false;
            rage      = 0.0f;
            health    = 0;
            SceneManager.LoadScene(levelToRestart);
            return;
        }

        // Check rage events
        if (isEnraged && rageTime <= 0)
        {
            isDead = true;
        }
        else if (isEnraged)
        {
            rageTime -= Time.deltaTime;
        }

        // Check if character just landed on the ground
        if (!m_grounded && m_groundSensor.State())
        {
            m_grounded = true;
            m_animator.SetBool("Grounded", m_grounded);
        }

        // Check if character just started falling
        if (m_grounded && !m_groundSensor.State())
        {
            m_grounded = false;
            m_animator.SetBool("Grounded", m_grounded);
        }

        // -- Handle input and movement --
        float inputX = Input.GetAxis("Horizontal");

        // Swap direction of sprite depending on walk direction
        if (inputX > 0)
        {
            transform.localScale = new Vector3(-7.0f, 7.0f, 7.0f);
        }
        else if (inputX < 0)
        {
            transform.localScale = new Vector3(7.0f, 7.0f, 7.0f);
        }

        // Move
        m_body2d.velocity = new Vector2(inputX * m_speed, m_body2d.velocity.y);

        // Set AirSpeed in animator
        m_animator.SetFloat("AirSpeed", m_body2d.velocity.y);

        // m_animator.SetTrigger("Recover");

        // Jump
        if (Input.GetKeyDown("space") && m_grounded)
        {
            m_animator.SetTrigger("Jump");
            m_grounded = false;
            m_animator.SetBool("Grounded", m_grounded);
            m_body2d.velocity = new Vector2(m_body2d.velocity.x, m_jumpForce);
            m_groundSensor.Disable(0.2f);
        }

        // Attack
        if (timeBtwAttack <= 0 && Input.GetMouseButtonDown(0))
        {
            m_animator.SetTrigger("Attack");
            Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, whatIsEnemies);
            for (int i = 0; i < enemiesToDamage.Length; i++)
            {
                Enemy hitted = enemiesToDamage[i].GetComponent <Enemy>();
                int   amount = computeDamageAmount();
                hitted.TakeDamage(amount);
                if (!isEnraged)
                {
                    increaseRage(hitted);
                }
                else
                {
                    decreaseRage(hitted);
                }
            }
            timeBtwAttack = startTimeBtwAttack;
        }
        else
        {
            timeBtwAttack -= Time.deltaTime;
        }


        //Change between idle and combat idle
        if (Input.GetKeyDown("f"))
        {
            m_combatIdle = !m_combatIdle;
        }

        // Run
        else if (Mathf.Abs(inputX) > Mathf.Epsilon)
        {
            m_animator.SetInteger("AnimState", 2);
        }



        // Combat Idle
        else if (m_combatIdle)
        {
            m_animator.SetInteger("AnimState", 1);
        }

        // Idle
        else
        {
            m_animator.SetInteger("AnimState", 0);
        }
    }
Example #3
0
    void Update()
    {
        // Check if character just landed on the ground
        if (!m_grounded && m_groundSensor.State())
        {
            m_grounded = true;
            m_animator.SetBool("Grounded", m_grounded);
        }

        // Check if character just started falling
        if (m_grounded && !m_groundSensor.State())
        {
            m_grounded = false;
            m_animator.SetBool("Grounded", m_grounded);
        }

        // -- Handle input and movement --
        float inputX = Input.GetAxis("Horizontal");

        // Swap direction of sprite depending on walk direction
        if (inputX > 0)
        {
            transform.localScale = new Vector3(-1.0f, 1.0f, 1.0f);
        }
        else if (inputX < 0)
        {
            transform.localScale = new Vector3(1.0f, 1.0f, 1.0f);
        }

        // Move
        m_body2d.velocity = new Vector2(inputX * m_speed, m_body2d.velocity.y);

        // Set AirSpeed in animator
        m_animator.SetFloat("AirSpeed", m_body2d.velocity.y);

        // m_animator.SetTrigger("Recover");

        // Attack
        if (timeBtwAttack <= 0 && Input.GetMouseButtonDown(0))
        {
            Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, whatIsEnemies);
            for (int i = 0; i < enemiesToDamage.Length; i++)
            {
                enemiesToDamage[i].GetComponent <Enemy>().TakeDamage(damage);
                if (rage < maxRage)
                {
                    if (enemiesToDamage[i].GetComponent <Enemy>().getIsDead())
                    {
                        rage += highRageIncValue;
                    }
                    else
                    {
                        rage += normalRageIncValue;
                    }
                    rageBar.fillAmount = ((rage * 100) / maxRage) / 100;
                }
            }
            timeBtwAttack = startTimeBtwAttack;
            m_animator.SetTrigger("Attack");
        }
        else
        {
            timeBtwAttack -= Time.deltaTime;
        }

        if (isDead)
        {
            m_animator.SetTrigger("Death");
        }

        //Change between idle and combat idle
        else if (Input.GetKeyDown("f"))
        {
            m_combatIdle = !m_combatIdle;
        }

        // Run
        else if (Mathf.Abs(inputX) > Mathf.Epsilon)
        {
            m_animator.SetInteger("AnimState", 2);
        }

        // Jump
        else if (Input.GetKeyDown("space") && m_grounded)
        {
            m_animator.SetTrigger("Jump");
            m_grounded = false;
            m_animator.SetBool("Grounded", m_grounded);
            m_body2d.velocity = new Vector2(m_body2d.velocity.x, m_jumpForce);
            m_groundSensor.Disable(0.2f);
        }

        // Combat Idle
        else if (m_combatIdle)
        {
            m_animator.SetInteger("AnimState", 1);
        }

        // Idle
        else
        {
            m_animator.SetInteger("AnimState", 0);
        }
    }