lunes, 22 de febrero de 2021

Crear un contador de vidas y un sonido de ir perdiendo vidas

 
LOGRE PONER UN CONTADOR DE VIDAS AL DARME EL ENEMIGO A MI DOY UN GRITO DE DOLOR Y PIERDO 1 VIDA, AL PERDER 10 VIDAS SE REINICIA EL JUEGO AL PRINCIPIO.

  if (other.gameObject.CompareTag ("pomada")){

 "pomada" es el proyectil del enemigo cuando me impacta su tag se llama
 "pomada"

lo que provoca que se me resten puntos de vida y que tambien utilizo para el script de provocar el sonido.

 

 

 

 

  using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class restavida : MonoBehaviour {


        // Create public variables for player speed, and for the Text UI game objects
        public float speed;
        public Text countText;
        public Text winText;

        // Create private references to the rigidbody component on the player, and the count of pick up objects picked up so far
        private Rigidbody rb;
        private int count;/// <summary>



        // At the start of the game..
        void Start ()
        {

            rb = GetComponent<Rigidbody>();

            // Set the count to zero 
            //count = 0;////original
        count = 10;
            // Run the SetCountText function to update the UI (see below)
            SetCountText ();

            // Set the text property of our Win Text UI to an empty string, making the 'You Win' (game over message) blank
            winText.text = "";
        }


        void OnTriggerEnter(Collider other) 
        {


        if (other.gameObject.CompareTag ("pomada")){

                other.gameObject.SetActive (false);///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

                // Add one to the score variable 'count'
            //count = count + 1;//////original
            count = count - 1;

                SetCountText ();
            }
        }

        // Create a standalone function that can update the 'countText' UI and check if the required amount to win has been achieved
        void SetCountText()
        {


        // countText.text = "VIDAS: " + count.ToString ();///original
    countText.text = "VIDAS: " + count.ToString ();
            // Check if our 'count' is equal to or exceeded 12
        //if (count >= 10) ////original
        if (count <= 1) 

                //if (count >= 99) 
            {
                // Set the text value of our 'winText'
                winText.text = "TE HAN MATADO";

                //Destroy (gameObject, 1.2f); 
            Application.LoadLevel (1);


            }
        }
    }









using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SUENAALPERDERUNAVIDA : MonoBehaviour {

        public AudioSource tickSource;

        void Start () {

            tickSource = GetComponent<AudioSource> ();



        }

        void Update () {
        }



        void OnTriggerEnter(Collider other) {
        if (other.gameObject.CompareTag ("pomada")){


                tickSource.Play ();



            }}

    }