Search Unity

Constant Force with parent space? Rigidbodies on each side of a cube falling towards to center.

Discussion in 'Physics' started by moldywood, Nov 20, 2017.

  1. moldywood

    moldywood

    Joined:
    Nov 21, 2016
    Posts:
    7
    Hello,

    I have a cube, which is actually made with 6 planes. I do it this way so each side has it's own local space and any object i child to it can use normal transform/rotation adjustments without having an offset for each side. So each child object's -y axis will fall towards its parent's side.

    I spawn each rigidbody above the actual side, so when i enter runtime, it falls down to the box. I'm using a Constant Force component to do this. I can't use Relative Force with a -y force, because if the rigidbody rotates then the y-axis also rotates and the rigidbody will go flying in whatever new direction the y-axis points. So i need to manually configure each rigidbody with a specific Force value (ie. rigidbodies on the top side will get a -y force, objects on the right side with get a -x force, etc).

    This method works fine, until the cube rotates, because the constant force is connected to the world space which doesn't rotate. So i need something in between Relative Force (local) and Force (world). I need Parent Force.

    I suppose i can always be doing math in fixedupdate() to adjust each constant force component so that it would always point towards the center even when the cube rotates, but that seems pretty heavy to do if i have a lot of rigidbodies in the scene. I was wondering if there was a cleaner way of doing this.

    I added a quick Unity project to detail this behavior in case my description is vague. [check the third post] Hold down the A key to rotate the cube, which should then send the cubes flying. The yellow cube depicts why Relative Force can't work, even when the rigidbody component is on an empty parent (although now i'm experimenting with the constraints parameters, and "freezing y rotation" seems to be behaving how i want, but i don't want to prevent rigidbody rotation from happening, and if i rotate the cube before the rigidbody actually falls to the plane/surface, then still some weird things happen).

    Thank you!
     
    Last edited: Nov 25, 2017
  2. moldywood

    moldywood

    Joined:
    Nov 21, 2016
    Posts:
    7
    this kinda of solved my problem, but also means i need a script running addForce in fixedupdate() on every rigidbody:
    https://github.com/SebLague/Spherical-Gravity

    is there any performance difference between using a script like this vs. a constant force component?
     
  3. moldywood

    moldywood

    Joined:
    Nov 21, 2016
    Posts:
    7
    I've included a modified version of the above script, and it works well except the object falls towards the center of the cube's side. This is due to the script calculating the difference between the object's transform.position and the cube side's transform.position. I instead need to keep the object's original position and just use some modified version of the difference that is calculated so it knows which relative direction down is so it goes straight down, and not down and also to the center.

    Find the updated unity project here: https://github.com/GregFinger/physCube
     
    Last edited: Nov 25, 2017
  4. moldywood

    moldywood

    Joined:
    Nov 21, 2016
    Posts:
    7
    got a working, but a bit of a ghetto solution. check my github for the unity package.

    I've reduced it down to a single script that you put on the rigidbody:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(Rigidbody))]
    6. public class gravityBody : MonoBehaviour {
    7.  
    8.     public float gravity = -10f;
    9.     Rigidbody rb;
    10.  
    11.     void Start () {
    12.  
    13.         rb = GetComponent<Rigidbody> ();
    14.         rb.useGravity = false;
    15.      
    16.     }
    17.  
    18.     void FixedUpdate () {
    19.  
    20.         rb.AddForce(rb.transform.parent.up * gravity);
    21.  
    22.         //rb.AddRelativeTorque(new Vector3(0f,-0.25f,0f));
    23.  
    24.     }
    25. }
    make sure the rigidbody is child to the collider surface/ground/side you want it to fall down towards.
     
    Last edited: Nov 26, 2017