Boolean switch ...

Hello,

The following script didn’t work and don’t find why … Somebody can help ?
The goal : using spacebar to switch between 2 positions.

Regards,

Patrick


var camActiveB : GameObject;

//… all variables declared …

var camSwitch : boolean = false;

function LateUpdate () {
if (!targetB)
return;

// … Other operations …

if(camActiveB.active == true camSwitch==false Input.GetKey(“space”)){
DistanceB = bdist2;
HeightB = bhei2;
bside = bsid2;
camSwitch=true;
}

if(camActiveB.active == true camSwitch==true Input.GetKey(“space”)){
DistanceB = bdist1;
HeightB = bhei1;
bside = bsid1;
camSwitch=false;
}

I’m not really a JS programmer, and this is completely untested, but I’d try something like this:

if (camActiveB.active == true  Input.GetKey("space")) {
   if (camSwitch)
   {
      DistanceB = bdist1;
      HeightB = bhei1;
      bside = bsid1;
   }
   else
   {
      DistanceB = bdist2;
      HeightB = bhei2;
      bside = bsid2;
   }
   camSwitch = !camSwitch;
}

One other thought. Is Input.GetKey really what you want? That’ll continue to fire as long as the key is held down, so you could unintentionally toggle the value of camSwitch rapidly in succession.

Perhaps Input.GetKeyDown might be a better fit, though I don’t know the context of what you’re trying to do…

Jeff