Random.Range不使用整数,为什么?

时间:2015-03-02 08:43:10

标签: unity3d unityscript

所以我正在做这个锁定选择的项目,它非常简单,但是我对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关闭,所以玩家无法将其改回假,并且只打印一次"门解锁了!"。

1 个答案:

答案 0 :(得分:2)

当您想要更改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;