x


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...

more ▼

asked Apr 28 '11 at 06:18 PM

DrOctagonapus gravatar image

DrOctagonapus
14 2 2 2

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first
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 :)

more ▼

answered Apr 28 '11 at 06:29 PM

OrangeLightning gravatar image

OrangeLightning
5.4k 47 57 113

(comments are locked)
10|3000 characters needed characters left

no errors, worked well :) , thx a lot

more ▼

answered Apr 29 '11 at 10:43 PM

DrOctagonapus gravatar image

DrOctagonapus
14 2 2 2

Please post as comments in the comments bit, not as answers.

Apr 29 '11 at 11:28 PM Fishman92
(comments are locked)
10|3000 characters needed characters left

oh well there is small error

there is: power += forceToAdd * Time.deltaTine;

should be: power += forceToAdd * Time.deltaTime;

Time not Tine.

more ▼

answered Mar 08 '12 at 12:15 AM

letmeknowit gravatar image

letmeknowit
6 2 2 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x304
x62
x55
x12

asked: Apr 28 '11 at 06:18 PM

Seen: 2383 times

Last Updated: Mar 08 '12 at 12:15 AM