- Home /
While Button Pressed
Hey there,
this problem sounds pretty easy to solve, but.... I don't get it.
I want to have a kind of loop that counts up the variable "acceleration" WHILE the space bar is pressed.And them when it is not pressed anymore I want to add a relative Force to the ball.
rigidbody.AddRelativeForce (Vector3.forward * acceleration);
So how do I write a counter, which counts while a button is pressed, and then, when it isn't anymore, it does whatever I want?
(For Example the Pinball game of windows)
It tried a while loop, which crashed my game several times...
Answer by FLASHDENMARK · Apr 28, 2011 at 06:29 PM
var power = 0.0;
var forceToAdd = 5.0;
function Update () { if(Input.GetButton("Jump")) { power += forceToAdd * Time.deltaTine; }
if(Input.GetButtonUp("jump")) { rigidbody.AddRelativeForce(Vector3.forward * power); } }
Simple, adds to the power variable when the Jump button is down. When it is released we add force to the rigidbody.
Let me know if there are any errors :)
Answer by letmeknowit · Mar 08, 2012 at 12:15 AM
oh well there is small error
there is: power += forceToAdd * Time.deltaTine;
should be: power += forceToAdd * Time.deltaTime;
Time not Tine.
Please, Post comments as comments, not as answers. Thanks.
Answer by DrOctagonapus · Apr 29, 2011 at 10:43 PM
no errors, worked well :) , thx a lot
Please post as comments in the comments bit, not as answers.
Your answer
Follow this Question
Related Questions
Using for as while. 3 Answers
Method containing while loop in custom class? 1 Answer
While loop questions 3 Answers
while loop issue 1 Answer