Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Getting Input using InputManager

Discussion in 'Scripting' started by lightpower26, Apr 19, 2016.

  1. lightpower26

    lightpower26

    Joined:
    Apr 19, 2016
    Posts:
    4
    1227 I'm pretty new to Unity, I recently learned quite a bit of JavaScript. I know most of the basics... I think. Now I just gotta learn the API. I noticed that Unity has a InputManager, so I messed around with that for a little bit and found that I can use that to help players customize their inputs. Now I'm a little confused... I have 3 inputs, "jump", "left", "right", "down", and "forward". How exactly would this work? This is what I have...

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. function Update(){
    4.     if (Input.GetKey("forward") && Input.GetKey("left")){
    5.         print("Walking forward and to the left");
    6.     } else if (Input.GetKey("forward") && Input.GetKey("right")){
    7.         print("Walking forward and to the right");
    8.     } else if (Input.GetKey("back") && Input.GetKey("left")){
    9.         print("Walking backward and to the left");
    10.     } else if (Input.GetKey("back") && Input.GetKey("right")){
    11.         print("Walking backward and to the right");
    12.     } else if (Input.GetKey("forward")){
    13.         print("Walking forward");
    14.     } else if (Input.GetKey("left")){
    15.         print("Walking to the left");
    16.     } else if (Input.GetKey("back")){
    17.         print("Walking backward");
    18.     } else if (Input.GetKey("right")){
    19.         print("Walking to the right");
    20.     } else if (Input.GetKey("jump")){
    21.         print("Jumping");
    22.     }
    23. }
    24.  
     
  2. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    726
  3. lightpower26

    lightpower26

    Joined:
    Apr 19, 2016
    Posts:
    4
    Ohhh... I see... The API reference is a bit... outdated I'd say, then. I just tried using the keys with strings like "w", "a", "s", and "d" since it said it could take KeyCodes OR strings... That or I read it wrong. Thanks for the reply, flash! I'll try it out when I have time
     
  4. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    The InputManager cannot be edited at RunTime only in the Editor as it is an asset (see SerializableObject). What you can do, and what most people do, is wright a wrapper for the inputs by creating an Action System. By creating an Action System you don't care were the inputs are coming from you just care about the end results (floats, bools). I only use the InputManager for GamePad layouts and thats it. Everything else is wrapped and saved on disc so the player can edit the bindings at runtime as to their pleasure.