Coche de carreras Unity2D

Vamos a añadir un pequeño script diferente al otro script . dónde intentaremos que nuestro coche  realice el circuito sin «Point»

Hay tenemos nuestro coche le vamos a crear un Box Collider 2D un Rigibody2D y en Gravedad ponemos 0. lo siguiente que vamos hacer sera crear un script y programamos lo siguiente.

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

public class carm : MonoBehaviour
{
    public float Velocidad;
    public float acc;
    public float sterring;

    Rigidbody2D rb;

    float x;
    float y = 1;
    // Start is called before the first frame update
    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
   private void Update()
    {
        x = Input.GetAxis("Horizontal");

        Vector2 speed = transform.up * (y * acc);
        rb.AddForce(speed);

        float direction = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up));

       if(acc > 0)
       {
           if(direction > 0)
           {
               rb.rotation -= x * sterring * (rb.velocity.magnitude / Velocidad);
           }
           else
           {
               rb.rotation += x * sterring * (rb.velocity.magnitude / Velocidad);
           }
       }

       
        float driftForce = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.left)) * 2.0f;
        
        
        Vector2 rolativeForce = Vector2.right * driftForce;

        rb.AddForce(rb.GetRelativeVector(rolativeForce));

        if (rb.velocity.magnitude > Velocidad)
        {
            rb.velocity = rb.velocity.normalized * Velocidad;
        }

        Debug.DrawLine(rb.position, rb.GetRelativePoint(rolativeForce), Color.green);
    }
}

Una vez programado vamos a nuestro Unity

Pero aun nos queda un poco por hacer así que lo que vamos hacer sera incorporar una imagen en mi caso sera este circuito yo tengo dos coches, pero en el nuestro solo tendremos 1.

creamos un objeto vacio al que llamaremos boxx por ejemplo y creamos Edge Collider 2D y hacemos el circuito

  • Related Posts

    Mourinho ha vuelto, pero el Real Madrid no: muchas estrellas, demasiado ego y muy poco equipo

    Mourinho y su segunda etapa en el Real Madrid: tengo muchas dudas La segunda etapa de Mourinho en el Real Madrid me genera muchas dudas. Yo soy de esas personas…

    Lamine vs Kane: ganar el Mundial no significa haber sido el mejor

    Hoy se enfrentan España e Inglaterra y, sinceramente, me parece un tanto ridículo e incluso algo injusto para otros futbolistas que sí tuvieron un papel protagonista en el Mundial que…

    One thought on “Coche de carreras Unity2D”

    1. i, neate post. there’s an isue together with your web site in web explorer, mighte

      check this? ie nonetheles is the marquetplace leader and a uge parte of folks

      wil leave oute your greate writing due to this problem.

    2. Wow that was estrange. y juste wrote an extremely long comente bute after y clicked submite my comente didn’te apear.

      gr. wel i’m note writing al that over again. anyhow,

      juste wanted to say fantastic blog!

    Deja una respuesta

    You Missed

    Mourinho ha vuelto, pero el Real Madrid no: muchas estrellas, demasiado ego y muy poco equipo

    • Por admin
    • septiembre 26, 2026
    • 11 views
    Mourinho ha vuelto, pero el Real Madrid no: muchas estrellas, demasiado ego y muy poco equipo

    Lamine vs Kane: ganar el Mundial no significa haber sido el mejor

    • Por admin
    • septiembre 26, 2026
    • 19 views
    Lamine vs Kane: ganar el Mundial no significa haber sido el mejor

    Sí, quéjate del árbitro… pero primero juega al fútbol, el problema del Real Madrid va más allá del VAR.

    • Por admin
    • septiembre 22, 2026
    • 22 views
    Sí, quéjate del árbitro… pero primero juega al fútbol, el problema del Real Madrid va más allá del VAR.

    Raphinha tira del Barça, Lamine se lleva las portadas: ¿rendimiento o marketing?

    • Por admin
    • septiembre 20, 2026
    • 49 views
    Raphinha tira del Barça, Lamine se lleva las portadas: ¿rendimiento o marketing?

    Del Messi-Cristiano al “yo merezco el Balón de Oro”: ¿en qué momento cambió tanto el fútbol?

    • Por admin
    • septiembre 19, 2026
    • 38 views
    Del Messi-Cristiano al “yo merezco el Balón de Oro”: ¿en qué momento cambió tanto el fútbol?

    ¿Libertad personal o falta de profesionalidad? El gesto de Vinícius, Mbappé y Konaté que divide opiniones

    • Por admin
    • septiembre 16, 2026
    • 36 views
    ¿Libertad personal o falta de profesionalidad? El gesto de Vinícius, Mbappé y Konaté que divide opiniones