Home Technical Talk

Unity 3d Using number's as key presses

manilamerc
polycounter lvl 6
Offline / Send Message
manilamerc polycounter lvl 6
I need help trying to input number's into the coding here is what I'm trying to do:

If you press the number one it will choose a weapon. If you press the number one again it will choose another weapon. The cycle continues as you keep pressing the number one. If you guys could show me what code could make this happen that would be awesome.

Here is what I got I am so lost

var projectile : GameObject;

function Start ()
{

}

function Update ()
{

if(Input.GetButtonUp("1"))
{
print("axe");
}


}

Replies

  • LoTekK
    Offline / Send Message
    LoTekK polycounter lvl 17
    You'd probably have better luck asking straight coding questions on the Unity forums, or on the Unity Answers site.
    var counter:int = 0;
    var weaponArr:string[] = ["axe", "knife", "someOtherThing"];
    var weaponCount:int;
    
    function Start()
    {
      weaponCount = weaponArr.length;
    }
    
    function Update()
    {
      if(Input.GetButtonDown("1"))  //Down is likely preferable to Up in this case
      {
        counter++;  //increment the global counter
        print(weaponArr[counter % weaponCount]);  // % is modulo, which is handy to "loop" a counter
      }
    }
    
  • Lamont
    Online / Send Message
    Lamont polycounter lvl 16
    Would a case statement be better for something like this?
  • LoTekK
    Offline / Send Message
    LoTekK polycounter lvl 17
    For an actual use-case scenario, I'd probably lean towards yes.
    Switch(counter % weaponCount)
    {
      case 0:
        //stuff
        break;
      case 1:
        //stuff
        break;
      case 2:
        //stuff
        break;
    etc
    }
    
Sign In or Register to comment.