Random.Range 不能使用整数,为什么?
Random.Range not working with integer, why?
所以我正在做这个开锁项目,它真的很简单,但我遇到了一些问题 Random.Range 当我按下按键时它不会改变我设置了。我的场景非常简单,我有一扇门,里面有脚本,还有 Unity 的第一人称控制器。
我希望 Random.Range 在我按下 1 到 9 键时更改值然后打印两者,如果它们等于或大于 collider.isTrigger 设置为 true。
#pragma strict
public var playerrangel : int = 0;
public var lockStrength : int = 1;
function Update ()
{
if (playerrangel == lockStrength || playerrangel >lockStrength){
// if both Ranges of player and lock are equal or greater then it sets Door Trigger to true.
collider.isTrigger = true;
print ("Door unlocked!");
}
else {
// if both Ranges of player and lock aren't equal or greater then it sets Door Trigger to false.
collider.isTrigger = false;
}
// The player enters range of the lock - 1 to 9.
if (Input.GetKeyDown ("1")) {
// Select Range 1
print (playerrangel + " Player Range");
print (lockStrength + " Lock Strength");
}
if (Input.GetKeyDown ("2")) {
// Select Range 2
print (playerrangel + " Player Range");
print (lockStrength + " Lock Strength");
}
if (Input.GetKeyDown ("3")) {
// Select Range 3
print (playerrangel + " Player Range");
print (lockStrength + " Lock Strength");
}
if (Input.GetKeyDown ("4")) {
// Select Range 4
print (playerrangel + " Player Range");
print (lockStrength + " Lock Strength");
}
if (Input.GetKeyDown ("5")) {
// Select Range 5
print (playerrangel + " Player Range");
print (lockStrength + " Lock Strength");
}
if (Input.GetKeyDown ("6")) {
// Select Range 6
print (playerrangel + " Player Range");
print (lockStrength + " Lock Strength");
}
if (Input.GetKeyDown ("7")) {
// Select Range 7
print (playerrangel + " Player Range");
print (lockStrength + " Lock Strength");
}
if (Input.GetKeyDown ("8")) {
// Select Range 8
print (playerrangel + " Player Range");
print (lockStrength + " Lock Strength");
}
if (Input.GetKeyDown ("9")) {
// Select Range 9
print (playerrangel + " Player Range");
print (lockStrength + " Lock Strength");
}
}
function OnTriggerEnter () {
playerrangel = Random.Range(1,30);
lockStrength = Random.Range(1,40);
}
我还有一个问题,当门被解锁时我能做些什么 Input.GetKeyDown 关闭所以玩家不能将它改回 false 并且只打印一次 "Door Unlocked!".
当你想改变它时,你应该再次调用Random.Range函数。 OnTriggerEnter 只会在碰撞开始时被调用。我建议改为这样:
int range = 0;
if (Input.GetKeyDown ("5")) { // Select Range 5
range = 5;
else if (Input.GetKeyDown ("6")) { // Select Range 6
range = 6;
if (range != 0)
{
int playerrangel = Random.Range(1,30);
int lockStrength = Random.Range(1,40);
// now do something with these values
}
对于你的另一个问题,你不能关闭Input.GetKeyDown
,但你可以跳过检查发生的功能:if (!doorOpened) CheckInput();
。那么很明显,你在播放成功后设置doorOpened=true;
所以我正在做这个开锁项目,它真的很简单,但我遇到了一些问题 Random.Range 当我按下按键时它不会改变我设置了。我的场景非常简单,我有一扇门,里面有脚本,还有 Unity 的第一人称控制器。 我希望 Random.Range 在我按下 1 到 9 键时更改值然后打印两者,如果它们等于或大于 collider.isTrigger 设置为 true。
#pragma strict
public var playerrangel : int = 0;
public var lockStrength : int = 1;
function Update ()
{
if (playerrangel == lockStrength || playerrangel >lockStrength){
// if both Ranges of player and lock are equal or greater then it sets Door Trigger to true.
collider.isTrigger = true;
print ("Door unlocked!");
}
else {
// if both Ranges of player and lock aren't equal or greater then it sets Door Trigger to false.
collider.isTrigger = false;
}
// The player enters range of the lock - 1 to 9.
if (Input.GetKeyDown ("1")) {
// Select Range 1
print (playerrangel + " Player Range");
print (lockStrength + " Lock Strength");
}
if (Input.GetKeyDown ("2")) {
// Select Range 2
print (playerrangel + " Player Range");
print (lockStrength + " Lock Strength");
}
if (Input.GetKeyDown ("3")) {
// Select Range 3
print (playerrangel + " Player Range");
print (lockStrength + " Lock Strength");
}
if (Input.GetKeyDown ("4")) {
// Select Range 4
print (playerrangel + " Player Range");
print (lockStrength + " Lock Strength");
}
if (Input.GetKeyDown ("5")) {
// Select Range 5
print (playerrangel + " Player Range");
print (lockStrength + " Lock Strength");
}
if (Input.GetKeyDown ("6")) {
// Select Range 6
print (playerrangel + " Player Range");
print (lockStrength + " Lock Strength");
}
if (Input.GetKeyDown ("7")) {
// Select Range 7
print (playerrangel + " Player Range");
print (lockStrength + " Lock Strength");
}
if (Input.GetKeyDown ("8")) {
// Select Range 8
print (playerrangel + " Player Range");
print (lockStrength + " Lock Strength");
}
if (Input.GetKeyDown ("9")) {
// Select Range 9
print (playerrangel + " Player Range");
print (lockStrength + " Lock Strength");
}
}
function OnTriggerEnter () {
playerrangel = Random.Range(1,30);
lockStrength = Random.Range(1,40);
}
我还有一个问题,当门被解锁时我能做些什么 Input.GetKeyDown 关闭所以玩家不能将它改回 false 并且只打印一次 "Door Unlocked!".
当你想改变它时,你应该再次调用Random.Range函数。 OnTriggerEnter 只会在碰撞开始时被调用。我建议改为这样:
int range = 0;
if (Input.GetKeyDown ("5")) { // Select Range 5
range = 5;
else if (Input.GetKeyDown ("6")) { // Select Range 6
range = 6;
if (range != 0)
{
int playerrangel = Random.Range(1,30);
int lockStrength = Random.Range(1,40);
// now do something with these values
}
对于你的另一个问题,你不能关闭Input.GetKeyDown
,但你可以跳过检查发生的功能:if (!doorOpened) CheckInput();
。那么很明显,你在播放成功后设置doorOpened=true;