Search Unity

Add arrow key inputs to Switch On String?

Discussion in 'Visual Scripting' started by graphics2, Jul 20, 2021.

  1. graphics2

    graphics2

    Joined:
    Jul 14, 2021
    Posts:
    1
    I'm using v2020.3.14 with the Bolt installed, in case this makes any difference.

    I'm learning how to use visual scripting by making a cube move. I have the Input: Get Input String and Switch On String units checking for WASD to move around, but also wanted to use arrow keys. I couldn't find a way to use them with Switch On String, so instead I have a series of Input: Get Key and Branch units chained together to handle the arrow keys.

    Is there a way to add arrow key inputs to Switch On String?
     
  2. mortezadianatfar

    mortezadianatfar

    Joined:
    May 8, 2017
    Posts:
    6
    Hey,
    I tried to use arrow keys which didn't work, I had an idea to create a graph variable (arrowkeysIndex) use it as integer to set values such as 0,1,2,3 whenever each arrow key pressed down.(input getkeydown). After that using SwitchOninteger and use get variable as value and attach it to the rest of the transform(but I lost between step and unit requirements). Finally, I notice some videos on youtube which I will link below. The idea is that to use Unity InputManager and using vector 3 for moving object with transform.translate

    Youtube Link
     

    Attached Files:

  3. SurreyMuso

    SurreyMuso

    Joined:
    Mar 16, 2020
    Posts:
    63
    @graphics2 Welcome on board. Perhaps you'll allow me to give you a few tips.

    It seems you are trying to use the old input methods. I strongly recommend that you get into the newer "Input system". It's a little harder to get going with but you will get arrow keys as well as WASD. And it's the future of input so little point learning the old Input methods first. There are plenty of video tutorials on the Input System but here's a very quick high-level overview.

    You need four things:

    1. Unity's new Input System - this is a additional Package that you have to install
    2. A Component that talks to the hardware so you don't have to - this is the Player Input component
    3. Something to turn hardware inputs into usable functions (look, move, fire) - this is the Input Actions Asset
    4. A Visual Script that takes the function from the Input Actions Asset and makes your player move, jump etc

    A bit more detail:
    1. Install the Input System package (Windows > Package Manager > Unity Registry > Input System). Packages give you extensions to Unity and, in the future, the Input System will be installed automatically but, for now, you have to install it yourself. You'll be prompted to restart the project but that's OK,

    2. You need to add a "Player Input" component to your cube. (Add Component > Player Input) Think of this as the engine that interfaces with different keyboard and controllers. While it get all the input, it doesn't know what you want to do with it and that's why you need something called "Input Action Assets".

    3. In the Player Input component, click "Create Actions". This associates certain keys/buttons with specific actions – keys for jumping, keys for moving, keys for looking around. When you do this, WASD and arrow keys will automatically be assigned to the Move function. The Input Action Assets are added to your Projects > Assets and you can open it and have a look, if you want. The Editor automatically assigns these new Actions to the Player Input component for you.

    4. You now have to create your Player Visual Script graph. Something like the attached should get you going (though you'll have to add a Rigid Body to your cube for it to work).
    Why have Unity brought out the new system? It's because you can write the same code and it will work with a gamepad, a keyboard and even a tablet. There is also the advantage that you don't have to write code in the On Update. The system reacts to a key coming in (something in computing terms called an Interrupt) and then calls your code. That makes it more efficient.

    Please feel free to ask more questions.

    Basic Move.png
     
  4. mortezadianatfar

    mortezadianatfar

    Joined:
    May 8, 2017
    Posts:
    6
    Hey Beechside,
    I installed input system, and create action. but I can't find unit called "on input system event vector 2"?
    and also another question I have is that I have a character in my scene which would not have a rigid body component. what would be your suggestion?

    My case is that I am developing VR app and now I want to develop events and triggers. I don't want to put on HMD every time to double-check if everything works, I want to trigger events by using the keyboard to navigate the character.

    Thanks
     
  5. SurreyMuso

    SurreyMuso

    Joined:
    Mar 16, 2020
    Posts:
    63
    OK. Two things

    1) You may have to generate the new units that relate to your Input System.

    Edit > Project Settings > Visual Scripting > Regenerate Nodes

    I'm on a Beta version of the editor at the moment so it's possible some terms may have changed but you should be able to find the regenerate command easily enough. If you can't find the node after regeneration, I'll have another think.

    2) As for moving. I suggested using Velocity and Rigid Body to keep it simple. In practice, you might like to try using the Character Controller. Select your Player Character and Add Component > Character Component. Then try this script:

    CC.png

    Let me know how you get on...
     
    ernbot likes this.
  6. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,076
    v2020.3.14 with the Bolt won't have the new Input System nodes. You need to be on at least 2021.1 tech stream to get those.
     
  7. SurreyMuso

    SurreyMuso

    Joined:
    Mar 16, 2020
    Posts:
    63
    Right, as always, @PanthenEye, though it was OP who has v2020.3.14, not @mortezadianatfar

    Anyway, I've always admired your tech knowledge on Visual Scripting. Do you have access to some tech documentation or did you work for Ludiq? Curious to know how you acquired info on the inner workings...
     
  8. PanthenEye

    PanthenEye

    Joined:
    Oct 14, 2013
    Posts:
    2,076
    No special tech docs or anything. I was part of the Bolt community and started using it near its release. Most of my knowledge comes from personal experience and discussions with the original dev on Discord back in the day. Unity Visual Scripting at this point in time is reskinned Bolt so the knowledge transfers 1:1.
     
    SurreyMuso likes this.