public void OnTriggerEnter2D(Collider2D collision) { if (!died) //se não estiver morto, sofre hit { if (collision.gameObject.tag == "arma") //se for colidido por uma arma { if (getHit == false) //se não estiver invulnerável { animator.SetTrigger("hit"); getHit = true; barrasHp.SetActive(true); armaInfo infoArma = collision.gameObject.GetComponent <armaInfo>(); //obtem o script de informação da arma Transform PosicaoArma = collision.GetComponent <Transform>(); //obtem a posição do colisor da arma int dano = Random.Range(infoArma.danoMin, infoArma.danoMax); AplicarDano(dano, infoArma.tipoDano); //GameObject fxDanoTemp = Instantiate(gameControler.fxDano[infoArma.tipoDano], PosicaoArma.position, PosicaoArma.rotation); //Destroy(fxDanoTemp, 1f); //print("Tomei " + infoArma.qtdDano + " de dano do tipo " + gameControler.tipoDano[infoArma.tipoDano]); //solução da aula para knocback, achei desnecessario //GameObject knockTemp = Instantiate(knocBackPrefab,knocBackPosition.position,knocBackPosition.rotation); //Destroy(knockTemp,0.03f); //determina a direção do knocback e para onde o inimigo olha quando sofre dano if (collision.gameObject.transform.position.x < transform.position.x) //verifica de que lado veio o golpe { if (transform.localScale.x > 0) //verifica se o inimigo já está virado para o personagem que desferiu o golpe { float x = transform.localScale.x; x *= -1; transform.localScale = new Vector3(x, transform.localScale.y, transform.localScale.z); barrasHp.transform.localScale = new Vector3(x, barrasHp.transform.localScale.y, barrasHp.transform.localScale.z); } enemyRB.AddForce(new Vector2(50, 50)); } else { if (transform.localScale.x < 0) { float x = transform.localScale.x; x *= -1; transform.localScale = new Vector3(x, transform.localScale.y, transform.localScale.z); barrasHp.transform.localScale = new Vector3(x, barrasHp.transform.localScale.y, barrasHp.transform.localScale.z); } enemyRB.AddForce(new Vector2(-50, 50)); } StartCoroutine(Invulneravel()); } } } }
private void OnTriggerEnter2D(Collider2D collision) { armaInfo armaInfo = collision.gameObject.GetComponent <armaInfo>(); if (isDead) { return; } switch (collision.gameObject.tag) { case "arma": if (!isHit) { isHit = true; barrasVida.SetActive(true); animator.SetTrigger("hit"); float danoArma = Random.Range(armaInfo.minDamage, armaInfo.maxDamage); int tipoDano = armaInfo.typeDamage; float danoTomado = danoArma + (danoArma * (ajusteDeDano[tipoDano]) / 100); vidaAtualInimigo -= Mathf.RoundToInt(danoTomado); percentVida = (float)vidaAtualInimigo / (float)vidaInimigo; if (percentVida < 0) { percentVida = 0; } hpBar.localScale = new Vector3(percentVida, 1, 1); if (vidaAtualInimigo <= 0) { isDead = true; animator.SetInteger("idAnimation", 3); StartCoroutine("Loot"); } GameObject danoTemp = Instantiate(danoTXTPrefab, transform.position, transform.localRotation); danoTemp.GetComponent <TextMesh>().text = Mathf.RoundToInt(danoTomado).ToString(); danoTemp.GetComponent <MeshRenderer>().sortingLayerName = "HUD"; GameObject fxTemp = Instantiate(_GameManager.fxDanoPrefab[tipoDano], transform.position, transform.localRotation); Destroy(fxTemp, 1f); int tempForcaX = 50; if (!playerEsquerda) { tempForcaX *= -1; } danoTemp.GetComponent <Rigidbody2D>().AddForce(new Vector2(tempForcaX, 250)); Destroy(danoTemp, 1f); GameObject knockTemp = Instantiate(knockForcePrefab, knockPosition.position, knockPosition.localRotation); Destroy(knockTemp, 0.03f); StartCoroutine("Invuneravel"); } break; } }
private void OnTriggerEnter2D(Collider2D collision) { if (died == true) { return; } switch (collision.gameObject.tag) { case "Arma": if (getHit == false) { getHit = true; barrasVida.SetActive(true); armaInfo infoArma = collision.gameObject.GetComponent <armaInfo>(); // animacao de hit animator.SetTrigger("hit"); float danoArma = Random.Range(infoArma.danoMin, infoArma.danoMax); int tipoDano = infoArma.tipoDano; //dano tomado float danoTomado = danoArma + (danoArma * (ajusteDano[tipoDano] / 100)); vidaAtual -= Mathf.RoundToInt(danoTomado); //rezus da vida a quantidade de dano tomado percVida = (float)vidaAtual / (float)vidaInimigo; if (percVida < 0) { percVida = 0; } hpBar.localScale = new Vector3(percVida, 1, 1); if (vidaAtual <= 0) { died = true; animator.SetInteger("idAnimation", 3); StartCoroutine("loot"); } print("tomei " + danoTomado + " de dano do tipo " + _GameCtrl.tiposDano[tipoDano]); GameObject danoTemp = Instantiate(danoTXTPrefab, transform.position, transform.localRotation); danoTemp.GetComponentInChildren <TextMeshPro>().text = Mathf.RoundToInt(danoTomado).ToString(); danoTemp.GetComponentInChildren <MeshRenderer>().sortingLayerName = "HUD"; GameObject fxTemp = Instantiate(_GameCtrl.fxDano[tipoDano], transform.position, transform.localRotation); int forcaX = 50; if (playerEsquerda == false) { forcaX *= -1; } danoTemp.GetComponent <Rigidbody2D>().AddForce(new Vector2(forcaX, 230)); Destroy(danoTemp, 1f); GameObject knockTemp = Instantiate(knockforcePrefab, knockPosition.position, knockPosition.localRotation); Destroy(knockTemp, 0.02f); StartCoroutine("invuneravel"); } break; } }