- Home /
having multiple inputs using getkey
I have code that allows me to move a 2D box using the arrow keys and accomplished this via getkey. Im now need to be able tocode the box so that when the spacebar is held it changes size, is this possible?
UPDATE - i will post what code i have later tonight as unable to grab it.
Pretty much, i have a 2d cude that i can move using arrow keys, when the spacebar is held, i want to be able to transform the scale of the cube with the arrow keys.
Example - If the spacebar is held and arrow key up is being held/pushed, the box increase upwards in height and increase downwards in height if the spacebar is held and the down arrow key is held/pushed.
if the spacebar is held and the arrow keys left and right are pushed/held the box must increase in width.
UPDATE 2 - Thanks guys i've managed todo it! One last thing would be can anyone hint me in the direction of how to stop the increase and decrease at a certain point?
I.E up arrow is to increase in height and down arrow is to decrease in height, until when you decrease the box too much, it starts to flip and increase again? is there anyway to stop this so once the box hits its smallest value it cant flip and start growing in the opposite direction?
THANKS !
using UnityEngine;
using System.Collections;
public class playermovement : MonoBehaviour
{
public float speed = 1f;
public Color newColor;
void Start ()
{
}
void Update ()
{
if (Input.GetKey (KeyCode.RightArrow))
transform.position += new Vector3 (speed * Time.deltaTime, 0.0f, 0.0f);
if (Input.GetKey (KeyCode.LeftArrow))
transform.position -= new Vector3 (speed * Time.deltaTime, 0.0f, 0.0f);
if (Input.GetKey (KeyCode.UpArrow))
transform.position += new Vector3 (0.0f, speed * Time.deltaTime, 0.0f);
if (Input.GetKey (KeyCode.DownArrow))
transform.position -= new Vector3 (0.0f, speed * Time.deltaTime, 0.0f);
if (Input.GetKey (KeyCode.Return))
newColor = new Color(Random.value, Random.value, Random.value);
renderer.material.color = newColor;
if (Input.GetKey (KeyCode.Space))
{
if (Input.GetKey (KeyCode.RightArrow))
{
transform.localScale += new Vector3(0.05f,0,0);
}
}
if (Input.GetKey (KeyCode.Space))
{
if (Input.GetKey (KeyCode.LeftArrow))
{
transform.localScale -= new Vector3(0.05f,0,0);
}
}
if (Input.GetKey (KeyCode.Space))
{
if (Input.GetKey (KeyCode.DownArrow))
{
transform.localScale -= new Vector3(0,0.05f,0);
}
}
if (Input.GetKey (KeyCode.Space))
{
if (Input.GetKey (KeyCode.UpArrow))
{
transform.localScale += new Vector3(0,0.05f,0);
}
}
}
}
Yes, that's possible. ...if you need help with how, you should make your question a bit mroe specific.
As Siaran said, 'Yes' is about the best answer you can get unless you provide a few specifics:
What is your existing code?
When you press spacebar what exactly do you want to happen? Do you want the box to be bigger while the button is held and revert back to normal after? Do you want the box to grow bigger every time you press space?
Hello all, i will post what code i have later tonight as unable to grab it.
Pretty much, i have a 2d cude that i can move using arrow keys, when the spacebar is held, i want to be able to transform the scale of the cube with the arrow keys.
Example - If the spacebar is held and arrow key vup is being held/pushed, the box increase upwards in height and increase downwards in height if the spacebar is held and the down arrow key is held/pushed.
if the spacebar is held and the arrow keys left and right are pushed/held the box must increase in width.
Answer by ForeignGod · Mar 16, 2015 at 09:34 AM
Your question is very open, but i suppose you look for something like this.
http://docs.unity3d.com/ScriptReference/Transform-localScale.html
Take a look at this and try to use it with GetKey. I wont write the whole script for you, but you can learn alot from here. :)
EDIT: "As you updated your question"
Make an if statement inside your if statement
if(Input.GetKeyDown(KeyCode.W))
{
if(Input.GetKeyDown(KeyCode.Space))
{
//Do something if both are pressed.
}
}
Not tested just an example on how to do it.
Your answer

Follow this Question
Related Questions
Word Wrapping Gui.Box 1 Answer
Pushing and Pulling an Object. 1 Answer
Countdown remove gui.box 2 Answers
how to make scroll box in the combo box? 1 Answer
full screen wide GUI.Box 3 Answers