Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 TommyEaves2002 · Aug 08, 2015 at 09:08 AM · physicsgravityforce

Zero Gravity like Physics?

Hi, I need to find a way so that once a certain key is pressed, there is no gravity in the space around me. I am creating a 2D game which does not use Unity's built in gravity, instead I have this piece of code in the update function of each of my objects:

 if (gravityType == "up") {
     rb.AddForce (Camera.main.transform.up * 9.8f);
 } else if (gravityType == "down") {
     rb.AddForce (Camera.main.transform.up * -9.8f);
 } else if (gravityType == "null") {
     // ===== What goes here? =====
 }

This just means a negative force of 9.8 units per second will be applied on the Y axis to the object it is applied to. I want to find a way how there is no gravity in the scene, so obviously I will have to stop applying this force, so that things seem to bounce of surfaces and maintain there current velocity over longer distances. If anybody knows how to do this it would be much appreciated if you could let me know.

Many Thanks,

Tommy Eaves

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 Demonicdaron · Aug 08, 2015 at 11:03 AM

You can simply replace the 9.8f by a variable, and set that variable to zero when the key is held down. So you could change your code to:

 if (gravityType == "up") {
      myGravity = 9.8f;
  } else if (gravityType == "down") {
      myGravity = 9.8f;
  } else if (gravityType == "null") {
      myGravity = 0f;
  }
 rb.AddForce (Camera.main.transform.up * myGravity);

To do something when a button is held down use

 if (Input.GetKey())

To do something when a button is pressed down (only once when is pressed down, the action will not continue as you hold the button down) use

 if (Input.GetKeyDown())

Still, I'm not sure that your code will do what I think you want, but this should anwer the question for what you have provided. Hope this helps

Comment
Add comment · Show 3 · 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 TommyEaves2002 · Aug 08, 2015 at 06:38 PM 0
Share

Hi, thanks for the help but for some strange reason when I set myGravity to 0f my object continues to fall. I have turned off both 2D and 3D gravity in the scene under Edit/ProjectSettings/Physics and Edit/ProjectSettings/Physics2D. Any ideas how I can fix this problem?

avatar image Demonicdaron · Aug 09, 2015 at 12:41 AM 0
Share

Gravity is acceleration, not speed; therefore if you stop the acceleration, it doesn't mean the the object will stop, as it may have some velocity left. To stop it completely, you can use CharacterController.$$anonymous$$ove(Vector3.zero)

avatar image TommyEaves2002 · Aug 10, 2015 at 09:36 PM 1
Share

It's working perfectly now you have explained it, thanks.

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

24 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Problems making a glider 3 Answers

Model tips over for no reason 1 Answer

Gravitational Object Creation for 3D Game 1 Answer

Replicate physics.gravity with custom code. 2 Answers

How to make a object jump constantly at y and move to the next position to z (perfectly) 0 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