- Home /
Reverse Gravity for a period of time
Hey,
I'm looking to reverse gravity for a period of time, like 2 second then reverse it back
Below is what I am using, but its not working... if it was I wouldn't be here haha. My thought is that when space is pressed the run time plus 2 is saved as startTime and gravity is flipped. When the 2 seconds has elapsed it will enter the if statement and flip gravity back. Currently it seems to just be a flip gravity button.
Also just as a side point to save posting two questions, how would I make sure if space is pressed during those 2 seconds, that it would do nothing
Thanks a lot
if (Input.GetKeyDown("space"))
{
startTime = Time.time + 2;
gravity = -gravity;
curVel.y = 0;
if (startTime == Time.time) {
gravity = -gravity;
curVel.y = 0;
}
}
Answer by fafase · Nov 16, 2012 at 03:16 PM
var notAgain =true;
if (Input.GetKeyDown("space")&& notAgain){
gravity = -gravity;
curVel.y = 0;
notAgain = false;
Invoke("Revert",2.0f);
}
function Revert(){
gravity = -gravity;
curVel.y = 0;
notAgain = true;
}
The notAgain variable ensures that you do not press twice the space bar and invoke the revert function twice. Only once the gravity is back that you can press again.
Edit: I modify the declaration of the notAgain and the function.
For a detailed explanation on timers you can check there http://unitygems.com/mistakes1/ and check the "Make something happens in the future" section
EDIT (again): just another way to write the same snippet and make you look more "Pro" :)
var notAgain = true;
if (Input.GetKeyDown("space")&& notAgain){
Revert();
Invoke("Revert",2.0f);
}
function Revert(){
gravity = -gravity;
curVel.y = 0;
notAgain = !notAgain;
}
Since you do the same thing twice you might as well make it a function. Though, I would say that slightly goes against my personal rule on function which is "Less than 5 lines of codes -> not a function". And do not try to put the Invoke in the function, you would end up in an endless recursion.
Hey, it works great apart from the disabling bit. I put the Revert function after the update function and then I have modded the other bit of code slightly because the true/false didn't seem to work... still doesn't. What am I missing?
var notAgain = "yes"; if (Input.Get$$anonymous$$eyDown("space")) { if (notAgain=="yes") { gravity = -gravity; curVel.y = 0; notAgain = "no"; Invoke("Revert", 0.4); }
if (notAgain=="no") { print("space"); } }
Fact is you seem to use UnityScript. Avoid the Yes/No thing, use boolean ins$$anonymous$$d. I correct the answer
I now have...
var notAgain = true; if (Input.Get$$anonymous$$eyDown("space")) { if (notAgain) { gravity = -gravity; curVel.y = 0; notAgain = false; Invoke("Revert", 1.0); } }
But while the character is rising, due to the reversed gravity, for 1 second (in this case)... you can still reverse the gravity back. I would like it so, when the character is rising, you can't press the button.....
Why are you not using exactly what I gave you? The first check in my case will check if you are pressing and if notAgain is true. In the case it is not true, pressing won't do anything. Though your way should work as well. Can you try with the exact same code I give and tell me what you get.
just re-did it... When my character is rising, if you press space it reverses gravity again. Its not fully disabling the button for some reason
Your answer
Follow this Question
Related Questions
Time limited actions 1 Answer
Newtonian Gravity 2 Answers
Applying force to my object has no effect. 1 Answer
I Need help with gravity 1 Answer
Rigidbody Gravity Too Slow 4 Answers