Quantcast
Channel: Questions in topic: "addforce"
Viewing all articles
Browse latest Browse all 1264

Rigidbody.AddForce seems not to do anything

$
0
0
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().mass, object2.GetComponent().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().AddForce(magnitude * direction); // Apply gravitational force on object1 towards object2. if (object1.GetComponent() == null) Debug.Log("object1 rb is null"); Debug.Log("Force on object1: (" + object1 + ") " + magnitude * direction); direction = -direction; // Invert direction of force. object1.GetComponent().AddForce(magnitude * direction); // Apply gravitational force on object2 towards object1. if (object2.GetComponent() == 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 result = new List(); 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(); List result = new List(); foreach (GameObject candidate in candidates) { if (candidate.activeInHierarchy) { result.Add(candidate); } } return result.ToArray(); } } I greatly appreciate any help!

Viewing all articles
Browse latest Browse all 1264

Trending Articles