Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 EtherealPineapple · Apr 24, 2016 at 07:31 AM · c#scripting problemtimegravity

C# | How to disable gravity for a certain amount of time?

Okay, so I'm making my own jumping script ( a very simple one), and I need to disable the gravity feature for like maybe 0.5 or 1 second for it to work. I just don't know how to do it. Also, I'm a beginner so please explain in depth what's happening in the code so that I can understand.

      void Update () {
                 if (Input.GetKey (KeyCode.Space)) 
                 {
                     transform.Translate (Vector3.up * 5 * Time.deltaTime);
                 }
 }
 

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
1
Best Answer

Answer by Scribe · Apr 24, 2016 at 04:20 PM

This seems like a strange way to implement a jump, normally gravity doesn't disappear as you leap into the air! But if that is what you want to do, use a coroutine. A coroutine just allows you to run a second lot of code 'parallel' to the other code being executed, rather than the usual, one method, then the next. Note that the code here doesn't run in a new thread, it simply 'saves' its state at the end of each frame, and continues where it left off in the next one:

 bool isJumping = false;
 Rigidbody rb;
 
 void Update(){
     //some code
 
     if(someAction && !isJumping){ //if someAction was performed and we are not already jumping, start!
         StartCoroutine(Jump(rb));
     }
 }
 
 IEnumerator Jump(Rigidbody rigidbody){
     isJumping = true; //make sure we can't jump whilst already doing so!
     bool oldGravity = rigidbody.useGravity; //save our old gravity
     rigidbody.useGravity= false; //set gravity to 'not used'
 
     float timer = 0.5f //set the time for which to hold gravity off
 
     while(timer >= 0){
         //do some jumping stuff
 
         timer -= Time.deltaTime; //minus the time that the frame took, from our timer i.e. count down
         yield return null; //move to the next frame
     }
 
     //our time is up
     rigidbody.useGravity = oldGravity; //set gravity back to normal
 
     //might want to wait until we hit the floor again or something, before allowing ourselves to jump again
     isJumping = false;
 }

Using Translate for movement with when physics is involved is generally a bad idea. Secondly, turning off gravity, will effect ever rigidbody's gravity, not just the player that is jumping... more design floors, best just to use AddForce or similar, or set rigidbody.velocity over the course of a few frames if you really want a set ascent speed.

Anyway... you have been warned!

Comment
Add comment · Show 6 · 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 colruytXD · Apr 24, 2016 at 05:11 PM 0
Share

for your 0.5 second requirement you will need a Coroutine

avatar image EtherealPineapple · Apr 24, 2016 at 05:15 PM 0
Share

Thank you very much, but also is there a better, more efficient way of doing it? Thanks.

avatar image Scribe EtherealPineapple · Apr 27, 2016 at 09:43 AM 0
Share

I suggested some alternatives at the bottom of my answer, for a similar effect, you simply set the rigidbodys velocity in a coroutine, and use

 yield return new WaitForFixedUpdate();

ins$$anonymous$$d of yield return null, so that the physics changes are correctly implemented.

avatar image Eno-Khaon EtherealPineapple · Apr 27, 2016 at 10:12 AM 0
Share

As another potential option, if you're looking to jump up to a specific height rather than jump for a fixed length of time (or it can be both, really), you can calculate the physics force required to reach that height using:

 float gravityStrength = Physics.gravity.magnitude; // Usually 9.81
 float jumpHeight = 5.0f; // How many meters/units do you want to jump up?
 float jumpForce = $$anonymous$$athf.Sqrt(2 * gravityStrength * jumpHeight);
avatar image meat5000 ♦ · Apr 28, 2016 at 07:22 AM 1
Share

If you use Physics.gravity doesn't everything 'jump'? Surely you want to disable Use Gravity on the rigidbody.

Strange jump technique :P

avatar image Scribe meat5000 ♦ · Apr 29, 2016 at 09:50 AM 0
Share

indeed, I should probably change that, I didn't really think, the whole idea seemed strange. I'll edit now! Could make for quite fun gameplay however!

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

8 People are following this question.

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

Related Questions

rigidbody.Use gravity not working from script 0 Answers

Gravity Switch Touch 1 Answer

How to fix initial gravity problem? 1 Answer

time dilation & gravity Effect Not working propperly 1 Answer

Multiple Cars not working 1 Answer


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