Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Jan 21, 2019 at 04:17 PM by unity_ei8IdEWWf5xzRQ for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by unity_ei8IdEWWf5xzRQ · Jan 21, 2019 at 02:13 PM · rigidbodygravityaddforcelayersforces

Rigidbody.AddForce seems not to do anything

Hello everyone! I am trying to write a script for exerting gravity on all objects on a certain layer in every FixedUpdate. The code I have written can be seen below.


In the testing scene I am working on I have two objects on the layer "GravityObjects" called Sphere1 and Sphere2, both with non-kinematic, non-gravity Rigidbody with a mass of 1. Sphere1 is centered in the origin, and Sphere2 is placed on (2, 0, 0). They are both unit spheres with no rotation.


The spheres refuse to move despite me getting messages in the console saying what forces are applied to what objects and what the directions are. Trying to move the spheres independently of this script using the exact same forces and works as expected.


I have tried to debug everything, as can be told by the debugging code, but I honestly cannot see why the objects are not moving. I think the problem is with AddForce, but I am not sure at all.


Here is the code:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GravityManager : MonoBehaviour {
 
     public float gravityModifier = 1;
 
     const double GRAVITY_CONSTANT = 0.0000000000667408;
     GameObject[] gravityObjects;
 
     void Start() {
         updateList();
         InvokeRepeating("updateList", 1.0f, 1.0f);
     }
 
     void FixedUpdate() {
         exertGravity();
     }
 
     void updateList() {
         gravityObjects = FindGameObjectsOnLayer("GravityObjects");
     }
 
     void exertGravity() {
         for (int i = 0; i < gravityObjects.Length; i++) {
             for (int j = i + 1; j < gravityObjects.Length; j++) {
                 if (gravityObjects[i] != null && gravityObjects[j] != null && i != j) {
                     GameObject object1 = gravityObjects[i];
                     GameObject object2 = gravityObjects[j];
                     Vector3 position1 = object1.transform.position;
                     Vector3 position2 = object2.transform.position;
 
                     float magnitude = calculateGravityNewton(object1.GetComponent<Rigidbody>().mass, object2.GetComponent<Rigidbody>().mass, Vector3.Distance(position1, position2)) * gravityModifier * Time.fixedDeltaTime;
 
                     Vector3 direction = (position2 - position1).normalized; // From position1 to position2.
                     Debug.Log("Direction from object1 to object 2: " + direction);
 
                     object1.GetComponent<Rigidbody>().AddForce(magnitude * direction); // Apply gravitational force on object1 towards object2.
                     if (object1.GetComponent<Rigidbody>() == null) Debug.Log("object1 rb is null");
                     Debug.Log("Force on object1: (" + object1 + ") " + magnitude * direction);
 
                     direction = -direction; // Invert direction of force.
                     object1.GetComponent<Rigidbody>().AddForce(magnitude * direction); // Apply gravitational force on object2 towards object1.
                     if (object2.GetComponent<Rigidbody>() == null) Debug.Log("object2 rb is null");
                     Debug.Log("Force on object2: (" + object2 + ") " + magnitude * direction);
                 }
             }
         }
     }
 
     float calculateGravityNewton(float m1, float m2, float r) {
         return (float) GRAVITY_CONSTANT * (m1 * m2) / (r * r);
     }
 
 // Returns an array of GameObjects that are on the layer which name was specified by the argument. Returns null if no such layer was found.
     GameObject[] FindGameObjectsOnLayer(string layerName) {
         int layer = LayerMask.NameToLayer(layerName);
 
         if (layer > -1) {
             GameObject[] candidates = GetAllGameObjectsInScene();
             List<GameObject> result = new List<GameObject>();
 
             foreach (GameObject candidate in candidates) {
                 if (candidate.layer == layer) {
                     result.Add(candidate);
                 }
             }
 
             return result.ToArray();
         }
 
         return null;
     }
 
     // Returns an array of all GameObjects that are active in the Scene Hierarchy.
     GameObject[] GetAllGameObjectsInScene() {
         GameObject[] candidates = FindObjectsOfType<GameObject>();
         List<GameObject> result = new List<GameObject>();
 
         foreach (GameObject candidate in candidates) {
             if (candidate.activeInHierarchy) {
                 result.Add(candidate);
             }
         }
 
         return result.ToArray();
     }
 }
 

I greatly appreciate any help!

Comment
Add comment · Show 2
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 xxmariofer · Jan 21, 2019 at 02:19 PM 0
Share

Can you log rigidbody.veloctys? also can you set angular drag and drag to 0 just in case they have really little force applyed and they are being stopped? both objects rigidbody are marked as simulated?

avatar image unity_ei8IdEWWf5xzRQ xxmariofer · Jan 21, 2019 at 04:16 PM 0
Share

Oh my god, I figured out what the problem was. I am applying the two opposing forces to the same object, so one object is completely unaffected and the other one gets forces on it that cancel each other out. This has taken me weeks, and it was such a tiny error all along!

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

204 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 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

let rigidbody idle around in space 1 Answer

Rigidbody doesn't seem to apply gravity 1 Answer

How to make GameObject fall with the same gravity while using AddForce() 1 Answer

Disable gravity (freeze character in mid air) without him flying into space? 1 Answer

Replicate Krunker slide hop movement 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