I have a code that switches between 4 arrows when you press either A, S, W, or D. Here’s the script…
var radio : UnityEngine.UI.Graphic;
var lighter : UnityEngine.UI.Graphic;
var pipe : UnityEngine.UI.Graphic;
var none : UnityEngine.UI.Graphic;
function Start () {
radio.enabled = false;
lighter.enabled = false;
pipe.enabled = false;
none.enabled = true;
}
function Update () {
if (Input.GetButton("Fire2") && Input.GetKeyDown("d"))
{
lighter.enabled = true;
radio.enabled = false;
pipe.enabled = false;
none.enabled = false;
}
if (Input.GetButton("Fire2") && Input.GetKeyDown("s"))
{
radio.enabled = false;
pipe.enabled = false;
lighter.enabled = false;
none.enabled = true;
}
if (Input.GetButton("Fire2") && Input.GetKeyDown("a"))
{
none.enabled = false;
pipe.enabled = true;
radio.enabled = false;
lighter.enabled = false;
}
if (Input.GetButton("Fire2") && Input.GetKeyDown("w"))
{
pipe.enabled = false;
radio.enabled = true;
lighter.enabled = false;
none.enabled = false;
}
}
I want to make it so that the variable none also equals true when neither W, A, S, or D is held. Sort of like a default arrow or resting state. How could I do this?
Set up a Boolean flag, for example called hasInput, and set it true when ever an input is pressed and at the begginning of Update set hasInput to false.
I just tried it, and I think it would work, but the booleans are always true. Why is that?
var radio : UnityEngine.UI.Graphic;
var lighter : UnityEngine.UI.Graphic;
var pipe : UnityEngine.UI.Graphic;
var none : UnityEngine.UI.Graphic;
var wInput : boolean;
var aInput : boolean;
var dInput : boolean;
function Start () {
radio.enabled = false;
lighter.enabled = false;
pipe.enabled = false;
none.enabled = true;
}
function Update () {
if (Input.GetButton("Fire2") && Input.GetKeyDown("d"))
{
lighter.enabled = true;
radio.enabled = false;
pipe.enabled = false;
none.enabled = false;
}
if (Input.GetButton("Fire2") && Input.GetKeyDown("s"))
{
radio.enabled = false;
pipe.enabled = false;
lighter.enabled = false;
none.enabled = true;
}
if (Input.GetButton("Fire2") && Input.GetKeyDown("a"))
{
none.enabled = false;
pipe.enabled = true;
radio.enabled = false;
lighter.enabled = false;
}
if (Input.GetButton("Fire2") && Input.GetKeyDown("w"))
{
pipe.enabled = false;
radio.enabled = true;
lighter.enabled = false;
none.enabled = false;
}
if (wInput && aInput && dInput)
{
pipe.enabled = false;
radio.enabled = false;
lighter.enabled = false;
none.enabled = true;
}
if(Input.GetKeyDown("w"))
{
wInput = false;
}
else
{
wInput = true;
}
if(Input.GetKeyDown("a"))
{
aInput = false;
}
else
{
aInput = true;
}
if(Input.GetKeyDown("d"))
{
dInput = false;
}
else
{
dInput = true;
}
}
probably what you want
if (Input.GetButton("Fire2"))
{
//set defaults
pipe.enabled = false;
radio.enabled = false;
lighter.enabled = false;
none.enabled = true;
if(Input.GetKeyDown("d"))
{
lighter.enabled = true;
none.enabled = false;
} else if(Input.GetKeyDown("a"))
{
lighter.enabled = true;
none.enabled = false;
} else if(Input.GetKeyDown("w"))
{
radio.enabled = true;
none.enabled = false;
}
}
I honestly have no idea what this [variablename].enabled is. Is this how JS handles booleans? I use C# so I wouldn’t know.
Thanks! It works really well!