Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 eni_cay · Jun 20, 2021 at 04:50 PM · rigidbody2dgravityoverlap

control object rigidbody individualy ?

i have two objects that use rigidbody , and 1 onbject has certain gravity twists it falls left and right , so when object 1 falls to the left/right , object 2 falls accordingly which is not wanted , so how can set this sideway gravity to control one object only ? like every object that uses rigidbody will be influenced by the gravity of object 1 heres a sample code :

   if (transform.position.x > 0)
             {
                 Physics2D.gravity = new Vector2(10f, 0);
 
             }
             else if (transform.position.x < 0)
             {
                 Physics2D.gravity = new Vector2(-10f, 0);
 
             }
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
0
Best Answer

Answer by DenisIsDenis · Jun 21, 2021 at 05:03 AM

In order not to change the gravity for all objects at once, it is necessary to implement your gravity for each object separately. This script does this:

 using UnityEngine;
 
 [RequireComponent(typeof(Rigidbody2D))]
 public class YourGravity : MonoBehaviour
 {
     // gravity when the object is in the positive x-axis
     public Vector2 gravity = Vector2.left * 9.81f;
     Rigidbody2D selfRigidbody2D;
 
     void Start()
     {
         // get the physical component
         selfRigidbody2D = GetComponent<Rigidbody2D>();
 
         // disable built-in gravity
         selfRigidbody2D.gravityScale = 0;
 
         // to make our gravity look smooth, turn on interpolation.
         selfRigidbody2D.interpolation = RigidbodyInterpolation2D.Interpolate;
     }
 
     void FixedUpdate()
     {
         if (transform.position.x > 0)
         {
             selfRigidbody2D.velocity += gravity * Time.fixedDeltaTime;
 
         }
         else if (transform.position.x < 0)
         {
             selfRigidbody2D.velocity -= gravity * Time.fixedDeltaTime;
         }
     }
 }

EDITED:

If you need to change the gravity for one object when you release the left mouse button, then the script must be changed:

 using UnityEngine;
 
 [RequireComponent(typeof(Rigidbody2D))]
 public class YourGravity : MonoBehaviour
 {
     // gravity when the object is in the positive x-axis
     public Vector2 gravity = Vector2.left * 9.81f;
     Rigidbody2D selfRigidbody2D;
 
     bool isClickedOnThisObject;
 
     void Start()
     {
         // get the physical component
         selfRigidbody2D = GetComponent<Rigidbody2D>();
 
         // disable built-in gravity
         selfRigidbody2D.gravityScale = 0;
 
         // to make our gravity look smooth, turn on interpolation.
         selfRigidbody2D.interpolation = RigidbodyInterpolation2D.Interpolate;
     }
 
     private void Update()
     {
         // Processing of keystrokes and mouse clicks
         // should always be done in the Update () function.
 
         //In this condition, we check if the player pressed
         //the given object and released the left mouse button.
         //If yes, then we change the gravity of this object.
 
         if (isClickedOnThisObject && Input.GetMouseButtonUp(0))
         {
             isClickedOnThisObject = false;
             gravity *= -1;
         }
     }
 
     void FixedUpdate()
     {
         // Physical calculations should always be done
         // in the FixedUpdate() function.
 
         selfRigidbody2D.velocity += gravity * Time.fixedDeltaTime;
     }
 
     private void OnMouseDown()
     {
         // Here we use the OnMouseDown() function, because
         // the OnMouseUp() function is called on all objects,
         // regardless of the location of the cursor.
 
         isClickedOnThisObject = true;
     }
 }
Comment
Add comment · Show 2 · 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 eni_cay · Jun 21, 2021 at 06:41 PM 0
Share

oh thank you a lot , just simple question , i have the lines that are in fixedupdate on OnMouseUp function , so is it okay if i added those line in OnMouseup instead of fixedupdate ?

avatar image DenisIsDenis eni_cay · Jun 22, 2021 at 03:44 AM 0
Share

I completed the answer, @eni_cay

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

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

Related Questions

How to change the gravity scale of the rigidbody 2d back to it's default value after player lands to the ground from jump 1 Answer

Change GravityScale in runtime 2 Answers

Gravity not working in a sidescrolling 2d game after upgrade to Unity 5 2 Answers

Flickering issue on Rigidbody 0 Answers

Gravity in new scene is too slow 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