- Home /
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)
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.
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.
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?
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");
}
}
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?
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'.
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
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
Follow this Question
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