0
\$\begingroup\$

I am using the new Unity Input system for my game.

When I press a key, for example "Q", I want gizmos to be drawn.

However, I can't subscribe to a keypress event with a Gizmo drawing function, because it says it needs to be inside of OnDrawGizmos(). Furthermore, I can't subscribe with that function because it says it doesn't take any parameters, and I need to pass InputAction.CallbackContext context as a parameter, for it to be considered okay by the event system.

Here's the code for how I did it before:

public void OnDrawGizmos()
{
    if (Input.GetKey(KeyCode.Q))
    {
        DrawStuff();
    }
}

How can I recreate this?

\$\endgroup\$
2
  • \$\begingroup\$ Have you considered setting a variable in the input response function, and checking that variable in OnDrawGizmos? \$\endgroup\$
    – DMGregory
    Feb 26, 2021 at 16:26
  • \$\begingroup\$ I will try to do that now. However that seems like a pretty big workaround, I thought there was some other method to achieve this. \$\endgroup\$
    – caleidon
    Feb 26, 2021 at 16:27

1 Answer 1

2
\$\begingroup\$

As I explained in my answer to your previous question, you generally should try to avoid thinking of input in terms of keypresses and more in terms of semantic actions. But in this particular case you are talking about the OnDrawGizmos editor-feature your actual players are not going to see anyway, so it's no big deal to go for a hackish solution. The 1:1 equivalent of the above code with the new Input API would be:

public void OnDrawGizmos()
{
     if (Keyboard.current[Key.Q].isPressed) {
         DrawStuff();
     }
}

For more information:

  • The Migration Guide from the old to the new input system (but keep in mind that most of the things described there are no longer best practice!)
  • Documentation for Keyboard (which is unfortunately a total mess). .current is the current keyboard (the new input system API is already designed with future support for multiple keyboards in mind, but currently the engine does not support more than one)
  • Documentation for KeyControl (which tells you what else you can do with a specific keyboard key)
\$\endgroup\$
1
  • \$\begingroup\$ Thank you, This is exactly what I was looking for. Hackish or not, it's not shown to players so this seems like the best and easiest way to do it. \$\endgroup\$
    – caleidon
    Feb 26, 2021 at 17:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .