CARRERAS DE COCHE PARTE 5

Bien vamos a comenzar el siguiente tutórala, en este caso lo que vamos hacer es crear una explosión cuando nuestro coche colissione.

Pero lo primero que vamos haces es arreglar un pequeño problema que nos ha surgido ya que salen solo 2 coches despues da error

Ahora vemos como solo salen 2 o 3 coches

 

 

 

Bien quitamos en el RESET el tag,

También lo desmarcamos. y con eso tendríamos el problema resuelto.

Ahora vamos a nuestro Player que en mi caso es el coche

bien creamos un Box Collider y un Rigibody , el Box collider se lo ponemos al coche, ahora seleccionamos la imágenes de explosión

 

Ahora  nuestras imágenes las vamos a poner a Sprite (2D and UI)

le vamos a poner en Múltiple y Sprite Editor y editamos las imágenes a las que seleccionamos 1 a 1

y aplicamos

Ahora vamos  a crear una animación seleccionamos nuestra explosión  Window – Animation- Animation y ponemos nuestra animación

 

 

Ahora vamos a nuestro Player y programamos lo siguiente

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

public class PlayerCar : MonoBehaviour
{

    public bool carchinge;
    public float tartPost;
    public float speed = 1f;
    public GameObject explosion; ////COPIAMOS Y PEGAMOS


    // Start is called before the first frame update
    void Start()
    {

    }

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

        if (Input.GetKeyDown(KeyCode.LeftArrow))
        {
            carchinge = true;
            tartPost = -4;


        }

        if (Input.GetKeyDown(KeyCode.RightArrow))
        {

            carchinge = true;
            tartPost = 4;

        }
        ChangeCarLane();
    }

    private void ChangeCarLane()
    {
        if (carchinge)
        {
            Vector3 currentPos = transform.position;

            float xPos = Mathf.Lerp(currentPos.x, tartPost, Time.deltaTime * speed);

            this.transform.position = new Vector3(xPos, transform.position.y, transform.position.z);


            if (Mathf.Abs(currentPos.x - tartPost) < 0.05f)
            {
                carchinge = false;
            }
        }

    }
        public void OnTriggerEnter2D(Collider2D col){//////COPIAMOS Y PEGAMOS
             if(col.tag == "ELIMI")
             {

                 Instantiate(explosion,transform.position,Quaternion.identity);
                 Destroy(gameObject);

             }



        }


        

    }

 

bien ahora a la explosión le vamos a crear un Script para que dure solo 2 segundos

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

public class fin : MonoBehaviour
{
 


    // Start is called before the first frame update
    void Start()
    {
        Destroy(gameObject, 2f);
    }

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


       
       
       

        

    }

}

 

 

 

Ahora nuestra explosion la copiamos y la pegamos en la carpeta de Prefab

y eliminados la explosión

copiamos la explosión en None (Game Object) y ya tendríamos la explosión