Search Unity

Need help whit me script pls

Discussion in 'Scripting' started by Nixel2013, Sep 23, 2019.

  1. Nixel2013

    Nixel2013

    Joined:
    May 9, 2019
    Posts:
    143
    I would like to make an object add X amount of life to my character, but I don't understand how to do it. I am a newbie on the subject of development and programming


    Code (CSharp):
    1. public class VidaExtra : MonoBehaviour
    2. {
    3.     public int CantidadDeVida;
    4.  
    5.     Health Vida;
    6.  
    7.  
    8.     private void Awake()
    9.     {
    10.  
    11.         Vida = GetComponent<Health>();
    12.  
    13.     }
    14.  
    15.     void OnTriggerEnter(Collider other)
    16.     {
    17.         if (other.CompareTag("Player"))
    18.         {
    19.             Vida.currenthealth =++ CantidadDeVida;
    20.  
    21.         }
    22.  
    23.     }
    24. }
    Code (CSharp):
    1. public class Health : MonoBehaviour
    2. {
    3.     public int startinghealth = 100;
    4.     public int currenthealth;
    5.     public Slider healthSlider;
    6.     public Image damageImage;
    7.     public float flashspeed = 5f;
    8.     public Color flashcolor = new Color(1f, 0f, 0f, 0.1f);
    9.  
    10.     public GameObject PersonajeVivo;
    11.     public GameObject PersonajeMuerto;
    12.     public Animator AnimaiconMuerte;
    13.     public GameObject explosion;
    14.     public GameObject PantallaEnNegro;
    15.  
    16.     PlayerController player;
    17.  
    18.     PlayerShooting PlayerShooting;
    19.     bool isDead;
    20.     bool damage;
    21.  
    22.     public float TiempoEntreVida = 0.15f;
    23.  
    24.     /* public float TiempoStart = 0;
    25.      public float TiempoEnd = 0;
    26.      public string Escena;
    27.      */
    28.     private void Awake()
    29.     {
    30.         currenthealth = startinghealth;
    31.         PlayerShooting = GetComponent<PlayerShooting>();
    32.         player = GetComponent<PlayerController>();
    33.         StartCoroutine(ReloadAmmo());
    34.  
    35.     }
    36.  
    37.     IEnumerator ReloadAmmo()
    38.     {
    39.         float reloadTime = TiempoEntreVida * 7;
    40.         while (true)
    41.         {
    42.             yield return new WaitForSeconds(reloadTime);
    43.             if (currenthealth < startinghealth)
    44.             {
    45.                 currenthealth++;
    46.                 healthSlider.value = currenthealth;
    47.             }
    48.         }
    49.  
    50.     }
    51.  
    52.     void Start()
    53.     {
    54.         if (damage)
    55.         {
    56.             damageImage.color = flashcolor;
    57.         }
    58.         else
    59.         {
    60.             damageImage.color = Color.Lerp(damageImage.color, Color.clear, flashspeed * Time.deltaTime);
    61.         }
    62.         damage = false;
    63.  
    64.  
    65.  
    66.         PersonajeVivo = GameObject.Find("Personaje 1");
    67.         PersonajeMuerto = GameObject.Find("PersonajeAnimacionMuerte");
    68.         PersonajeMuerto.SetActive(false);
    69.         AnimaiconMuerte.enabled = false;
    70.  
    71.         PantallaEnNegro = GameObject.Find("Image");
    72.         PantallaEnNegro.SetActive(false);
    73.  
    74.     }
    75.  
    76.     void Update()
    77.     {
    78.        
    79.     }
    80.  
    81.  
    82.     public void TakeDamage(int amount)
    83.     {
    84.  
    85.         damage = true;
    86.  
    87.         currenthealth -= amount;
    88.  
    89.         healthSlider.value = currenthealth;
    90.  
    91.  
    92.  
    93.         if (currenthealth <= 0)//&&!isDead)
    94.         {
    95.             PersonajeVivo.SetActive(false);
    96.             PersonajeMuerto.SetActive(true);
    97.             player.enabled = false;
    98.             AnimaiconMuerte.enabled = true;
    99.             Instantiate(explosion, transform.position, transform.rotation);
    100.             PantallaEnNegro.SetActive(true);
    101.             // Destroy(gameObject, 10f);
    102.             // Destroy(gameObject);
    103.  
    104.           /*  TiempoStart += Time.deltaTime;
    105.             if (TiempoStart >= TiempoEnd)
    106.             {
    107.                 SceneManager.LoadSceneAsync(Escena);
    108.             }
    109.             */
    110.  
    111.         }
    112.  
    113.        
    114.  
    115.         // void Death()
    116.         //{
    117.     //isDead = true;
    118.         //PlayerShooting.DisableEffects();
    119.         // PlayerShooting.enabled = false;
    120.  
    121.         // }
    122.  
    123.     }
    124. }
     
  2. My advice is to keep watching AND following tutorials. If you want to learn the method how to implement powerups/objects you can pick up, you can do like this:


    Otherwise on the forums we are eager to help if you put your own effort into it and start to develop it. If you stuck somewhere, you can post what you have done so far and what's your problem. We will help, but we can't make an entire feature for you and I think a forum post is not the best tool to write an entire tutorial just for you how to do an entire feature.
     
    Nixel2013 likes this.