GetKeyUp not registering after a long hold

Here is what I am trying to do:
-While you hold space, the objects move 2x faster and start moving 2x faster. When you let go, their speeds return to normal.
-While you hold left shift, the objects move at 1/2 speed and start moving at 1/2 speed. When you let go, their speeds return to normal.

What it is doing:
When you press either key, everything speeds up or slows down like it should until the next spawn starts. If you let go before that happens, everything returns to normal speed and it works perfect. If you hold either through a spawn, the newly spawned objects dont register when the key is released, so they do not get their speeds adjusted back to normal speed.

Here is the spawner script:

#pragma strict
var isSpawning : boolean;
var ObstaclePrefab : GameObject;
static var speeding : boolean;
static var slowing : boolean;
static var speedx : float;
function Start () {
	isSpawning = false;
	speeding = false;
	slowing = false;
	speedx = -75;
}

function Update () {
	if(Input.GetKeyDown("space")  speedx==-75){
		speedx = -150;
	}
	if(Input.GetKeyUp("space")  speedx==-150){
		speedx = -75;
	}
	if(Input.GetKeyDown("left shift")  speedx==-75){
		speedx = -37.5;
	}
	if(Input.GetKeyUp("left shift")  speedx==-37.5){
		speedx = -75;
	}
	if(!isSpawning) Spawning();
}

function Spawning () {
	var spawncheck : int;
	var spawned : GameObject;;
	isSpawning=true;
	spawncheck = Random.Range(1,101); 
	if(spawncheck<=20){ //change this to change the chance of spawn.
		spawned = Instantiate(ObstaclePrefab,transform.position,transform.rotation);
		yield WaitForSeconds(.1);
		spawned.rigidbody.AddForce(Vector3(speedx,0,0));
		spawned.GetComponent(ObstacleScript).speeding=speeding;
		spawned.GetComponent(ObstacleScript).slowing=slowing;
	}
	yield WaitForSeconds(2.2);
	isSpawning=false;
}

And the Obstacle script (controls the speed of the moving objects):

#pragma strict
static var speeding : boolean;
static var slowing : boolean;
function Start () {

}

function Update () {
	if(transform.position.z<-.5) rigidbody.AddForce(Vector3(-.75,0,0));
	if(transform.position.x<-13) Destroy(gameObject);
	if(Input.GetKeyDown("space")  !speeding  !slowing){
		speeding = true;
		rigidbody.velocity=rigidbody.velocity*2;
		Debug.Log("Space Down");
	}
	if(Input.GetKeyUp("space")  speeding  !slowing){
		rigidbody.velocity=rigidbody.velocity/2;
		Debug.Log("Space Up");
		speeding = false;
	}
	if(Input.GetKeyDown("left shift")  !speeding  !slowing){
		slowing = true;
		rigidbody.velocity=rigidbody.velocity/2;
	}
	if(Input.GetKeyUp("left shift")  !speeding  slowing){
		slowing = false;
		rigidbody.velocity=rigidbody.velocity*2;
	}
}

function OnCollisionEnter (ColInfo : Collision){
	rigidbody.AddForce(Vector3(0,0,-3));
}

What makes me think the GetKeyUp is not registering is the Debug on line 18. If you release space before the next spawn, the log message is shown, if you wait til after the spawn, you don’t get the log message. I am getting no error message, and the code all looks right to me, so I need more eyes on it apparently. Does anyone see what is wrong? Why is it acting so strange? lockbox helped me narrow down the problem better (thanks by the way), so the new question is below.

Also, when I press and release space, I should get spammed with that debug.log message (one for each spawned object currently in play), but I only get one. Collapse is off in the console, I specifically turned it off to check this.

EDIT: New question, how do I get the newly spawned objects to register the getkeyup? And, I forgot to mention before, there are 33 spawners. I dont think that will matter, but Its a lot, so I figured Id throw it out there in case it might cause a problem.

Thanks

Try using this code instead …

    #pragma strict
    var isSpawning : boolean;
    var ObstaclePrefab : GameObject;
    static var speeding : boolean;
    static var slowing : boolean;
    static var speedx : float;
    
    var displayMessage : String = "";
    var id : int = 0;
    function Start () {
        isSpawning = false;
        speeding = false;
        slowing = false;
        speedx = -75;
    }
     
    function Update () {
        if(Input.GetKeyDown("space")  speedx==-75){
            speedx = -150;
            displayMessage = speedx.ToString();
        }
        if(Input.GetKeyUp("space")  speedx==-150){
            speedx = -75;
            displayMessage = speedx.ToString();
        }
        if(Input.GetKeyDown("left shift")  speedx==-75){
            speedx = -37.5;
            displayMessage  = speedx.ToString();
        }
        if(Input.GetKeyUp("left shift")  speedx==-37.5){
            speedx = -75;
            displayMessage  = speedx.ToString()     ;
        }
        if(!isSpawning) Spawning();
    }
     
    function Spawning () {
        var spawncheck : int;
        var spawned : GameObject;;
        isSpawning=true;
        spawncheck = Random.Range(1,101);
        if(spawncheck<=20){ //change this to change the chance of spawn.
            spawned = Instantiate(ObstaclePrefab,transform.position,transform.rotation);
            yield WaitForSeconds(.1);
            spawned.rigidbody.AddForce(Vector3(speedx,0,0));
            spawned.GetComponent(ObstacleScript).speeding=speeding;
            spawned.GetComponent(ObstacleScript).slowing=slowing;
          	spawned.GetComponent(ObstacleScript).id=id++;
        }
        yield WaitForSeconds(2.2);
        isSpawning=false;
    }
    
    function OnGUI() {
    	GUI.Box ( new Rect(10,Screen.height-36 ,122,24),displayMessage);
    }
    #pragma strict
    public var speeding : boolean;
    public var slowing : boolean;
    
    var displayMessage : String = "";
    var displayMessage2 : String = "";
    var displaySpeeding : String = "";
    var displaySlowing : String = "";
    public var id : int = 0;
    
    function Start () {
    }
     
    function Update () {
        if(transform.position.z<-.5) rigidbody.AddForce(Vector3(-.75,0,0));
        if(transform.position.x<-13) Destroy(gameObject);
        if(Input.GetKeyDown("space")  !speeding  !slowing){
            speeding = true;
            rigidbody.velocity=rigidbody.velocity*2;
            displayMessage = "Space Down";
        }
        if(Input.GetKeyUp("space")  speeding  !slowing){
            rigidbody.velocity=rigidbody.velocity/2;
             displayMessage = "Space Up";
            speeding = false;
        }
        if(Input.GetKeyDown("left shift")  !speeding  !slowing){
            slowing = true;
            rigidbody.velocity=rigidbody.velocity/2;
            displayMessage2 = "left Shift Down";
            
        }
        if(Input.GetKeyUp("left shift")  !speeding  slowing){
            slowing = false;
            rigidbody.velocity=rigidbody.velocity*2;
            displayMessage2 = "left Shift Up";
        }
    }
     
    function OnCollisionEnter (ColInfo : Collision){
        rigidbody.AddForce(Vector3(0,0,-3));
    }
    
    function OnGUI() {
    	if (speeding) {
    		displaySpeeding = "Speeding";
    	} else {
    		displaySpeeding = "Not Speeding";
    	}
    	GUI.Box ( new Rect(id*122,24,120,24), id + ":" + displaySpeeding);
    	
    	if (slowing) {
    		displaySlowing = "Slowing";
    	} else {
    		displaySlowing = "Not slowing";
    	}
    	GUI.Box ( new Rect(id*122,48,120,24),id + ":" + displaySlowing);
    	
    	GUI.Box ( new Rect(id*122, 96,120,24),id + ":" + displayMessage);
    	GUI.Box ( new Rect(id*122, 120,120,24),id + ":" + displayMessage2);
    }

If I correctly understood what your additions are doing, it pretty much verifies what I was thinking. New spawns while a button is pressed don’t get a true returned for a getkeyup. They all do in fact get the correct starting force, but because they dont register the getkeyup, they dont get slowed down or sped up to normal speed when the button is released.

The conditions to be met to register the GetKeyUp for left shift is not speeding and slowing. … think about it

BTW, how are your obstacles moving? I made them rigid bodies, but they don’t move. lol

and speeding and not slowing GetKeyUp for space

I dont know why I didnt consider the other variables not getting their values right, tho they should, I used that exact method elsewhere (copy and pasted from there to this part and changed some variable names). Ill look at that and make sure those variables are getting the values they are supposed to have. Thanks.

The moving objects have rigidbodies with a mass of 1, with use gravity un-checked. no other forces are acting on them other than what these 2 scripts do. Id guess you probably left gravity on for them, or gave them a huge mass, cant think of any other reason yours wouldnt be moving.

Left gravity on. lol

Ok, I feel like a complete idiot now. After going back and checking every where speeding and slowing are referenced, they were never getting true values, always false. It turns out, they were never changed in Spawn.js when objects were started going faster or slower, so when the item spawned, the spawn script was assigning false to both values in ObstacleScript.

Would it be better to keep doing input checks on every obstacle like i am doing (there ends up being around 40-60 obstacles running at once), or assign a mySpawner variable and use something like mySpawner.GetComponent(Spawn).speeding for both variables, using the spawners input instead?

Now to the next challenge, keeping the spacing between spawn waves even regardless of speeding or slowing, but thats for another time (or not hopefully), since I havn’t even attempted it yet.

Thanks for steering me in the right direction and helping me troubleshoot my own code there, rather than fixing it for me. Helps me learn better. And you got me a head start on learning the built in gui stuff that I havn’t had a chance to play with yet, so thanks for that too! :slight_smile:

Don’t. This is the right place to ask when you need another set of eyes to look at your code.

I don’t intuitively know the answer to this question. Both work, I think it’s a question of how many extra CPU cycles does it take to do one over the other. If you don’t press or release any keys - so no key up or down, then the first condition test is false and it doesn’t continue evaluating the other conditions versus whatever it takes to get that variable using javascript during every update cycle - and then you’re still doing the condition checks for speeding and slowing. i don’t know if java caches that address look up for the variable - or if it looks it up each time.

I would say continue with what you’re doing until performance becomes an issue, and revisit it then.

Glad I could help. :slight_smile: