Advertisement
VictorMunoz

Reto UF3

Apr 19th, 2024
667
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.43 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using Unity.VisualScripting;
  4. using UnityEngine;
  5. using UnityEngine.UI; // Librería necesaria para cargar elementos de la interfaz ('text')
  6.  
  7. public class Player2D : MonoBehaviour
  8. {
  9.     // Velocidad de la barra controladora
  10.     public float vel;
  11.     // Eje horizontal de movimiento de la barra
  12.     protected float x_mov;
  13.     // Bloqueo al tocar los limites horizontales
  14.     private bool bloqDer, bloqIzq = false;
  15.     // Número de toques
  16.     private int nToques = 0;
  17.     // Variable para mostrar la puntuación
  18.     public GameObject puntuacion;
  19.  
  20.     // Start is called before the first frame update
  21.     void Start()
  22.     {
  23.         // Definir velocidad inicial
  24.         vel = 8.0f;
  25.         // Definir tiempo inicial
  26.         Time.timeScale = 1.0f;
  27.     }
  28.  
  29.     // Update is called once per frame
  30.     void Update()
  31.     {
  32.         // Obtener el valor de desplazamiento horizontal (entre -1 y 1). Se multiplica por el tiempo y velocidad para reducirlo y ajustarlo
  33.         x_mov = Input.GetAxis("Horizontal") * Time.deltaTime * vel;
  34.  
  35.         // Si se selecciona el movimiento a la derecha (números positivos)
  36.         if (x_mov > 0.0f) {
  37.             // Comprobar que el movimiento a la derecha no esté bloqueado
  38.             if (!bloqDer) {
  39.                 // Mover ESTE objeto (barra controladora)
  40.                 this.gameObject.transform.Translate(x_mov, 0.0f, 0.0f);
  41.             }
  42.         }
  43.         // Si se selecciona el movimiento a la izquierda (números negativos)
  44.         else if (x_mov < 0.0f) {
  45.             // Comprobar que el movimiento a la izquierda no esté bloqueado
  46.             if (!bloqIzq) {
  47.                 // Mover ESTE objeto (barra controladora)
  48.                 this.gameObject.transform.Translate(x_mov, 0.0f, 0.0f);
  49.             }
  50.         }
  51.     }
  52.  
  53.     // Método para manejar la colisión con las barras laterales (other)
  54.     private void OnTriggerEnter2D(Collider2D other) {
  55.         // Si OTHER tiene el tag "LimiteDerecho", activar bloqDer para bloquear el movimiento a la derecha
  56.         if (other.gameObject.CompareTag("LimiteDerecho")) {
  57.             bloqDer = true;
  58.         }
  59.         // Si OTHER tiene el tag "LimiteIzquierdo", activar bloqIzq para bloquear el movimiento a la izquierda
  60.         if (other.gameObject.CompareTag("LimiteIzquierdo")) {
  61.             bloqIzq = true;
  62.         }
  63.     }
  64.    
  65.     // Método para volver a permitir el movimiento cuando la barra no esté colisionando
  66.     private void OnTriggerExit2D(Collider2D other) {
  67.         // Si se ha apartado de "LimiteDerecho", poner bloqDer en false
  68.         if (other.gameObject.CompareTag("LimiteDerecho")) {
  69.             bloqDer = false;
  70.         }
  71.         // Si se ha apartado de "LimiteIzquierdo", poner bloqIzq en false
  72.         if (other.gameObject.CompareTag("LimiteIzquierdo")) {
  73.             bloqIzq = false;
  74.         }
  75.     }
  76.  
  77.     // Método para puntuar los toques con el balón
  78.     private void OnCollisionEnter2D(Collision2D other) {
  79.         // Si toca el objeto con el tag "Balon"
  80.         if (other.gameObject.CompareTag("Balon")) {
  81.             // Incrementar el número de toques
  82.             nToques++;
  83.             // Asignar el número de toques al texto de puntuación
  84.             // Como es un elemento de UI (dentro de un canvas) es necesario añadir la librería 'UnityEngine.UI'
  85.             puntuacion.GetComponent<Text>().text = nToques.ToString();
  86.         }
  87.     }
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement