Search Unity

What are the best ways to get keyboard and mouse input?

Discussion in 'Scripting' started by JustaDuck97, Oct 11, 2019.

  1. JustaDuck97

    JustaDuck97

    Joined:
    Jul 31, 2019
    Posts:
    45
    I'm a beginner and don't understand how exactly to get input from your keyboard and mouse. I really don't completely understand getting input from anywhere. I've followed many tutorials, but they all do it differently and don't explain clearly. I understand that the way you write code and vary greatly from person to person, but I don't understand any ways that have been presented to be because they are never explained clearly.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    "Best" depends on what you're doing. The easiest way to get keyboard input is with Input.GetKeyDown, Input.GetKey, and Input.GetKeyUp. You can instead use the input manager and create joystick like axis and user configurable key layouts, but the extra layer of abstraction while useful is just another thing to go wrong for a beginner. So I'd try the ones I mentioned first.

    https://docs.unity3d.com/ScriptReference/Input.GetKeyDown.html
    https://docs.unity3d.com/ScriptReference/Input.GetKey.html
    https://docs.unity3d.com/ScriptReference/Input.GetKeyUp.html

    As far as mouse input, if you're talking button clicks you can use the similarly named Input.GetMouseButtonDown, Input.GetMouseButton, and Input.GetMouseButtonUp. Button 0 is left click for a right hand configured mouse, button 1 is right click. Again there is input manager abstraction available as well.

    https://docs.unity3d.com/ScriptReference/Input.GetMouseButtonDown.html
    https://docs.unity3d.com/ScriptReference/Input.GetMouseButton.html
    https://docs.unity3d.com/ScriptReference/Input.GetMouseButtonUp.html

    There's also a new input manager I haven't tried yet which I believe is in a preview package. You could investigate that if you're interested.
     
    JustaDuck97 likes this.
  3. JustaDuck97

    JustaDuck97

    Joined:
    Jul 31, 2019
    Posts:
    45
    Thank you very much