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