C# Unity汽车游戏在文本上显示每公里速度

C# Unity汽车游戏在文本上显示每公里速度,c#,unity3d,C#,Unity3d,我想在屏幕上显示车速kph,但我只有0kph,它不工作。。虽然我有这个密码。。任何建议都很好,谢谢 代码的任何更新/新编辑都将非常棒。感谢您从评论中,OP显然附加了速度指示器文本GameObject下显示的代码,而不是汽车。这导致了 变换位置 及 起始位置 始终为相同的值,因为文本对象是一个完全不移动的固定UI元素。(世界空间UI对象除外) 您需要了解transform.position返回代码附加到的游戏对象的位置。因此,如果将其附加到文本,它将为您提供文本的位置,而不是汽车的位置 这里的解

我想在屏幕上显示车速kph,但我只有0kph,它不工作。。虽然我有这个密码。。任何建议都很好,谢谢


代码的任何更新/新编辑都将非常棒。感谢您

从评论中,OP显然附加了速度指示器文本GameObject下显示的代码,而不是汽车。这导致了

变换位置

起始位置

始终为相同的值,因为文本对象是一个完全不移动的固定UI元素。(世界空间UI对象除外)

您需要了解transform.position返回代码附加到的游戏对象的位置。因此,如果将其附加到文本,它将为您提供文本的位置,而不是汽车的位置

这里的解决方案是使用刚体将代码附加到汽车上,然后通过inspector将层次结构中的文本游戏对象指定给speed indicator文本组件变量

最后,使用刚体组件,您只需使用

text.text = GetComponent<Rigidbody>().velocity.magnitude;
text.text=GetComponent().velocity.magnitude;

从评论中,OP显然附加了速度指示器文本GameObject下显示的代码,而不是汽车。这导致了

变换位置

起始位置

始终为相同的值,因为文本对象是一个完全不移动的固定UI元素。(世界空间UI对象除外)

您需要了解transform.position返回代码附加到的游戏对象的位置。因此,如果将其附加到文本,它将为您提供文本的位置,而不是汽车的位置

这里的解决方案是使用刚体将代码附加到汽车上,然后通过inspector将层次结构中的文本游戏对象指定给speed indicator文本组件变量

最后,使用刚体组件,您只需使用

text.text = GetComponent<Rigidbody>().velocity.magnitude;
text.text=GetComponent().velocity.magnitude;

我来自未来。只是想让你知道,如果你想让这样的东西开箱即用,你可以使用我的代码:

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

using UnityEngine;
using UnityEngine.UI;

public class realSpeedOMeter : MonoBehaviour
{
    public float currentSpeed;
    public Text speedText;
    Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        currentSpeed = rb.velocity.magnitude * 2.23694f;

        speedText.text = currentSpeed.ToString() + "MPH";
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共级realSpeedOMeter:MonoBehavior
{
公共交通速度;
公共文本;
刚体rb;
void Start()
{
rb=GetComponent();
}
无效更新()
{
currentSpeed=rb.velocity.magnitude*2.23694f;
speedText.text=currentSpeed.ToString()+“MPH”;
}
}

我来自未来。只是想让你知道,如果你想让这样的东西开箱即用,你可以使用我的代码:

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

using UnityEngine;
using UnityEngine.UI;

public class realSpeedOMeter : MonoBehaviour
{
    public float currentSpeed;
    public Text speedText;
    Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        currentSpeed = rb.velocity.magnitude * 2.23694f;

        speedText.text = currentSpeed.ToString() + "MPH";
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共级realSpeedOMeter:MonoBehavior
{
公共交通速度;
公共文本;
刚体rb;
void Start()
{
rb=GetComponent();
}
无效更新()
{
currentSpeed=rb.velocity.magnitude*2.23694f;
speedText.text=currentSpeed.ToString()+“MPH”;
}
}

我目前正在我的项目中使用此代码,它工作得非常完美。记住,我添加了一些选项,使其更加完整

using System;
using UnityEngine;
using UnityEngine.UI;

public enum SpeedUnit
{
    mph,
    kmh
}

public class SpeedOMeter : MonoBehaviour
{
    private float currentSpeed;
    [SerializeField] private Text speedText;
    [SerializeField] private string previousText;
    [SerializeField] private SpeedUnit speedUnit;
    [SerializeField] private int decimalPlaces;
    Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        

        if (speedUnit == SpeedUnit.mph)
        {

            // 2.23694 is the constant to convert a value from m/s to mph.
            currentSpeed = (float)Math.Round(rb.velocity.magnitude * 2.23694f, decimalPlaces);

            //currentSpeed = (float)Math.Round((double)rb.velocity.magnitude * 2.23694f, 0);

            speedText.text = previousText + currentSpeed.ToString() + " mph";

        }

        else 
        {

            // 3.6 is the constant to convert a value from m/s to km/h.
            currentSpeed = (float)Math.Round(rb.velocity.magnitude * 3.6f, decimalPlaces);

            //currentSpeed = (float)Math.Round((double)rb.velocity.magnitude * 3.6f, 0);

            speedText.text = previousText + currentSpeed.ToString() + " km/h";

        }
    }
}
使用系统;
使用UnityEngine;
使用UnityEngine.UI;
公共枚举速度单位
{
每小时英里数,
kmh
}
公共级车速表:单程
{
私人浮动速度;
[SerializeField]专用文本speedText;
[SerializeField]私有字符串previousText;
[序列化字段]专用SpeedUnit SpeedUnit;
[序列化字段]私有整数小数位数;
刚体rb;
void Start()
{
rb=GetComponent();
}
无效更新()
{
如果(速度单位==速度单位.mph)
{
//2.23694是将数值从m/s转换为mph的常数。
当前速度=(浮点)数学四舍五入(rb.velocity.magnitude*2.23694f,小数位数);
//当前速度=(浮点)数学圆((双)rb.velocity.Magnite*2.23694f,0);
speedText.text=previousText+currentSpeed.ToString()+“mph”;
}
其他的
{
//3.6是将值从m/s转换为km/h的常数。
当前速度=(浮点)数学四舍五入(rb.velocity.Magnite*3.6f,小数位数);
//当前速度=(浮点)数学圆((双)rb.velocity.Magnite*3.6f,0);
speedText.text=previousText+currentSpeed.ToString()+“km/h”;
}
}
}

我目前正在我的项目中使用此代码,它工作得非常完美。记住,我添加了一些选项,使其更加完整

using System;
using UnityEngine;
using UnityEngine.UI;

public enum SpeedUnit
{
    mph,
    kmh
}

public class SpeedOMeter : MonoBehaviour
{
    private float currentSpeed;
    [SerializeField] private Text speedText;
    [SerializeField] private string previousText;
    [SerializeField] private SpeedUnit speedUnit;
    [SerializeField] private int decimalPlaces;
    Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        

        if (speedUnit == SpeedUnit.mph)
        {

            // 2.23694 is the constant to convert a value from m/s to mph.
            currentSpeed = (float)Math.Round(rb.velocity.magnitude * 2.23694f, decimalPlaces);

            //currentSpeed = (float)Math.Round((double)rb.velocity.magnitude * 2.23694f, 0);

            speedText.text = previousText + currentSpeed.ToString() + " mph";

        }

        else 
        {

            // 3.6 is the constant to convert a value from m/s to km/h.
            currentSpeed = (float)Math.Round(rb.velocity.magnitude * 3.6f, decimalPlaces);

            //currentSpeed = (float)Math.Round((double)rb.velocity.magnitude * 3.6f, 0);

            speedText.text = previousText + currentSpeed.ToString() + " km/h";

        }
    }
}
使用系统;
使用UnityEngine;
使用UnityEngine.UI;
公共枚举速度单位
{
每小时英里数,
kmh
}
公共级车速表:单程
{
私人浮动速度;
[SerializeField]专用文本speedText;
[SerializeField]私有字符串previousText;
[序列化字段]专用SpeedUnit SpeedUnit;
[序列化字段]私有整数小数位数;
刚体rb;
void Start()
{
rb=GetComponent();
}
无效更新()
{
如果(速度单位==速度单位.mph)
{
//2.23694是将数值从m/s转换为mph的常数。
当前速度=(浮点)数学四舍五入(rb.velocity.magnitude*2.23694f,小数位数);
//当前速度=(浮点)数学圆((双)rb.velocity.Magnite*2.23694f,0);
speedText.text=previousText+currentSpeed.ToString()+“mph”;
}
其他的
{
//3.6是将值从m/s转换为km/h的常数。
当前速度=(浮点)数学四舍五入(rb.velocity.Magnite*3.6f,小数位数);
//当前速度=(浮点)数学圆((双)rb.velocity.Magnite*3.6f,0);