Movimiento simple de nuestro coche

Aquí lo que vamos hacer es un movimiento simple de nuestro coche, durante la carrera

 

Aquí tenemos nuestro coche,  ahora os voy a enseñar los componentes que le tengo asignado,

 

a mi coche le tengo asignado un Box Collider2D un Nav Mesh Agent, y el Script.. ahora os enseñare el Script

 

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


public class Movimientodelcoche : MonoBehaviour
{
    private NavMeshAgent agent;
    [SerializeField]
    private float Velocidad;

    [SerializeField]
    private float Rotaciondelcar;




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

        agent = GetComponent<NavMeshAgent>();

        agent.autoBraking = false;
    }



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

        transform.rotation *= Quaternion.Euler(+90, 0, 0);
        float horizontalInput = Input.GetAxis("Horizontal");

        float verticalInput = Input.GetAxis("Vertical");

        Vector2 movementDirection = new Vector2(horizontalInput, verticalInput);
        float inputMagnitude = Mathf.Clamp01(movementDirection.magnitude);
        movementDirection.Normalize();

        transform.position -= -transform.up * Velocidad * Time.deltaTime;

        if (movementDirection != Vector2.zero)
        {
            Quaternion toRotation = Quaternion.LookRotation(Vector3.forward, movementDirection);
            transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, Rotaciondelcar * Time.deltaTime);
        }


    }
}

este código seria el que utilizaría en mi coche, Pero para hacer todo esto tenemos que tener NavMesh

 

  • Related Posts

    Real Madrid y Barcelona ganan entre polémicas: ¿es esta la Liga que queremos?

    El Clásico se acerca entre polémicas arbitrales: Barcelona y Real Madrid, los dos gigantes de una Liga que parece cosa de dos Se acerca uno de los partidos más esperados…

    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…

    One thought on “Movimiento simple de nuestro coche”

    1. Unquestionably consider that which you stated. Your favorite
      justification seemed to be on the web the easiest thing to consider of.
      I say to you, I definitely get irked at the same time as other folks think about worries that they
      plainly do not realize about. You controlled
      to hit the nail upon the top and also outlined out the entire thing
      without having side-effects , people could take a signal.
      Will probably be again to get more. Thanks

    Deja una respuesta

    You Missed

    Real Madrid y Barcelona ganan entre polémicas: ¿es esta la Liga que queremos?

    • Por admin
    • octubre 20, 2026
    • 0 views
    Real Madrid y Barcelona ganan entre polémicas: ¿es esta la Liga que queremos?

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

    • Por admin
    • octubre 15, 2026
    • 20 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
    • 19 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
    • 54 views
    Criticar a Lamine por su fútbol, sí; cuestionar que sea español, no.