Parte 3 juego de carreras

Aquí vamos con la tercera parte del juego de carreras que estamos creando

Ahora vamos a Player no a CAR y vamos a crear un Script

 

vamos a realizar el siguiente script

 

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

public class PlayerCar : MonoBehaviour
{

    public bool carchinge;
    public float tartPost;
    public float speed = 1f;
    // 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;
            }
        }
    }

}




 

ahora vamos a crear otro GameObject

A este GameObject le llamaremos Competidor ahora lo que vamos hacer es buscar a nuestro Competidor.

bien a nuestra imagen le ponemos Sprite (2D and UI) y aplicamos y se lo adjuntamos a «Competidor »

 

Bien ahora vamos añadir el siguiente Script

 

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

public class competidor : MonoBehaviour
{
    public float moveSpeed = 1f;

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

    // Update is called once per frame
    void Update()
    {
        transform.Translate(-transform.up * Time.deltaTime * moveSpeed);
        
    }
}

 

 

AHora lo que vamos hacer es duplicar al competidor

Así que hacemos lo siguiente Control/P y Control/V