- Home /
The question is answered, right answer was accepted
Input problem...
I want to add to my Custom Level Editor a special function : whenever user holds a Shift key (doesn't really matter left or right) he can more accuratly move objects (0.1 per pressing button vs 1). However, this function is not working for me... Maybe you can add your own solution and help me...
if (Input.GetKey(KeyCode.D))
{
foreach(GameObject t in selection)
{
t.transform.position += new Vector3(1, 0, 0);
}
}
else if (Input.GetKey(KeyCode.W))
{
foreach (GameObject t in selection)
{
t.transform.position += new Vector3(0, 1, 0);
}
}
else if (Input.GetKey(KeyCode.A))
{
foreach (GameObject t in selection)
{
t.transform.position += new Vector3(-1, 0, 0);
}
}
else if (Input.GetKey(KeyCode.S))
{
foreach (GameObject t in selection)
{
t.transform.position += new Vector3(0, -1, 0);
}
}
else if (Input.GetKeyDown(KeyCode.LeftShift) || Input.GetKeyDown(KeyCode.RightShift))
{
if (Input.GetKey(KeyCode.D))
{
foreach (GameObject t in selection)
{
t.transform.position += new Vector3(0.1f, 0, 0);
}
}
else if (Input.GetKey(KeyCode.W))
{
foreach (GameObject t in selection)
{
t.transform.position += new Vector3(0, 0.1f, 0);
}
}
else if (Input.GetKey(KeyCode.A))
{
foreach (GameObject t in selection)
{
t.transform.position += new Vector3(-0.1f, 0, 0);
}
}
else if (Input.GetKey(KeyCode.S))
{
foreach (GameObject t in selection)
{
t.transform.position += new Vector3(0, -0.1f, 0);
}
}
}
*Array "selection" is a holder of all objects, that were selected via special script and placed into a list of objects.
Answer by vanIvan · Sep 16, 2019 at 06:59 PM
So I figure it out by my own way : I added a new boolean called "shiftPressed" and check if Right or Left shift were pressed, than
shiftPressed = true;
And when user release one of this buttons:
shiftPressed = false;
And I move all my objects in selection depending on shiftPressed bool...
Answer by Hellium · Sep 16, 2019 at 06:30 PM
What a lot of code duplication....
float step = (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
? 0.1f : 1f;
if (Input.GetKey(KeyCode.D)) MoveSelection( Vector3.right, step );
if (Input.GetKey(KeyCode.W)) MoveSelection( Vector3.up, step );
if (Input.GetKey(KeyCode.A)) MoveSelection( Vector3.left, step );
if (Input.GetKey(KeyCode.S)) MoveSelection( Vector3.down, step );
// ...
private void MoveSelection( Vector3 direction, float step )
{
foreach (GameObject t in selection)
t.transform.position += direction * step;
}
Nope, it still doesn't work, however if I will push shift+A at one time it works. But I want to be able to hold shift, press button and than object should move at 0.1 point. If shift key is not pressed, it should move at 1 point...
The code works perfectly fine here. You may have changed Input.Get$$anonymous$$ey
by Input.Get$$anonymous$$eyDown
somewhere.