Ahora lo que vamos hacer es que nuestro disparo colisione con nuestro enemigo y explote
Vamos hacer que cuando nuestra bola de fuego toque el otro coche explote
bien nuestra bola de fuego que es esta vamos hacer que explote esto es lo que tenemos actualmente programado de nuestra bola
using System.Collections; using System.Collections.Generic; using UnityEngine; public class disparo : MonoBehaviour { // Start is called before the first frame update Rigidbody2D rb; public float speed; private void Awake() { rb = GetComponent<Rigidbody2D>(); } private void Update() { rb.velocity = -Vector2.down * speed; } } }
vamos añadir lo siguiente
using System.Collections; using System.Collections.Generic; using UnityEngine; public class disparo : MonoBehaviour { // Start is called before the first frame update Rigidbody2D rb; public float speed; public GameObject explosion; private void Awake() { rb = GetComponent<Rigidbody2D>(); } private void Update() { rb.velocity = -Vector2.down * speed; } public void OnTriggerEnter2D(Collider2D col) { if (col.tag == "ELIMI") { Instantiate(explosion, transform.position, Quaternion.identity); Destroy(gameObject); } } }
ahora a nuestra bola de fuego le vamos asignar un Tag «BOLA»
y la volvemos a poner en la carpeta de prefab y la eliminados despues se la adjuntamos a nuestro player
ahora nos vamos a Script del coche competidor que es nuestro enemigo que tiene este escript
using System.Collections; using System.Collections.Generic; using UnityEngine; public class explosionn : MonoBehaviour { // Start is called before the first frame update public void OnTriggerEnter2D(Collider2D col) { if (col.tag == "ELIMI") { Instantiate(explosion, transform.position, Quaternion.identity); Destroy(gameObject); } } }
bien vamos añadir lo siguiente
using System.Collections; using System.Collections.Generic; using UnityEngine; public class explosionn : MonoBehaviour { public GameObject explosion; // Start is called before the first frame update public void OnTriggerEnter2D(Collider2D col) { if (col.tag == "ELIMI") { Instantiate(explosion, transform.position, Quaternion.identity); Destroy(gameObject); } if (col.tag == "BOLA") { Instantiate(explosion, transform.position, Quaternion.identity); Destroy(gameObject); } } }
con esto añadido nuestra bola y el coche explotara cuando colisionen
ahora vamos a crear un
Create Empty
le llamamos a los RESET2 y detrás de nuestros coches lo ponemos para nuestra bola de fuego y en nuestro RESET 2 programamos lo siguiente
using System.Collections; using System.Collections.Generic; using UnityEngine; public class RESET2 : MonoBehaviour { // Start is called before the first frame update void OnTriggerEnter2D(Collider2D other) { if (other.tag == "BOLA") { Destroy(other.gameObject); } } }
con esto eliminaremos nuestra bola de fuego y no se acumularan.