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

    El Real Madrid remonta a la Roma, pero sigue sin convencer: ¿tiene Vinícius los días contados?

    El Real Madrid remonta a la Roma, pero sigue sin convencer: ¿tiene Vinícius los días contados? Ayer el Real Madrid consiguió una victoria frente a la Roma en un partido…

    El Barça está por encima de Messi, señor Gaspart, pero su historia más gloriosa lleva su nombre

    JOAN GASPART a LEO MESSI Gaspart, Messi y el Barça: el club está por encima de todos, pero su historia no se puede borrar «¿Quién se ha creído? Que vaya…

    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

    El Real Madrid remonta a la Roma, pero sigue sin convencer: ¿tiene Vinícius los días contados?

    • Por admin
    • octubre 15, 2026
    • 19 views
    El Real Madrid remonta a la Roma, pero sigue sin convencer: ¿tiene Vinícius los días contados?

    El Barça está por encima de Messi, señor Gaspart, pero su historia más gloriosa lleva su nombre

    • Por admin
    • octubre 13, 2026
    • 18 views
    El Barça está por encima de Messi, señor Gaspart, pero su historia más gloriosa lleva su nombre

    Mbappé desafía al Bernabéu y Mourinho lo defiende: ¿castigo justo o caza mediática?

    • Por admin
    • octubre 10, 2026
    • 50 views
    Mbappé desafía al Bernabéu y Mourinho lo defiende: ¿castigo justo o caza mediática?
    El último baile del 10

    Criticar a Lamine por su fútbol, sí; cuestionar que sea español, no.

    • Por admin
    • octubre 5, 2026
    • 53 views
    Criticar a Lamine por su fútbol, sí; cuestionar que sea español, no.

    Cristiano y Portugal: una historia demasiado grande para terminar de esta manera

    • Por admin
    • octubre 4, 2026
    • 23 views
    Cristiano y Portugal: una historia demasiado grande para terminar de esta manera