ashtynj
February 23, 2020, 6:58pm
1
I’m making a game with a 3D keyboard that squashes the keys down when you press them, however I have run into an issue. to check that a key is being pressed, I have a script in a parent empty that checks every single letter to see if it’s being pressed and scales the associated child. I use the following code in void Update()
if (Input.GetKeyDown("q"))
{
temp = transform.localScale;
temp.x = 1.5f;
temp.y -= .5f;
temp.z = 1.5f;
parent.transform.GetChild(0).transform.localScale = temp;
}
if (Input.GetKeyUp("q"))
{
temp = transform.localScale;
temp.x = 1.5f;
temp.y += .5f;
temp.z = 1.5f;
parent.transform.GetChild(0).transform.localScale = temp;
}
if (Input.GetKeyDown("w"))
{
temp = transform.localScale;
temp.x = 1.5f;
temp.y -= .5f;
temp.z = 1.5f;
parent.transform.GetChild(1).transform.localScale = temp;
}
if (Input.GetKeyUp("w"))
{
temp = transform.localScale;
temp.x = 1.5f;
temp.y += .5f;
temp.z = 1.5f;
parent.transform.GetChild(1).transform.localScale = temp;
}
This does work for every letter I add, but it’s tedious and seriously long. I’d like to know if there’s an alternative way other than checking every single key individually. I’m a beginner to C# so if you could use easier-to-understand terms it’d be very much appreciated.
Hellium
February 23, 2020, 7:17pm
2
If all the keys have the same behaviour:
void Update()
{
for ( KeyCode key = KeyCode.A ; key <= KeyCode.Z ; ++key )
{
if ( Input.GetKeyDown( key ) )
{
temp = transform.localScale;
temp.x = 1.5f;
temp.y -= .5f;
temp.z = 1.5f;
parent.transform.GetChild( 0 ).transform.localScale = temp;
}
if ( Input.GetKeyUp( key ) )
{
temp = transform.localScale;
temp.x = 1.5f;
temp.y += .5f;
temp.z = 1.5f;
parent.transform.GetChild( 0 ).transform.localScale = temp;
}
}
}
If you want to prevent two keys from being pressed at the same time:
private KeyCode pressedKey = KeyCode.None;
void Update()
{
for ( KeyCode key = KeyCode.A ; key <= KeyCode.Z ; ++key )
{
if ( pressedKey == KeyCode.None && Input.GetKeyDown( key ) )
{
temp = transform.localScale;
temp.x = 1.5f;
temp.y -= .5f;
temp.z = 1.5f;
parent.transform.GetChild( 0 ).transform.localScale = temp;
pressedKey = key;
}
if ( pressedKey == key && Input.GetKeyUp( key ) )
{
temp = transform.localScale;
temp.x = 1.5f;
temp.y += .5f;
temp.z = 1.5f;
parent.transform.GetChild( 0 ).transform.localScale = temp;
pressedKey = KeyCode.None;
}
}
}
According to the details you gave in your comment:
private KeyCode pressedKey = KeyCode.None;
// Order it the way you want
private KeyCode[] keys = new KeyCode[]
{
KeyCode.A, // Will change child #0
KeyCode.B,
KeyCode.C,
KeyCode.D,
KeyCode.E,
KeyCode.F,
KeyCode.G,
KeyCode.H,
KeyCode.I,
KeyCode.J,
KeyCode.K, // Will change child #10
KeyCode.L,
KeyCode.M,
KeyCode.N,
KeyCode.O,
KeyCode.P,
KeyCode.Q,
KeyCode.R,
KeyCode.S,
KeyCode.T,
KeyCode.U,
KeyCode.V,
KeyCode.W,
KeyCode.X,
KeyCode.Y,
KeyCode.Z, // Will change child #25
};
void Update()
{
for ( int keyIndex = 0 ; keyIndex < keys.Length ; ++keyIndex )
{
KeyCode key = keys[keyIndex];
if ( pressedKey == KeyCode.None && Input.GetKeyDown( key ) )
{
temp = transform.localScale;
temp.x = 1.5f;
temp.y -= .5f;
temp.z = 1.5f;
parent.transform.GetChild( keyIndex ).transform.localScale = temp;
pressedKey = key;
}
if ( pressedKey == key && Input.GetKeyUp( key ) )
{
temp = transform.localScale;
temp.x = 1.5f;
temp.y += .5f;
temp.z = 1.5f;
parent.transform.GetChild( keyIndex ).transform.localScale = temp;
pressedKey = KeyCode.None;
}
}
}