Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Difference between GetButton, GetKey and GetMouseButton?

Discussion in 'Scripting' started by iamsidv, Jan 24, 2013.

  1. iamsidv

    iamsidv

    Joined:
    Jan 24, 2013
    Posts:
    17
    Hi I am a new bie in Unity3D. While going through the scripting references, I got familiar with these three keywords, But however, I am failing to understand the difference between these three.

    GetButton - (Does that mean it is related to the keyboard's button or the mouse button?)
    GetKey - (keyboard's key or the Mouse's Key)
    GetMouseButton - (Obviously, its for the mouse I guess.)

    PS : Do mention the uses of the above each.

    Thanks in advance. :)
     
  2. spinaljack

    spinaljack

    Joined:
    Mar 18, 2010
    Posts:
    892
    GetButton refers to the controls create in the input manager. Users can change these at run time.
    http://docs.unity3d.com/Documentation/Manual/Input.html

    GetKey uses keyboard key codes which is useful if you want to hard code inputs or create your own input manager

    GetMouseButton obviously for mouse buttons numbers e.g. 0 or 1 for left and right button
     
  3. Harinezumi

    Harinezumi

    Joined:
    Jan 7, 2013
    Posts:
    46
    I know this is an old thread, but my question is related, and I couldn't find anything more recent.

    Is there any difference between the following 2 lines?
    Code (CSharp):
    1. Input.GetMouseButton(0); // parameter could be 1 or 2 as well
    2. // vs.
    3. Input.GetKey(KeyCode.Mouse0); // parameter could be Mouse1 or Mouse2 as well
    I just really hate using int constants and would much prefer the enum.

    Thanks!
     
    apw6_unity likes this.
  4. sharmajeenit2000

    sharmajeenit2000

    Joined:
    Jan 16, 2019
    Posts:
    1
    Yes, there's a major difference @Harinezumi although it is tricky to get, i will try,
    Ex.
    If(Input.GetMouseButton(0){
    Shoot();
    }
    AND
    if(Input.GetKey(KeyCode.Mouse(0){
    Shoot();
    }

    In the second example it does not matter if you keep holding the left mouse button, the shoot func. will be called just once. You can use it for shooting rifles or shotguns, etc.(In Practical examples)

    Whereas, in the first one the shoot func. will be called every frame(until you have held the button) this can be used for shooting automatic rifles, etc.

    hope you get it.
     
  5. emrehancivelek

    emrehancivelek

    Joined:
    Jul 30, 2019
    Posts:
    1
    @sarmajeenit2000

    GetMouseButtonDown(0) Doesn't work while holding (Just a click)
    GetMouseButton(0) Works while holding
    GetKey(KeyCode.Mouse(0)) Works while holding