- Home /
increasing rate of spawn on button press
I'm trying to give the player control over the rate of spawn of the 'ballClone's by pressing shift and - (_) to decrease and shift and = (+) to increase.
But it has no effect in the game, although there are no errors
var rate = 1.0;
var ball : Rigidbody;
private var tick = 0.0;
function Update () {
if (Time.time > tick)
{
tick = Time.time + rate;
var ballClone : Rigidbody = Instantiate(ball, Vector3(0,12,0),Quaternion.identity);
}
if (Input.GetKeyDown("_"))
{
rate = rate - 0.1;
}
if (Input.GetKeyDown("+"))
{
rate = rate + 0.1;
}
}
thought it would be simple, but I'm pretty stuck. Any ideas whats wrong?
Answer by aldonaletto · Sep 07, 2011 at 02:58 PM
Unity does not recognizes Shift+key combinations: it always reads "=" and "-" , no matter if shift is pressed or not - you must check for shift yourself, like this:
...
if (Input.GetKey("left shift") || Input.GetKey("right shift")){
if (Input.GetKeyDown("-")) rate = rate + 0.1;
if (Input.GetKeyDown("=")) rate = rate - 0.1;
}
... By the way, the logic was reversed: increasing variable rate actually reduces the rate, and vice versa. This was fixed on the code above.
thanks very much, this works perfectly.
does the || mean 'either' ?
It's the logic OR, and yes, you can think of it as an either clause. There also exists &&, the logic AND: you could think of it as both (or all, if there are more conditions).
Your answer
Follow this Question
Related Questions
continuous shooting 1 Answer
Help Guitar Hero Style or similar 0 Answers
Checking if object intersects? 1 Answer
how do i make my spawn system spawn every 2 seconds then wait 10minutes? 3 Answers
trying to regulate character speed. 1 Answer