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?

1 Answer

1

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.

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).