Complex Navigation Control System -> 1000th Support Post

Hi,

I’m working on a more complex navigation control system than what seems to be availible via the API scripting. Please inform me of a possible work around. Thanks. (I’m a 2X Licensed Owner)

What I need is an EVENT to be fired when a KEY is released. I’m doing a port from different engine that has the following API functions:

Key(){}// ~= Unity's Input.GetKeyDown
KeyPressed(){}// ~= Unity's Input.GetKey
KeyReleased(){}// != Unity's Input.

What I’d like is Input.GetAxis to have all 3 states (Axis,AxisPressed,GetAxisRelease). that way the user can have a custom interface.

Thanks,
VICTOM

You could use Input.anyKey in the Update function to see if any key was held down and based processing off that. If you are concerned about multiple held keys at a time then this would require you have a flag, an array, or someway to indicate. Another option would be to use Input.inputString and process the input that way. Seems like I ran into a little quirk with inputString but I do not remember. Maybe something like:

function Update() {
  if (Input.anyKey) {
    CheckFlagsOrArrayAndClearAndPerformAnyOtherProcessing();
  } else {
    ProcssGetKeysOrInputStringToCheckStateChanges();
  }
}

i don’t have time to find it now but you could search the forums for a charge script (i think that’s what it was called) - basically on mouse down your projectile charges on release it fires. i think that would probably get you what you want ; )

Something like this maybe?

var keyPressDown = false;

function Update () {
    if (Input.GetKeyDown ("down")) {
        print ("down key was pressed down");
        keyPressDown = true;
    }
    if (keyPressDown  !Input.GetKey ("down")) {
        print ("down key was released");
        keyPressDown = false;
    }
}

–Eric

Thanks for the reply Ifrog. The navigation system I’m using does use a flag system. This is why I need KeyReleased or AxisReleased to set my flag back to 0

I’ll explain…

//pseudo code
function Navigation(Heading){ // Heading = 0...3
  if ( compassLocked = [0,0,0,0] ) {
   //compass is NOT locked so LOCK Heading
     compassLocked[Heading] = 1;
     switch(Heading){
        case 0: //do animation for north
        case 1: //do animation for east
        case 2: //do animation for south
        case 3: //do animation for west
     }
  }
  // OK The compass IS LOCKED so...
  // TEST NORTH
  else if(Heading == 0){
     // Now test for strafing N
     // Because We're NOT going N YET we pressed <UP>
     if (compassLocked[1]{//animate E  strafe N}
     if (compassLocked[3]{//animate W  strafe N}
  }

  // TEST EAST ( AND then S,W same way)
  else if(Heading == 1){
     // Now test for strafing E
     // Were NOT going E YET we pressed <RIGHT>
     if (compassLocked[0]{//animate N  strafe E}
     if (compassLocked[1]{//animate S  strafe E}
  }
}//END OF Navigation

/////////////////////////////////////

//KeyReleases call NavigationRelease 

function NavigationRelease(HeadingReleased){
 switch(HeadingRelease){
  case 0://TEST NORTH <UP>
   if(compassLocked==[1,0,0,0]){
    compassLocked[0]=0
   }
   if(compassLocked[1]{//go E  end strafe N}
   if(compassLocked[3]{//go W  end strafe N}

  case 1://TEST EAST <RIGHT>
   if(compassLocked==[0,1,0,0]){
    compassLocked[1]=0
   }
   if(compassLocked[0]){//go N  end strafe E}
   if(compassLocked[2]){//go S  end strafe E}

  //case 23 S,W same way
}

Press IS DIFFERENT than…
Press

ALSO holding IS DIFFERENT than…
press

Thanks for reading this. I look forward to replies.

Cheers,
-VICTOM

var wasDown = false;

function Update ()
{
   var isUp = !Input.GetKey("space")  wasDown;
   wasDown = Input.GetKey("space");
   if (isUp)
       print("releasing button now");
}

That said we will add an Input.GetButtonUp functions.

Thank YOU!
While we’re on the subject of new functions.
Why not update Unity’s class MonoBehaviour to include these Overridable Functions:

  • OnKey(‘k’) Fires once on ‘k’
  • OnKeyPress(‘k’) Fires multiple on ‘k’
  • OnKeyRelease(‘k’) Fires once on ‘k’
  • OnAxis(‘Horizontial’) Fires once
  • OnAxisPress(‘Horizontial’) Fires multiple
  • OnAxisRelease(‘Horizontial’) Fires once
    This would allow scripts to be written tighter and allow for more reuse. Also, since it is a new event it doesn’t TAX the Update()or FixedUpdate() functions each frame for testing inputs. To me… PUSHING (On) events is better than PULLING (Get) events, I’m safe to say…yes?

Thanks to everyone who replied!

Cheers,
-VICTOM

In application development, this might be true. In the case of games it is not.

When writing controllers for game objects, it is very convenient to be able to interleave the code. Eg. You have a character and want him to move forward and sideways.
If you were event driven you would have to first move him forward, then move him sideways or in whatever order the user pressed the keys.

Event driven advocates of course bring up that polling is evil since it takes up resources, while nothing needs to be done. And in the gui case it is true, but in the case of games it generally is not true or doesn’t matter at all when you look at it in a profiler.

This is because most of your game code for a player controller has some kind of loop anyway, which needs to be processed every frame no matter what. And it is extremely convenient to have all processing inside of that loop.

Eg. only because no button is pressed doesn’t mean your character doesn’t move anymore. In the case of physics even more so, since you might also need to stop the character.

That said, there are cases for using event driven input in games, but polling is certainly generally more useful.

You should still be able to use the suggestion above to implement your compass.

  1. Thanks for the insight.

:shock: Don’t get me wrong; I’m no flag waver; I’m just a curious spectator.

:wink: So… there’s a chance for event driven input in Unity?

:? Opps, my bad.
:smile: YES! Thank you for the code. I meant to mention that in my last post.
:idea: Thanks for producing such an awesome product!

Cheers,
-VICTOM