Difference between GetButton, GetKey and GetMouseButton?

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. :slight_smile:

GetButton refers to the controls create in the input manager. Users can change these at run time.

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

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?

Input.GetMouseButton(0); // parameter could be 1 or 2 as well
// vs.
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!

1 Like

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.

1 Like

@sarmajeenit2000

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

5 Likes