- Home /
Trying to move object consantly after button is pressed
I am trying to "set the gameObject loose" when i press the Space bar. I cant seem to find any help using Google. Here is my code so far:
private bool hasStarted = false;
public int Speed = 100;
if(Input.GetKey(KeyCode.Space) && (hasStarted == false))
{
transform.Translate(moveDirection * Speed * Time.deltaTime);
}
It is being run in update, and as i said above, i'd like to have it move constantly after Space has been pressed once, while not being held down
What is the "Speed" object you're using in the Translate call? You probably want "Speed" to be a float variable you've defined/set previously. Its first letter is capital, which means it's probably some class or static reference maybe, and won't compile? Is that the problem you're having?
You're testing when space is down AND when hasStarted is false, but since hasStarted is never changed, it basically only tests when space is down, but doesn't keep moving. Answer forthco$$anonymous$$g...
Yes sorry, forgot to include the Speed. It is a public int which i just use to define its speed. Will comment when i ahve tried your answer below.
EDIT: Thank you very much, it worked :-)
Sure thing! Please consider marking the answer below as correct if you consider this question resolved.
Answer by cagezero · May 08, 2013 at 07:55 PM
Try this:
private bool hasStarted = false;
if(Input.GetKey(KeyCode.Space) || (hasStarted == true))
{
transform.Translate(moveDirection * Speed * Time.deltaTime);
hasStarted = true;
}
Your answer
Follow this Question
Related Questions
2D Character controller that moves in increments. 1 Answer
GUI.Button press (not click) 3 Answers
Moving left and right touch problem 0 Answers
OnGUI: += and -= operations problem 2 Answers