- Home /
Question by
AidanHorton · Apr 30, 2014 at 04:23 PM ·
booleanswitch
Changing Missile Speed
Ok, so i'm making a game, and I have missiles. For some developer cheats I want it when you press 'q' and 'e' together it changes the speed to something higher, but when I press it the speed doesn't change... Here's the code:
var projectile : Rigidbody;
var speed = 20;
var dev : boolean = false;
function Update () {
if (Input.GetKey('q')) {
if (Input.GetKey('e')) {
Switch(dev);
}
}
if (dev == true) {
speed = 300;
}
else if (dev == false) {
speed = 135;
}
// Put this in your update function
if (Input.GetButtonDown("FireCar2")) {
// Instantiate the projectile at the position and rotation of this transform
var clone : Rigidbody;
clone = Instantiate(projectile, transform.position, transform.rotation);
// Give the cloned object an initial velocity along the current
// object's Z axis
clone.velocity = transform.TransformDirection (Vector3.forward * speed);
}
}
function Switch (bool){
if( bool == false) {
bool = true;
}
else {
bool = false;
}
return bool;
}
Any help would be appreciated!
Comment
There are logic errors here, but also some other issues. Start by putting a '#pragma strict' at the top of the file, and then fix the compiler errors.
Best Answer
Answer by robertbu · Apr 30, 2014 at 04:32 PM
Get rid of your 'Switch' function. Replace lines 6 - 10 with this:
if ((Input.GetKeyDown(KeyCode.Q) && (Input.GetKey(KeyCode.E)) || (Input.GetKeyDown(KeyCode.E) && (Input.GetKey(KeyCode.Q))) {
dev = !dev;
}
Note, like your original code, these keys would act like a switch. You may want to change the logic so that the speedup only happens with these keys are held down.