- Home /
How to make rigidbodies on each side of a cube fall towards the cube? [multiple gravity / addForce]
I have a world cube (as opposed to a sphere), and on each of the six sides i have rigidbodies that need to fall towards it's relative side/ground. ConstantForce's Force parameter won't work because the world cube will need to rotate, and when that rotates, all the rigidbodies global rotation axis also changes. The Relative Force parameter won't work either because the rigidbodies themselves can also rotate independently, so their local rotation axis can change.
So i figure some type of script that runs AddForce and adjusts the vector based on position and rotation of the rigidbody in relation to it's side would be the solution. I've posted in the forums about it (forum post) but haven't received any replies.
My current solution works but i feel there must be a better way. You can find my current unity project at my Github with the scene set up with the cubes, but i'll also post the script here. The rigidbody must be child to the collider surface/ground/side it should fall down towards.
rigidbody using System.Collections; using System.Collections.Generic; using UnityEngine;
[RequireComponent(typeof(Rigidbody))]
public class gravityBody : MonoBehaviour {
public float gravity = -10f;
Rigidbody rb;
void Start () {
rb = GetComponent<Rigidbody> ();
rb.useGravity = false;
}
void FixedUpdate () {
rb.AddForce(rb.transform.parent.up * gravity);
//rb.AddRelativeTorque(new Vector3(0f,-0.25f,0f));
}
}
thank you.
To clarify, you want them to fall down on the faces?
For example even if the object was close to the corner, it wouldn't slide/fall toward the center of the face?
Correct, it should fall straight down. So my current solution, since the object can potentially rotate and change it's local y axis, is it uses its parent's (the ground beneath it) local y axis. The ground beneath it never independently rotates, so the Y axis well stay the same (going straight through the "world cube" origin).
Your answer
Follow this Question
Related Questions
How can I match Unity's gravity implementation per object? 1 Answer
Physics in 2D mode not working? 0 Answers
Setting a RigidBody's velocity messes with my custom gravity, not sure how to proceed. 0 Answers
How To Set Individual Rigidbody Gravity [Solved] 3 Answers
add force to object that has 2 different rigid bodies 0 Answers