x


Toggling something (namely, gravity)

Hey. I want the player to be able to toggle if he should fly or be grounded. So when(for example) the player presses "f" then he can fly and when he presses it again he stops. How would I put that in a simple line of code ?

more ▼

asked Mar 21 '12 at 10:33 PM

vipvex gravatar image

vipvex
29 16 27 30

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

2 answers: sort voted first

Which script are you currently using? I've modded the FPSWalker script of days gone by to turn gravity off, if that's any clue. Look in your code where gravity is applied. Make a 'flying' var (boolean) that when it's 'true' you don't apply gravity. And have Update look for the 'f' key to toggle that 'flying' boolean.

var flying = false;

function Update()
{
  if (Input.GetKeyDown("F"))
   flying = !flying;
}
more ▼

answered Mar 21 '12 at 10:44 PM

DaveA gravatar image

DaveA
26.8k 153 171 257

Edited to show code how user can toggle it.

Mar 22 '12 at 01:39 AM DaveA
(comments are locked)
10|3000 characters needed characters left

What i want is for the player to be able to togle it on or off.

more ▼

answered Mar 22 '12 at 01:37 AM

vipvex gravatar image

vipvex
29 16 27 30

Please use the 'add new comment' under the answer. Answering your own question with a comment is not helpful.

Mar 22 '12 at 01:38 AM DaveA
(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:

x819
x169
x103

asked: Mar 21 '12 at 10:33 PM

Seen: 475 times

Last Updated: Mar 22 '12 at 01:39 AM