Two Rigidbodies under a parent

Hi, I’m trying to do something really simple like having two rigidbodies attached to two separate child of a parent gameobject. Under the child there are wheel colliders.

Parent
-Child (Rigidbody)
-WheelCollider
-WheelCollider
-Child (Rigidbody)
-WheelCollider
-WheelCollider

From the parent Script I declared two public variables (otherLeft and otherRight) of a type GameObject, and from the inspector I attached the two child rigidbodies. But for some reason when I do otherLeft.rigidbody.AddForce (0, 10, 0); nothing is happening.

Is this a limitation or I am doing it wrong?

Thanks
Phil

I think you have to connect all rigidbodies with joints and probably you end up with 3 rigidbodies (one for the parent too).
If they are a child of gameObject with no rigidbody they can’t move.
If you don’t need joints you can consider apply different forces to a unique rigidbody in different points, using AddForceAtPosition (eventually using empty child gameObjects as the transform point).

Ah good to know, but what I want to achieve is the object to spin, so I would need two force in opposing directions.

You can do that with AddForceAtPosition.

… or perhaps just use AddTorque

Hello Fraconte;

I tried last night using the AddForceAtPosition command, but I don’t seem to be able to add two force at the same time on the common rigidbody. It only seem to add a force in one direction. Unless I’m doing it wrong. Then I tried the addtorque and it is better. But i’m getting a little bit of unwanted rotation when I use that…

I guess I will explain better what I want to achieve. I’m building a game with a 6x6 car which has the wheels all aligned, so 3 wheels on one side and another 3 wheels on the other side. So to steer it works like a tank. That is why I wanted to have two separate Rigidbodies. To have the wheels on one side going forward and the other set reverse.

I could always rotate my transform but…

Look at this:

using UnityEngine;
using System.Collections;

public class CarForce : MonoBehaviour
{
    Vector3 center = Vector3.zero;
    Vector3 wheelOffset = new Vector3 (2, 0, 0);
    new Rigidbody rigidbody;
    new Transform transform;

    void Start ()
    {
        rigidbody = base.rigidbody;
        transform = base.transform;
        rigidbody.centerOfMass = center; //this automatically fix any unbalance in the prefab
    }
  
    void Update ()
    {
        float steer = -Input.GetAxis ("Horizontal");

        rigidbody.AddForceAtPosition (transform.forward * steer * 10, transform.TransformPoint (center + wheelOffset));
        rigidbody.AddForceAtPosition (transform.forward * -steer * 10, transform.TransformPoint (center - wheelOffset));

        //rigidbody.AddTorque (transform.up * steer * 40);
    }
}

Okay I’m getting somewhere with that.

Now the problem I’m facing is the gameobject turns and after a while it will catch air and flip in every direction. I tried to lower the center of gravity and also to increment the mass of the rigidbody.

I will try to fine-tune it again tonight but…