Radar control de velocidad Unity2D

Vamos hacer un radar que controle la velocidad de nuestro coche, pero lo primero que vamos hacer es el coche es un movimiento que ya he puesto en videos anterior post , pero lo vamos a volver hacer

Aquí tenemos nuestro coche. con un Box collider2d el Is Trigger activado, el rigbody2d con gravedad 0 y el Script que ahora os pondré en la descripción

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

public class movement : MonoBehaviour
{

    public float velocidad;
    public float rotacion;
    private Rigidbody2D rb;



    void Start()
    {
        rb = GetComponent<Rigidbody2D>();

    }

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

    {


        float h = -Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");

        Vector2 speed = transform.up * (v * velocidad);

        rb.AddForce(speed);

        float direction = Vector2.Dot(rb.velocity, rb.GetRelativeVector(Vector2.up));
        if (direction >= 0.0f)
        {
            rb.rotation += h * rotacion * (rb.velocity.magnitude / 5.0f);

        }
        else
        {
            rb.rotation -= h * rotacion * (rb.velocity.magnitude / 5.0f);

        }

        Vector2 forward = new Vector2(0.0f, 0.5f);
        float steeringRightAngle;
        if (rb.angularVelocity > 0)
        {
            steeringRightAngle = -90;
        }
        else
        {
            steeringRightAngle = 90;
        }

        Vector2 rightAngleFromForward = Quaternion.AngleAxis(steeringRightAngle, Vector3.forward) * forward;
        Debug.DrawLine((Vector3)rb.position, (Vector3)rb.GetRelativePoint(rightAngleFromForward), Color.blue);

        float driftForce = Vector2.Dot(rb.velocity, rb.GetRelativeVector(rightAngleFromForward.normalized));

        Vector2 relativeForce = (rightAngleFromForward.normalized * -1.0f) * (driftForce * 10.0f);


        Debug.DrawLine((Vector3)rb.position, (Vector3)rb.GetRelativePoint(relativeForce), Color.black);

        rb.AddForce(rb.GetRelativeVector(relativeForce));

    }
}



Vamos a crear un Canvas imagen y le insertamos el velocímetro

Creamos un Text que será el que mira la velocidad de nuestro coche. y Ahora vamos a la imagen del velocímetro  y la creamos un Script que será el siguiente

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

public class Velocimetero : MonoBehaviour
{
    public Rigidbody2D Personaje;

    [Header("UI")]
    public Text speedvelociti;
    private float speed = 0.0f;
 

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

        speed = Personaje.velocity.magnitude * 15.6f;
        if (speedvelociti != null)
            speedvelociti.text = ((int)speed) + "km/h";

    }
}

primero pondríamos el coche y después el Text que hemos creado

Creamos un objeto o una imagen que no será mas que un decorativo, y un objeto pequeño que será el que mida la velocidad
Le creamos un box Collider 2D le marcamos el Is trigger

Script del Radar

el radar el pequeño al que le hemos creado un box collider 2D con el Is trigger marcado ahora le vamos a crear un Script que será el siguiente
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class fami : MonoBehaviour
{
    public Rigidbody2D target;
    private float speed = 0.0f;
    [Header("UI")]
    public Text speeddLabel;
    void OnTriggerEnter2D(Collider2D other)
    {

        if (other.CompareTag("Player"))
        {
            speed = target.velocity.magnitude * 15.6f;

            if (speed > 31)
            {
                Debug.Log("Radar");
                speeddLabel.text = "31";
                speeddLabel.text = ((int)speed) + "km/h";
            }
        }
    }
}

El tag de Player lo debe tener nuestro coche, que también tiene que tener el Is trigger activado, ahora deberíamos crear un texto colocarlo donde el radar grande, y incorporar el coche al radar y ya estaría todo

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *