Carreras de coche Parte 4

Aquí vamos a hacer la parte 4 lo que vamos a hacer es generar coches y después eliminarlos bien comenzamos

Bien eso son los coches que hemos creado de competidor que fuimos copiando y pegando, y creamos un «GameObject» al que vamos a llamar es «GeneradordeObstaculos» todo junto y creamos un New Script

 

Bien pondremos el Obstcle Cars el Size pondremos el numero y en elemnt0 vamos poniendo los obstáculo, que podremos poner en la carpeta de prefab aquí os dejare el Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GENERADOR : MonoBehaviour
{

    public GameObject obstacleCar;
    public GameObject[] obstacleCars;
    // Start is called before the first frame update
    void Start()
    {
        InvokeRepeating("GENERADORDEOBSATUCULOS", 1f, 5f);
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    private void GENERADORDEOBSATUCULOS()
    {

        int randomNum = Random.Range(0, 2);

        if (randomNum == 0)
        {

            Instantiate(obstacleCars[Random.Range(0, obstacleCars.Length)], new Vector3(-4f, 31f, 180f), Quaternion.identity);
        }
        else
        {
            Instantiate(obstacleCar, new Vector3(4f, 31f, 180f), Quaternion.identity);
        }

    }
}

 

 

 

 

Ahora os voy a enseñar en caso de que no vuestro clones no se vean

Vamos a nuestra Materia y lo cambiamos a transparent,

Bien continuamos  ahora creamos otro GameObject al que llamaremos «RESET»este eliminara al coche

A nuestro RESET le creamos una Box Collider 2D al que le asignamos el Is Trigger  y creamos un script

Así nos quedara y ahora vereis el Script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RESET : MonoBehaviour
{
    
    // Start is called before the first frame update

    public void OnTriggerEnter2D (Collider2D other){

        if (other.tag == "ELIMI"){
            Destroy (other.gameObject);
        }
   
    }
    
}

Bien aquí tendremos nuestro script de reset ahora vamos a nuestro coche en mi caso será un coche de Formula 1 y le creamos un Tag al que llamaremos «ELIMI»

 

Bien aquí creamos el Add Tag.. y sé lo asignamos a RESET y al coche, al coche además de asignarle el Tag le asignamos un Box Collider2D y un Rygiboy2D.