Advertisement
VictorMunoz

Player2D.cs

Apr 21st, 2024 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.16 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.IO.Compression;
  4. using Unity.VisualScripting;
  5. using UnityEngine;
  6.  
  7. public class Player2D : MonoBehaviour
  8. {
  9.     public GameObject laser, puntoSpawnIzq, puntoSpawnDer, explosion;
  10.     protected GameObject explosionClon;
  11.     private GameObject laserIzq, laserDer;
  12.     public float velProyectil = 10.0f, velPlayer = 5.0f;
  13.     private float x, y;
  14.     private bool bloqIzq, bloqSup, bloqDer, bloqInf = false;
  15.     public VariableJoystick vj;
  16.  
  17.  
  18.  
  19.     // Update is called once per frame
  20.     void Update()
  21.     {
  22.         x = vj.Horizontal * Time.deltaTime * velPlayer;
  23.         y = vj.Vertical * Time.deltaTime * velPlayer;
  24.  
  25.         if (x > 0.0f)
  26.         {
  27.             if (!bloqDer)
  28.             {
  29.                 this.gameObject.transform.Translate(x, y, 0.0f);
  30.             }
  31.         }
  32.  
  33.         if (x < 0.0f)
  34.         {
  35.             if (!bloqIzq)
  36.             {
  37.                 this.gameObject.transform.Translate(x, y, 0.0f);
  38.             }
  39.         }
  40.  
  41.         if (y > 0.0f)
  42.         {
  43.             if (!bloqSup)
  44.             {
  45.                 this.gameObject.transform.Translate(x, y, 0.0f);
  46.             }
  47.         }
  48.  
  49.         if (y < 0.0f)
  50.         {
  51.             if (!bloqInf)
  52.             {
  53.                 this.gameObject.transform.Translate(x, y, 0.0f);
  54.             }
  55.         }
  56.  
  57.         // Cuando se pulsa el clic derecho del ratón, disparar los láseres
  58.         if (Input.GetMouseButtonDown(1))
  59.         {
  60.             // Instanciar un nuevo objeto láser
  61.             laserIzq = (GameObject)Instantiate(laser, puntoSpawnIzq.transform.position, Quaternion.identity);
  62.             laserDer = (GameObject)Instantiate(laser, puntoSpawnDer.transform.position, Quaternion.identity);
  63.             // Proporcionar la velocidad establecida al láser
  64.             laserIzq.GetComponent<Rigidbody2D>().velocity = new Vector2(0.0f, velProyectil);
  65.             laserDer.GetComponent<Rigidbody2D>().velocity = new Vector2(0.0f, velProyectil);
  66.             // Destruir el láser después de 2s. Así no se acumulan si no impacta a ningún asteroide
  67.             Destroy(laserIzq, 2.0f);
  68.             Destroy(laserDer, 2.0f);
  69.         }
  70.     }
  71.  
  72.     void OnTriggerEnter2D(Collider2D other)
  73.     {
  74.         if (other.gameObject.CompareTag("BordeIzq"))
  75.         {
  76.             bloqIzq = true;
  77.         }
  78.         else if (other.gameObject.CompareTag("BordeSup"))
  79.         {
  80.             bloqSup = true;
  81.         }
  82.         else if (other.gameObject.CompareTag("BordeDer"))
  83.         {
  84.             bloqDer = true;
  85.         }
  86.         else if (other.gameObject.CompareTag("BordeInf"))
  87.         {
  88.             bloqInf = true;
  89.         }
  90.     }
  91.  
  92.     void OnTriggerExit2D(Collider2D other)
  93.     {
  94.         if (other.gameObject.CompareTag("BordeIzq"))
  95.         {
  96.             bloqIzq = false;
  97.         }
  98.         else if (other.gameObject.CompareTag("BordeSup"))
  99.         {
  100.             bloqSup = false;
  101.         }
  102.         else if (other.gameObject.CompareTag("BordeDer"))
  103.         {
  104.             bloqDer = false;
  105.         }
  106.         else if (other.gameObject.CompareTag("BordeInf"))
  107.         {
  108.             bloqInf = false;
  109.         }
  110.     }
  111. }
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement