Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ZachRyall · Nov 16, 2012 at 02:56 PM · gravitytimer

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; 
         }
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 15 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ZachRyall · Nov 16, 2012 at 04:15 PM 0
Share

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"); } }

avatar image fafase · Nov 16, 2012 at 04:22 PM 0
Share

Fact is you seem to use UnityScript. Avoid the Yes/No thing, use boolean ins$$anonymous$$d. I correct the answer

avatar image ZachRyall · Nov 16, 2012 at 04:32 PM 0
Share

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

avatar image fafase · Nov 16, 2012 at 04:41 PM 0
Share

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.

avatar image ZachRyall · Nov 16, 2012 at 04:46 PM 0
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges