- Home /
Enable rigidbody gravity,Enable Rigid body gravity
for my school assignment I want objects with a certain tag to have rigidbody gravity enabled when a raw image with button component added to it is clicked but not before hand. I know how to make a button change scene but I cant find anything about this. Help Please.,I want to make blocks activate rigidbody gravity when a rawimage with button component is clicked. but not before hand.
Answer by Stratosome · Aug 26, 2018 at 07:35 AM
Heyo,
You can do something like this:
private Rigidbody rb;
private void Start() {
rb.useGravity = false;
rb = GetComponent<Rigidbody>();
}
public void EnableGravity() {
rb.useGravity = true;
}
The main bit will be with the EnableGravity function. If you have created one of Unity's UI buttons in your scene and you click on it, you should be able to find its Button component (in the inspector window). Inside of that component there is an OnClick event. If you click on that + button and fill in the information, you should be able to add the EnableGravity function to this OnClick event.
This was assuming that the button on each rigidbody though. If you are wanting a single button to enable gravity on a number of rigidbodies, there are a couple ways of doing it. You'll want to do something very similar to what I described above, but we will want to put something different inside of the buttons OnClick event. What you could do is find all gameobjects that have the tag you are looking for, grab their rigidbody components, and enable gravity, or you could make a list and populate it with objects yourself. It would then use this list of objects and enable gravity on those. Here is a quick example of each:
Using an array of predefined rigidbodies
// In Unity's inspector window, populate with scene objects that you want to have gravity enabled on
public Rigidbody[] objects;
// Go through each rigidbody in the array and enable gravity
public void EnableGravity() {
for (int i = 0; i < objects.Length; i++) {
objects[i].useGravity = true;
}
}
Using GameObject.FindGameObjectsWithTag
// Go through each rigidbody in the array and enable gravity
public void EnableGravity() {
// Find all objects with a specific tag
GameObject[] targets = GameObject.FindGameObjectsWithTag("myTag");
for (int i = 0; i < targets.Length; i++) {
Rigidbody rbComp = targets[i].GetComponent<Rigidbody>();
rbComp.useGravity = true;
}
}
Just a reminder that if you are using Unity's UI buttons, you will need to make sure you assign this function to the button's OnClick event. Otherwise it'll never happen. Let me know if you have further questions!
Thank you this was helpful I didn't know the script that allowed me to change things in rigidbody
Answer by Play2D · Aug 26, 2018 at 07:41 AM
Disable the gravity for rigid bodies.
Then add a 'Constant Force' component to your game objects you wish to have gravity.
When you click the button, access the Constant Force component's force parameter and give it the gravity you desire. Most likely something like this:
ConstantForce cf;
cf = gameObject.GetComponent<ConstantForce>();
if (click)
{
cf.force = new Vector3(0, -9.8f, 0);
}
Answer by eses · Aug 26, 2018 at 07:34 AM
Hi @coltanjamesshaw02
Well if this is a school assignment, you should be learning and doing research (= googling and making notes) - right? I can help a bit though:
For finding objects google for: "unity find objects with tag"
For creating UI elements from code:
https://docs.unity3d.com/Manual/HOWTO-UICreateFromScripting.html
For adding click listeners to buttons google for: "unity add button listener from code"
To get components:
https://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html
Ack, good point about it being a school assignment... Heh.
it was my $$anonymous$$cher who told me to go here because I couldn't find anything to change rigidbody with script
Your answer
Follow this Question
Related Questions
I have a planetary gravity script; how do I make the object speed up? 1 Answer
Tranform Animation Curve ruins Gravity 1 Answer
Rigidbody velocity disables gravity 1 Answer
Trying to walk on a wall 0 Answers
Calculating trajectory of Spaceship 0 Answers