Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Rotating Child Object Independently Without It Being Affected By Parent?

Discussion in 'Scripting' started by Jeepster, Apr 10, 2019.

  1. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    132
    Is it possible to make a child object rotate independently of the parents rotation and keep the parents position?

    In my scene the parent/player, with a rigidbody on it, has a mouselook script that allows me to see wherever I point the mouse. When the player hits a "gift box" a spinning wheel, that is actually a child object, is enabled/activated and starts rotating on the Y-axis using add force and it's own rigidbody.

    But when I look around while the wheel spins, it gets all messed up and spins in every-which direction. No constrains have worked to solve the problem. I have found 0 solutions in the forums. HEELP!!!!

    I can't simply instantiate the wheel and not have it as a child object, because it is part of a much larger coordinated set of functions and objects that are instantiated when the level is loaded (multiplayer game). But there must be a solution to this frustrating issue. I would greatly appreciate the help.

    It's such a small thing: Make a 3D object wheel spin and stay in front of the camera as though it was a GUI, and stay in place regardless of the mouselook script...Can anybody help me fix this?
     
  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    293
    If you can't have your wheel out of player hierarchy, then you need to compensate parent's rotation. In late update, after all animations and effects and controls applied, set the wheel global rotation to desired value to override parent rotation:

    Code (CSharp):
    1. private void LateUpdate(){
    2.     wheel.transform.rotation = Quaternion.euler(Mathf.Sin(Mathf.Deg2Rad * Time.time), 0, 0);
    3. }
     
    Jeepster likes this.
  3. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    132
    Thank you so much for your reply. I tried applying the code to the camera that has the mouselook script on it and then add public GameObject wheel; But it seems to override the rotation force on the wheel and turn it on it's side. I tried Mathf.Sin(Mathf.Deg2Rad * Time.time on all axis and it didn't work, but I'm sure the counteracting principle is the key.

    I should've posted my mouselook code. Here it is:
    Code (CSharp):
    1. rotationX += Input.GetAxis("Mouse X") * lookSpeed;
    2.         rotationY += Input.GetAxis("Mouse Y") * lookSpeed;
    3.  
    4.         transform.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
    5.         transform.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);
    This is what needs to be counter acted. I hope the wheel rotation:
    Code (CSharp):
    1.     Vector3 torque;
    2.         torque.y = Random.Range(-200, 200);
    3.         //gO.AddRelativeTorque(0f, 2000f, 0f, ForceMode.Impulse);
    4.         gO.AddTorque(Vector3.left * torque.y, ForceMode.Impulse);
    -will still work.
     
  4. Jeepster

    Jeepster

    Joined:
    Jan 23, 2014
    Posts:
    132
    Do you how to implement the counter rotation when its input.GetAxis and transform local rotation that is used as mouselook by any chance? I'm so stuck :/