Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Critical damage to Ammo.

Discussion in 'Scripting' started by GeovaneUnity54, Jun 13, 2022.

  1. GeovaneUnity54

    GeovaneUnity54

    Joined:
    Nov 14, 2021
    Posts:
    91
    Good morning guys, so I'm trying to make the player's ammo do critical damage.

    So I made this code:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Munição : MonoBehaviour
    6. {
    7.     [SerializeField] float Speed;
    8.    
    9.     public int Dano = 1;
    10.  
    11.     public ParticleSystem Explosao_Tiro1;
    12.  
    13.     public float danoCritico;
    14.    
    15.     void Update()
    16.     {
    17.         transform.Translate(Vector3.up * Time.deltaTime * Speed);
    18.     }
    19.  
    20.     void OnCollisionEnter2D(Collision2D collision)
    21.     {
    22.         if(collision.gameObject.CompareTag("Inimigo"))
    23.         {  
    24.             ChamarRandom();
    25.             Destroy(gameObject);
    26.             Instantiate(Explosao_Tiro1, transform.position, Quaternion.identity);
    27.         }
    28.  
    29.         if(collision.gameObject.CompareTag("Parede"))
    30.         {
    31.             Destroy(gameObject);
    32.             Instantiate(Explosao_Tiro1, transform.position, Quaternion.identity);
    33.         }
    34.  
    35.         if(collision.gameObject.CompareTag("MuniçaoInimigo"))
    36.         {
    37.             Destroy(gameObject);
    38.             Instantiate(Explosao_Tiro1, transform.position, Quaternion.identity);
    39.         }
    40.     }
    41.  
    42.     void ChamarRandom()
    43.     {
    44.         float randDroop = Random.Range (1, 100);
    45.  
    46.         if (randDroop <= danoCritico)
    47.         {
    48.             StartCoroutine("Critico");
    49.         }
    50.     }
    51.  
    52.     IEnumerator Critico()
    53.     {
    54.         Dano *= 2;
    55.         yield return new WaitForSeconds(5f);
    56.         Dano = 1;
    57.     }
    58. }
    But it didn't work, does anyone have any idea why it didn't work?

    Thank you very much in advance :)
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    This is a very common thing we see on the forums but saying "it didn't work" does not describe a problem. Please put some effort into describing what you want to happen in which part of the code and what actually happens.

    Note: Please consider NOT using bold text in all your posts.
     
  3. romulocatao

    romulocatao

    Joined:
    Feb 21, 2021
    Posts:
    1
    put your critical damage as a probability, like
    Code (CSharp):
    1. float criticalChance = 30f;
    2.  
    3. //cal it at random probability
    4. if (Random.Range(0,100f) < criticalChance)
    5. {
    6. //critical hit here
    7. }