Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using System.IO.Compression;
- using Unity.VisualScripting;
- using UnityEngine;
- public class Player2D : MonoBehaviour
- {
- public GameObject laser, puntoSpawnIzq, puntoSpawnDer, explosion;
- protected GameObject explosionClon;
- private GameObject laserIzq, laserDer;
- public float velProyectil = 10.0f, velPlayer = 5.0f;
- private float x, y;
- private bool bloqIzq, bloqSup, bloqDer, bloqInf = false;
- public VariableJoystick vj;
- // Update is called once per frame
- void Update()
- {
- x = vj.Horizontal * Time.deltaTime * velPlayer;
- y = vj.Vertical * Time.deltaTime * velPlayer;
- if (x > 0.0f)
- {
- if (!bloqDer)
- {
- this.gameObject.transform.Translate(x, y, 0.0f);
- }
- }
- if (x < 0.0f)
- {
- if (!bloqIzq)
- {
- this.gameObject.transform.Translate(x, y, 0.0f);
- }
- }
- if (y > 0.0f)
- {
- if (!bloqSup)
- {
- this.gameObject.transform.Translate(x, y, 0.0f);
- }
- }
- if (y < 0.0f)
- {
- if (!bloqInf)
- {
- this.gameObject.transform.Translate(x, y, 0.0f);
- }
- }
- // Cuando se pulsa el clic derecho del ratón, disparar los láseres
- if (Input.GetMouseButtonDown(1))
- {
- // Instanciar un nuevo objeto láser
- laserIzq = (GameObject)Instantiate(laser, puntoSpawnIzq.transform.position, Quaternion.identity);
- laserDer = (GameObject)Instantiate(laser, puntoSpawnDer.transform.position, Quaternion.identity);
- // Proporcionar la velocidad establecida al láser
- laserIzq.GetComponent<Rigidbody2D>().velocity = new Vector2(0.0f, velProyectil);
- laserDer.GetComponent<Rigidbody2D>().velocity = new Vector2(0.0f, velProyectil);
- // Destruir el láser después de 2s. Así no se acumulan si no impacta a ningún asteroide
- Destroy(laserIzq, 2.0f);
- Destroy(laserDer, 2.0f);
- }
- }
- void OnTriggerEnter2D(Collider2D other)
- {
- if (other.gameObject.CompareTag("BordeIzq"))
- {
- bloqIzq = true;
- }
- else if (other.gameObject.CompareTag("BordeSup"))
- {
- bloqSup = true;
- }
- else if (other.gameObject.CompareTag("BordeDer"))
- {
- bloqDer = true;
- }
- else if (other.gameObject.CompareTag("BordeInf"))
- {
- bloqInf = true;
- }
- }
- void OnTriggerExit2D(Collider2D other)
- {
- if (other.gameObject.CompareTag("BordeIzq"))
- {
- bloqIzq = false;
- }
- else if (other.gameObject.CompareTag("BordeSup"))
- {
- bloqSup = false;
- }
- else if (other.gameObject.CompareTag("BordeDer"))
- {
- bloqDer = false;
- }
- else if (other.gameObject.CompareTag("BordeInf"))
- {
- bloqInf = false;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement