gpt4 book ai didi

c# - 如何在 C# 中获取随机数以及为什么键盘输入不起作用?

转载 作者:行者123 更新时间:2023-11-30 20:34:59 25 4
gpt4 key购买 nike

我是 Unity 和 C# 的新手。

我正在尝试制作一个将在控制台中执行的简单程序,它会告诉用户在用户单击 SPACE 后等待一段时间,计算机会说出他等待了多少时间。

所以这是一个简单的代码。

为此,我必须生成一个随机数,并且需要一些键盘输入。

但是当我输入空格键时没有任何反应。

没有编译错误。

随机数只生成数字 0。

代码:

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

public class droid : MonoBehaviour {
float startTime;
float playerTime;
float targetTime;

// Use this for initialization
void Start () {
target();
}

// Update is called once per frame
void Update () {
playerTime = Time.time - startTime;

if (Input.GetKeyDown(KeyCode.Space))
{
print("You took " + playerTime);
}
}

void target() {
print("Your Time " + targetTime);
targetTime = Random.Range(0, 10);
startTime = Time.time;
}
}

最佳答案

您在生成值之前打印该值。在您的 target() 中,移动此行:

targetTime = Random.Range(0, 10);

以上

print("Your Time " + targetTime);

关于c# - 如何在 C# 中获取随机数以及为什么键盘输入不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38532382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com