The question is answered, right answer was accepted
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!
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?
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!
Follow this Question
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