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 matta9001 · Nov 30, 2013 at 08:58 PM · javascriptguirigidbodybuttongravity

Rigidbody Gravity scripting question (Javascript Answers Only!)

Okay. I'm making a simple game where you simply turn gravity on and off. So my question for you is how would i turn off gravity on the rigidbody so that i can push it around (I already got the script to make it push around i only need to know how i would turn off the gravity for many models just by the push of a gui button)

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Sundar · Nov 30, 2013 at 09:02 PM

http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody-useGravity.html

Comment
Add comment · 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
0

Answer by robertbu · Nov 30, 2013 at 09:11 PM

"how i would turn off the gravity for many models"

You need some unique property of the set of models you want to turn off or on. It could be a tag or a script that they or only they share. Or you could build a list as you create them. Here is a function that works with the tag 'Pushable':

 function SetGravity(gravity : boolean) {
     var objs = GameObject.FindGameObjectsWithTag("Pushable");
     for (var obj in objs) {
         obj.rigidbody.useGravity = gravity;
     }
 } 

 function OnGUI() {
     if (GUI.Button(Rect(0,0,100,50), "Disable")) 
         SetGravity(false);
     if (GUI.Button(Rect(0,75,100,50), "Enable")) 
         SetGravity(true);
 }

Note the above code assumes that all object tagged with 'Pushable' have a Rigidbody component? Also why do you have to turn off gravity in order to push something around. With gravity and friction an object is more difficult to push, but all you have to do is add more force.

Comment
Add comment · Show 7 · 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 matta9001 · Nov 30, 2013 at 09:30 PM 0
Share

I don't need to turn off the gravity to push things around, i'm just making a game where you control gravity. I already have a script for pushing thins around. Also the script is a bit confusing. I dont know how to attach that script with gui buttons.

avatar image matta9001 · Nov 30, 2013 at 09:34 PM 0
Share

See if you can help me with this. This is my script

var gravity = true;

function OnGUI () {

 GUI.Box (Rect (10,10,100,75),"");
 
 if (GUI.Button (Rect (20,20,80,20), "Gravity On")) {
     gravity = true;
     Debug.Log("Gravity On");
 }

 if (GUI.Button (Rect (20,50,80,20), "Gravity Off")) {
     gravity = false;
     Debug.Log("Gravity Off");
 }

} can i attach your script to $$anonymous$$e that i wrote, somehow?

avatar image Sundar · Nov 30, 2013 at 09:41 PM 0
Share

here you go

 function OnGUI () {
 
     GUI.Box (Rect (10,10,100,75),"");
      
     if (GUI.Button (Rect (20,20,80,20), "Gravity On")) {
     rigidbody.useGravity = true;
     Debug.Log("Gravity On");
     }
      
     if (GUI.Button (Rect (20,50,80,20), "Gravity Off")) {
     rigidbody.useGravity = false;
     Debug.Log("Gravity Off");
     }
 
 } 
avatar image matta9001 · Nov 30, 2013 at 10:07 PM 0
Share

Ok i probably did this wrong, but i attached this script to every object. So it works for one gameobject but not for the rest for some reason. What can i do to fix it?

avatar image robertbu · Nov 30, 2013 at 10:36 PM 0
Share

I edited the script above to show how you use GUI to enable/disable the game objects. You can attach it to a single empty game object. It expects all the game objects to be tagged the with 'Pushable'.

http://docs.unity3d.com/Documentation/Components/Tags.html

Show more comments
avatar image
0

Answer by CookieKirby · Nov 30, 2013 at 10:37 PM

I am not sure if this would halp you, but Physics.Gravity = Vector3.zero turns off gravity entirly

Comment
Add comment · Show 1 · 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 matta9001 · Nov 30, 2013 at 10:52 PM 0
Share

It would help if it turns all rigid body gravity. Can you help give me an example script or something? It would help i just want this issue solved. Or like robert said about a tag, i dont know how i would do that.

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

18 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

Related Questions

How can I attach a GUI.Button to a variable? 2 Answers

DrawTexture GUI iPhone 1 Answer

When I restart the level, everything doesn't move 2 Answers

Setting Scroll View Width GUILayout 1 Answer

GUI.Button gets clicked automatically 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