c# - 用 KeyCode 改变速度针
问题描述
我有一个脚本,它以 0km/h 开头显示一个速度计。现在我想借助“UpArrow”等键码来提高速度,并用“DownArrow”来降低速度。谢谢你的帮助!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class NewSpeedometer : MonoBehaviour
{
public float maxSpeed = 0.0f; // The maximum speed of the target ** IN KM/H **
public float minSpeedArrowAngle;
public float maxSpeedArrowAngle;
[Header("UI")]
public Text speedLabel; // The label that displays the speed;
public RectTransform arrow; // The arrow in the speedometer
private float speed = 0.0f;
private void Update()
{
if (speedLabel != null)
speedLabel.text = ((int)speed) + " km/h";
if (arrow != null)
arrow.localEulerAngles =
new Vector3(0, 0, Mathf.Lerp(minSpeedArrowAngle, maxSpeedArrowAngle, speed / maxSpeed));
}
}
解决方案
在 Update() 中,您应该放置一个 if(Input.GetKey(keycode.uparrow)) 并在其中设置速度以增加乘以 Time.deltaTime 的值和可以称为加速度的值。
对于减速,您可以做同样的事情,但将值从速度中提取出来。
推荐阅读
- visual-studio - 是否可以在不按暂停和继续的情况下“热重载”ASP.NET Core 应用程序?
- css - 使用 CSS 网格,是否可以在间隙中添加边框线?
- javascript - 如何在javascript中迭代数组(不是元素)的属性?
- reactjs - 试图从表reactjs中的firebase获取数据
- javascript - HTML 表上的“querySelectorAll”未按预期工作
- file - 通过 lambda 下载大文件 - 受配额限制
- java - OSX 上的 sdkman 既不能安装也不能卸载 Java 1.8
- flutter - Flutter 2.0 - Web - 强制桌面模式(因为它们的显示方式不同)
- javascript - 如何在 chrome.tabs.executeScript 中执行函数
- c - 从 ARM (Keil uVIsion) 的 C 代码生成部分并获取开始和结束符号,并防止链接器删除它们