Hey guys I do not much about coding but here is what I want
Create a script to simulate key presses
Enter a value through the inspector so the output
changes accordingly
Users enters 65, print(Pistol);
Users enters 66, print(Shotgun);
If the same value is entered twice for a key, switch to
a secondary weapon
Create the logic for this and to test it, turn off the Clear on
Play option for the output
If you guys could send me a code for this that would be great
Replies
var CurrentWeapon = 0;
private var LastSelection = 0;
function Update (){
if (Input.GetKeyDown ("space")){
CheckValues();
}
}
function CheckValues(){
if (CurrentWeapon == LastSelection){
CheckSecondaryWeapon();
}
if (CurrentWeapon == 65 && SecondaryWeapon == false) {
print ("pistol");
LastSelection = 65;
}
if (CurrentWeapon == 66 && SecondaryWeapon == false) {
print ("Shotgun");
LastSelection = 66;
}
}
function CheckSecondaryWeapon(){
if (SecondaryWeapon == true){
SecondaryWeapon = false;
LastSelection = 0;
}
if (CurrentWeapon == 65 && LastSelection == 65) {
print ("SwitchingToSecondary");
SecondaryWeapon = true;
LastSelection = 65;
}
if (CurrentWeapon == 66 && LastSelection == 66) {
print ("SwitchingToSecondary");
SecondaryWeapon = true;
LastSelection = 66;
}
}
heres a quick go at it, not sure if it works, i just wrote it here in the reply window. Hope i didnt just do your homework dude.
could you show me?
http://www.codecademy.com/courses/conditionals-in-javascript/3?curriculum_id=4f4b35445cb288000300000c#!/exercises/0
even better would be using C# when coding for Unity, it's much more elegant
I also forgot to mention that I have this code attached to an empty game object, I'm not sure if that makes a difference to anything you said... and what function would I use for keyboard presses? Does this look close to working?
private var _input : String;
function Update ()
{
// Determine what key was pressed
var digit : String = "65";
if(Input.GetKeyDown(KeyCode.KeyPad0) || Input.GetKeyDown(KeyCode.Alpha0))
digit = "65";
// ... do this for all the digits
if(digit != "")
_input += digit;
// If the buffer is ready, parse it
if(_input.Length >= 2)
{
var num : int = parseInt(_input);
_input = "";
switch(num )
{
case 65:
print("pistol");
break;
case 66:
print("shotgun");
break;
}
}
}