示例#1
0
    public void TakeDamage(int damage)
    {
        Life -= damage;

        StartCoroutine(DamageAnimation());

        playersound.SoundPlay(playersound.clips[0]); //audio

        if (Life <= 0)
        {
            Die();
        }
    }
示例#2
0
    // Update is called once per frame
    void Update()
    {
        Vector3 realVelocity;

        realVelocity.x = Speed * horizontal;
        realVelocity.y = Body.velocity.y;
        realVelocity.z = 0;

        Body.velocity = realVelocity;


        //Move
        if (horizontal < 0)
        {
            MainSprite.flipX     = false;
            attackPoint.position = new Vector3(transform.position.x - 0.50f, transform.position.y, transform.position.z);
        }
        else if (horizontal > 0)
        {
            MainSprite.flipX     = true;
            attackPoint.position = new Vector3(transform.position.x + 0.50f, transform.position.y, transform.position.z);
        }

        MainAnimation.SetFloat("walk speed", Mathf.Abs(horizontal));

        MainAnimation.SetFloat("jump", Body.velocity.y);

        //atack
        if (TimeToAttack <= 0)
        {
            TimeToAttack = FirstTimeToAttack;
            if (Input.GetMouseButton(0))
            {
                playersound.SoundPlay(playersound.clips[0]); //audio
                MainAnimation.SetTrigger("attackmovement");
                MainAnimation.Play("atack 1");
                Debug.Log("attack on");



                Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackPoint.position, AttackRange, DefineEnemy);
                for (int i = 0; i < enemiesToDamage.Length; i++)
                {
                    enemyControler enemy = enemiesToDamage[i].GetComponent <enemyControler>();
                    if (enemy != null)
                    {
                        enemy.TakeDamage(Damage);
                    }
                    else
                    {
                        bossLife boss = enemiesToDamage[i].GetComponent <bossLife>();
                        if (boss != null)
                        {
                            boss.TakeDamage(Damage);
                        }
                    }
                }
            }
        }
        else
        {
            TimeToAttack -= Time.deltaTime;
        }

        //vida
        if (Life > NumberOfHeart)
        {
            Life = NumberOfHeart;
        }

        for (int i = 0; i < hearts.Length; i++)
        {
            if (i < Life)
            {
                hearts[i].sprite = fullHeart;
            }
            else
            {
                hearts[i].sprite = emptyHeart;
            }
            if (i < NumberOfHeart)
            {
                hearts[i].enabled = true;
            }
            else
            {
                hearts[i].enabled = false;
            }
        }
        if (Life <= 0)
        {
            Die();
        }
        //cheats
        if (Input.GetKeyDown(KeyCode.Mouse1))
        {
            Damage += 2;
            swordcheat.gameObject.SetActive(true);
        }
        if (Input.GetKeyDown(KeyCode.LeftControl))
        {
            Life += 2;
            heartcheat.gameObject.SetActive(true);
        }
    }