Search Unity

Input Question

Discussion in 'Scripting' started by mehware, Nov 22, 2007.

  1. mehware

    mehware

    Joined:
    Nov 19, 2007
    Posts:
    739
    Why in some of the controller example code

    Code (csharp):
    1.  
    2. var v = Input.GetAxisRaw("Vertical");
    3. var h = Input.GetAxisRaw("Horizontal");
    4.  
    is used for input for player movement and not getInputKey("w"); for forward movement for example.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Because it's much easier to write:

    transform.position.x += Input.GetAxis("Horizontal");

    than:

    if (Input.GetKey("a")) transform.position.x -= 1.0;
    if (Input.GetKey("d")) transform.position.x += 1.0;

    Over and over. Especially since you can combine them too:

    transform.position += Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

    And also this allows players to select their own controls and use joysticks, etc.
     
  3. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    The biggest reason is that if you work through an Input Axis, end users can reconfigure their keys in the final game from the launch dialog
     
  4. David-Helgason

    David-Helgason

    Moderator

    Joined:
    Mar 29, 2005
    Posts:
    1,104
    .... and that GetAxis() can be smoothed, instead of being either -1, 0, or 1.

    d.