Search Unity

Is it really impossible to have a child object rotate independently of the parent in UNITY?

Discussion in 'Physics' started by Jeepster, Apr 23, 2019.

  1. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    401
    Hi,

    So over the past few months I've encountered a seriously frustrating problem: I've made spinning wheel (3D object) that is a child of my camera which has a mouselook script on it. When the wheel is NOT a child object, it spins perfectly and upright, but as a child object, it's rotation is affected by the parent.

    This is the mouselook code on the camera:

    1. rotationX += Input.GetAxis("Mouse X") * lookSpeed;
    2. rotationY += Input.GetAxis("Mouse Y") * lookSpeed;

    3. transform.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
    4. transform.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);
    The wheel spins in relation to where and how fast I look around with the camera instead of simply staying in front of the camera and spinning on it's own y axis. I'm using AddTorque and I've tried with adding the torque using Vector3 or it's transform and it's the same result!! I'm going crazy over here. How come it's completely impossible to do something so painfully simple in Unity??? If anybody knows how to make something like this work please let me know!!!

    here's the spin wheel code:

    Code (CSharp):
    1.   void Awake()
    2.     {
    3.         gO = GetComponent<Rigidbody>();
    4.         gO.angularDrag = Random.Range(0f, 1.5f);
    5.         gO.AddTorque(transform.up * torque, ForceMode.Impulse  );
    6.  
    7.    
    8.     }
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Don't parent it and have a bit of logic tracking the wanted location.
     
  3. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    467
    As soon as you add a Rigidbody to a game object, its transform is controlled by the Physx engine, where the parent transform just doesn’t mean anything.

    You might get it to work if you add a rigid body to your camera too and create a HingeJoint between the spinning wheel and the camera rigidibody.

    Instead of setting the transform.localrotation on the camera, use MoveRotation on the rigidbody of the camera.

    I hope I could help!
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,433
    Having children follow the rotation and position of the parent is nearly the entire purpose of parent/child relationships in the scene hierarchy.
     
    SparrowGS likes this.
  5. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    467
    I just tested what I wrote earlier and I'm afraid I was wrong. The proposed solution might still work, though.