unity 3d student

Beginner B02 – Detecting Input

How to detect basic input using a script.

Code Used (Javascript)

function Update () {
  1.     if(Input.GetButtonUp("Jump")){
  2.         Debug.Log("We Have Hit the Space Bar!");
  3.     }
  4. }

Further Reading

Share via Social Media

  • Digg
  • StumbleUpon
  • del.icio.us
  • Facebook
  • Yahoo! Buzz
  • Twitter
  • Google Bookmarks
  • Add to favorites


    I hope it’s okay to ask questions here as well.

    Why do you have to check keypresses in the Update() function, but not collisions? Wouldn’t you also need to check every frame if there is a collision?

    • drej – I completely see your logic here and technically you’re correct, you just don’t do it with an Update() function. Try and think of it this way – the collision functions report when something occurs, so if they do collide, they tell the engine, if nothing happens they don’t. Whereas with Update() it will run what you place into it, but if you put no instructions in, it does nothing too, the difference is that a collision may occur, causing a set of instructions to be hit whereas as something like the input class may be called by a keypress, but it may keep occuring, likewise you could say that a collision could keep occuring but it’s simply the way that Unity handles things – keeping the collision reporting to its own designated function gives you the opportunity to deal with things separately in terms of logic – Its a tricky one to explain but you simply need to bear in mind that Unity has some classes to deal with things ready made and others are dealt with by things like Update, in a more generic manner – you could of course say hey why isn’t there a function KeyPress() – well there are some mouse functions like that – check the docs for more info – its just that we’re listening out for a key release in the example given – it all ultimately gets fed back to the engine in a similar way, but Unity hides this from you and tries to offer classes and coding structures that make more logical sense. Sorry for the mess of an answer, its a little late here!

    Thanks — your tutorials are really really helpful… 🙂

  • I just wanted to thank you for your tutorials, I just installed unity on my pc and so far I have tried all your examples and all good!
    I was wondering if we are going to have a tutorial about how to publish from PC to iPhone down the road.

  • I haven’t yet gone through all of the tutorials posted yet (By the way, excellent job from what I’ve seen so far), but I was curious if there is a way that you can manage the keys yourself or even allow the user to change it themselves. The reason I ask this is because I am building a FPS with friends and I’m trying to enable an “AimDownSight” script I found in the Unify wiki page. In the details, the author of the script asks for you to create a button called “Sights” and that you should map a button to it. My question would be – how do I do that? If possible I’d like the RightClick on the mouse (I believe its called Mouse2) to do this, but I’m completely lost when trying to make this happen in my code.

    • Adam, you can manage your inputs by going to Edit > Project Settings > Input.

      To get something like you’re trying you should simply be able to query if(Input.GetButtonDown(“Fire2”)){ // do your aiming } and then GetButtonUp to get out of aiming though. Note that when looking at the settings of Fire1 and Fire2 in input settings, you’ll see ‘mouse1’ as Fire2, this is because mouse0 is left click.

    These Tutorials are great i was a bit nervous starting out with unity coz i thought it would be complicated as hell but know i know what i missing it creates smooth running scenes with top physix without even entering a line of code, even a simple scene created with Unity would take thousands of lines of code.

  • Hi, just wanted to say that your tutorials are great, I am brand new to this sort of thing and so far everything has been so easy to follow. So thank you as this has helped so much, I find it so tough learning from a book.

    Having said that, I know I will need to understand the scripting side of things a bit more, could you offer any suggestions for resources that explain the different parts of the script you are writing.. I get what is going on in your examples as you explain it well, but this means I only know these basic examples, and I would really like to understand the broader concepts behind it so that I can pick this up more quickly.

    Cheers! 🙂

  • The best way to remember thease scripts are to do bit by bit and copying it down at first then dealte it and try to do it your self without looking untill you have done it without any mistakes 🙂

  • So for the buttons like space bar i can buy one of those gaming controllers with the analog sticks the triggers and the bumpers and it will read it, right?
    great tutorials I plan on making a zombie game and try to publish it. >=D

  • Here’s a tip, don’t name the file Input, it won’t like you.

  • I did the following to check the press and release of the space bar:

    function Update () {

    Debug.Log("Space bar Press!");
    Debug.Log("Space bar released!");



  • sir,
    your tutorial is very good and outstanding i really like it.
    sir my question is that
    when i put my c# script to empty game object.than it give an error like

    can’t add script

    can’t add script behavior keypress. the script file does not match the name of class defined in the script.

  • First of all, thank you so much for the excellent tutorials. I love that they begin from scratch, that really resonates with me.

    I noticed that here and in the collision detection vid that you are doing a comparison based on a string. That does not seem very efficient to me. Does Unity do something to make that fast? Is it better to check against some other kind of enumeration? Thanks.

  • C# code:

    using UnityEngine;
    using System.Collections;

    public class KeyPress : MonoBehaviour
    void Update()
    Debug.Log(“We have hit the space bar!”);
    }// End if
    }// End Update method
    }// End KeyPress class

  • Here’s what I have for c#:

    using UnityEngine;
    using System.Collections;

    public class keyPress : MonoBehaviour {

    // Use this for initialization
    void Start () {


    // Update is called once per frame
    void Update () {

    Debug.Log (“Pressed Space Key!!”);


  • Just found a really interesting website for learning JavaScript. Dunno how well it’ll teach but I’m willing to give it a try. http://www.Codeavengers.com

    Check it out! 😀

  • X-Box Controller & Unity 4.

    I notice every time I click the x-box controller button it fires many times rather than only once.
    For example:
    input.GetButton(“Fire”) – X-Box recognises the action & but fires many time per tap.
    input.GetButtonDown(“Fire”) – X-Box does not recognise this.
    input.GetButtonUp(“Fire”) – X-Box does not recognise this.

    How to set UP/Down for x-box controllers ??

    Best, Michael

    • sounds like you’re detecting over multiple frames. Where is your GetButton call?

    These tutorials are a big help with learning Unity scripting, that’s for sure.

Leave a Comment