Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using Unity.VisualScripting;
- using UnityEngine;
- using UnityEngine.UI; // Librería necesaria para cargar elementos de la interfaz ('text')
- public class Player2D : MonoBehaviour
- {
- // Velocidad de la barra controladora
- public float vel;
- // Eje horizontal de movimiento de la barra
- protected float x_mov;
- // Bloqueo al tocar los limites horizontales
- private bool bloqDer, bloqIzq = false;
- // Número de toques
- private int nToques = 0;
- // Variable para mostrar la puntuación
- public GameObject puntuacion;
- // Start is called before the first frame update
- void Start()
- {
- // Definir velocidad inicial
- vel = 8.0f;
- // Definir tiempo inicial
- Time.timeScale = 1.0f;
- }
- // Update is called once per frame
- void Update()
- {
- // Obtener el valor de desplazamiento horizontal (entre -1 y 1). Se multiplica por el tiempo y velocidad para reducirlo y ajustarlo
- x_mov = Input.GetAxis("Horizontal") * Time.deltaTime * vel;
- // Si se selecciona el movimiento a la derecha (números positivos)
- if (x_mov > 0.0f) {
- // Comprobar que el movimiento a la derecha no esté bloqueado
- if (!bloqDer) {
- // Mover ESTE objeto (barra controladora)
- this.gameObject.transform.Translate(x_mov, 0.0f, 0.0f);
- }
- }
- // Si se selecciona el movimiento a la izquierda (números negativos)
- else if (x_mov < 0.0f) {
- // Comprobar que el movimiento a la izquierda no esté bloqueado
- if (!bloqIzq) {
- // Mover ESTE objeto (barra controladora)
- this.gameObject.transform.Translate(x_mov, 0.0f, 0.0f);
- }
- }
- }
- // Método para manejar la colisión con las barras laterales (other)
- private void OnTriggerEnter2D(Collider2D other) {
- // Si OTHER tiene el tag "LimiteDerecho", activar bloqDer para bloquear el movimiento a la derecha
- if (other.gameObject.CompareTag("LimiteDerecho")) {
- bloqDer = true;
- }
- // Si OTHER tiene el tag "LimiteIzquierdo", activar bloqIzq para bloquear el movimiento a la izquierda
- if (other.gameObject.CompareTag("LimiteIzquierdo")) {
- bloqIzq = true;
- }
- }
- // Método para volver a permitir el movimiento cuando la barra no esté colisionando
- private void OnTriggerExit2D(Collider2D other) {
- // Si se ha apartado de "LimiteDerecho", poner bloqDer en false
- if (other.gameObject.CompareTag("LimiteDerecho")) {
- bloqDer = false;
- }
- // Si se ha apartado de "LimiteIzquierdo", poner bloqIzq en false
- if (other.gameObject.CompareTag("LimiteIzquierdo")) {
- bloqIzq = false;
- }
- }
- // Método para puntuar los toques con el balón
- private void OnCollisionEnter2D(Collision2D other) {
- // Si toca el objeto con el tag "Balon"
- if (other.gameObject.CompareTag("Balon")) {
- // Incrementar el número de toques
- nToques++;
- // Asignar el número de toques al texto de puntuación
- // Como es un elemento de UI (dentro de un canvas) es necesario añadir la librería 'UnityEngine.UI'
- puntuacion.GetComponent<Text>().text = nToques.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement