Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 coltanjamesshaw02 · Aug 26, 2018 at 06:50 AM · rigidbodygravitybutton trigger events

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.

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

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!

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 coltanjamesshaw02 · Aug 26, 2018 at 11:15 PM 0
Share

Thank you this was helpful I didn't know the script that allowed me to change things in rigidbody

avatar image
1

Answer by Play2D · Aug 26, 2018 at 07:41 AM

Disable the gravity for rigid bodies. alt text


Then add a 'Constant Force' component to your game objects you wish to have gravity. alt text


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);
 }



disablerigid.png (7.2 kB)
cforce.png (6.1 kB)
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 coltanjamesshaw02 · Aug 26, 2018 at 11:18 PM 0
Share

thank you very much

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

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 Stratosome · Aug 26, 2018 at 07:37 AM 0
Share

Ack, good point about it being a school assignment... Heh.

avatar image coltanjamesshaw02 · Aug 26, 2018 at 11:17 PM 0
Share

it was my $$anonymous$$cher who told me to go here because I couldn't find anything to change rigidbody with script

avatar image Stratosome coltanjamesshaw02 · Aug 26, 2018 at 11:46 PM 0
Share

Fair enough, that's reasonable. :P

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

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

Related Questions

Make A Cube 'Sticky' 4 Answers

Rigidbody AddForce/Velocity & gravity - mysterious interference 1 Answer

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


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