miércoles, 12 de enero de 2022

¿Como funciona un sistema de sumar puntos en Unity?;

¿Como funciona un sistema de sumar puntos en Unity?

En las capturas de pantalla se pueden ver los elementos que intervienen para dicha labor...como si fuera una receta de cocina lo voy a explicar….

Ingredientes….

1-Un script en c#.

2- Un Canvas con dos text.

3- Un collider con el trigger acribado en este caso una esfera que lo tiene el player el hombre lobo y que tendrá el contenido del script en C#.

4- Un Tag con un nombre inventado en este caso le llamamos ”MATA” y se lo aplicamos a los enemigos, al nombre del Tag me refiero y ellos a su vez un cubo por encima de su cabeza también con el trigger activado.

5- A continuación el script y video de referencia.

 


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AUMENTAPUNTOS : 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>

        void Start ()
        {


            // Assign the Rigidbody component to our private rb variable
            rb = GetComponent<Rigidbody>();

            // Set the count to zero 
            count = 0;

            // 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 = "";
        }

        // store a reference to that collider in a variable named 'other'..
        void OnTriggerEnter(Collider other) 
        {

            //if (other.gameObject.CompareTag ("ogri2"))
            if (other.gameObject.CompareTag ("MATA"))

            {
                // Make the other game object (the pick up) inactive, to make it disappear
                other.gameObject.SetActive (false);///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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


                // Run the 'SetCountText()' function (see below)
                SetCountText ();
            }
        }

        // Create a standalone function that can update the 'countText' UI and check if the required amount to win has been achieved
        void SetCountText()
        {
            // Update the text field of our 'countText' variable
            //countText.text = "Count: " + count.ToString ();

            countText.text = "PUNTOS: " + count.ToString ();
            // Check if our 'count' is equal to or exceeded 12
            if (count == 11) 
                //if (count >= 11) /////////original
                //if (count >= 111112) ////////7original
            {



            }
        }
    }


-------------------------------------------------------------------------

cambio de escena por colision y nombrado por tag el gameobject
------------------------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class portoquedetaccambialaescena : MonoBehaviour {


        void Start () {

        }




        //void OnCollisionEnter (){


        void OnCollisionEnter (Collision collision)
        {
            //if (collision.gameObject.tag == "maloazul (4)") {
            //Destroy (gameObject, 0.0f); 


            if (collision.gameObject.tag == "pomada") {
                Destroy (gameObject, 2.0f); 

                //    transform.position = new Vector3 (198.301f, 20.316f, 136.023f);/////////nuebo mio
                //transform.Translate (new Vector3 (198 * Time.deltaTime, 20, 136.2f), Space.Self);/////////////IMBENTADO

                //Application.LoadLevel (1);////ORIGINALLLLLLLLLLLL
                Application.LoadLevel (1);




            }
        }
    }