- Home /
Double-tap Number keys
Hello am trying to make a fps game. and i try to set up my weapon system i havnt`s goten the script for changing weapons via scroll whell yett but am currious how i can get (Double-tap) effect eks pressing (1) i have a knife but if i press 1 2 times quickly i get another kinife/weapon so instead of be limited to 0-9 i can have 0-9 *2 of weapons :) ps am Norwegian with Dyselexia and am New to UnityAnswer.etc so.
Hi, you need to re-structure your post. Include all the scripts you are using and what you have tried doing so far to solve this.
re-structure? yes her is my weapons switching script i use for number`s per weapon i havn`t tried on the Double tap number yett what i mean is that if i press 0 up to 9 i get different weapons but if i double press one of the number i get another weapon so that ins$$anonymous$$d of 10 weapons i can have 20 weapons for a fps game am working on or atleast trying so far only cg has done good. simple weapon switch script below:
function Awake()
{
// Select the first weapon
SelectWeapon(0);
}
function Update ()
{
//Check if the fire button was pressed
if(Input.GetButton("Fire1"))
Broadcast$$anonymous$$essage("Fire");
if(Input.Get$$anonymous$$eyDown("1"))
{
SelectWeapon(0);
}
else if(Input.Get$$anonymous$$eyDown("2"))
{
SelectWeapon(1);
}
else if(Input.Get$$anonymous$$eyDown("3"))
{
SelectWeapon(2);
}
else if(Input.Get$$anonymous$$eyDown("4"))
{
SelectWeapon(3);
}
}
function SelectWeapon(index : int)
{
for (var i=0;i<transform.childCount;i++)
{
// Activate the selected weapon
if (i == index)
{
transform.GetChild(i).gameObject.SetActiveRecursively(true);
}
// Deactivate all other weapons
else
{
transform.GetChild(i).gameObject.SetActiveRecursively(false);
}
}
}
am not good at scripting i got this via folowed a digitaltutor tutorial so. if enny input on how to make a simular script but with the doubel press function aswell that whud be greate.
Answer by NoseKills · Oct 28, 2014 at 09:19 PM
I didn't test this but this should give you the idea.
if a weapons key was pressed, store which key it was into
lastPressedNumber
if a weapons key was pressed, store the time when it happened to
lastPressedTime
(otherwise reset both
lastPressedNumber
andlastPressedTime
)if a weapons key is pressed and the same key was pressed within
doubleTapTime
seconds, switch to secondary weapon of that keyvar lastPressedNumber : String; var lastPressedTime : Number; var doubleTapTime : Number = 0.3; function Update () { //Check if the fire button was pressed if(Input.GetButton("Fire1")) BroadcastMessage("Fire");
var timeSincePress: Number = Time.realtimeSinceStartup - lastPressedTime; if(Input.GetKeyDown("1")) { if (timeSincePress < doubleTapTime && lastPressedNumber == "1") { SelectWeapon(10); } else { SelectWeapon(0); } lastPressedNumber = "1"; } else if(Input.GetKeyDown("2")) { if (timeSincePress < doubleTapTime && lastPressedNumber == "2") { SelectWeapon(11); } else { SelectWeapon(1); } lastPressedNumber = "2"; } else if(Input.GetKeyDown("3")) { if (timeSincePress < doubleTapTime && lastPressedNumber == "3") { SelectWeapon(12); } else { SelectWeapon(2); } lastPressedNumber = "3"; } else if(Input.GetKeyDown("4")) { if (timeSincePress < doubleTapTime && lastPressedNumber == "4") { SelectWeapon(13); } else { SelectWeapon(3); } lastPressedNumber = "4"; } else { lastPressedNumber = null; lastPressedTime = 0; } if (lastPressedNumber != null) { // something was pressed lastPressedTime = Time.realtimeSinceStartup; } }
oh nice. Thank you so mutch. al see to test it out. i can reply to morrow if it worked with or Withot enny modification. :D i dont have internett in the Norwegian night so+i start at school early but i might be abel to test it and reply quickly as possible. :)
i tested it i had modify it some and so now it is set properly but it dont work some how with the double press it stil youst work with 0-9. but it got me to think more correctly thoe in scripting. so i might figure it out and post it. :)
Your answer
Follow this Question
Related Questions
GUI display weapon name you are looking at 1 Answer
Weapon shooting mechanism problem. 1 Answer
Weapon switching 1 Answer
methods of positioning weapons to hand 2 Answers
find the angle to rotate the gun to face mouse point with its barrel 1 Answer