Search Unity

Stop child gameobject rotating when parent rotates

Discussion in 'Scripting' started by piggybank1974, Oct 14, 2016.

  1. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    I've been searching the interwebs, but just cannot get anything to work.

    My gameobject is rotating on the Z plan

    Code (CSharp):
    1.    
    2. if (Input.GetKey(KeyCode.A))
    3.     transform.Rotate(NextRotationState(ref mRotationStateIndex).Degrees * Vector3.forward * Time.deltaTime);
    4.    
    5.    if (Input.GetKey(KeyCode.D))
    6.     transform.Rotate(-PreviousRotationState(ref mRotationStateIndex).Degrees * Vector3.forward * Time.deltaTime);
    7.  
    I added a script on the gameobjects that i don't want to rotate e.g

    Code (CSharp):
    1.  
    2. public class FixRotation : MonoBehaviour
    3. {
    4. Vector3 mRotation;
    5.  
    6. void Awake()
    7. {
    8. mRotation = transform.localEulerAngles;
    9. }
    10. void Update ()
    11. {
    12. transform.localEulerAngles = mRotation;
    13. }
    14.  
    15. // To make the object y axis keep pointing up:
    16. //void LateUpdate()
    17. //{
    18. // transform.eulerAngles = Vector3.zero;
    19. //}
    20.  
    21. //Quaternion mrotation;
    22.  
    23.  
    24. // void Awake()
    25. // {
    26. //  mrotation = transform.localRotation;
    27. // }
    28.  
    29. // void Update()
    30. // {
    31. // }
    32. // void LateUpdate()
    33. // {
    34. //  transform.localRotation  = mrotation;//new Vector3(0, 0, -mrotation.z);
    35. // }
    36. }
    37.  
    but nothing seem to work, might be because I'm not understand the problem :confused:
     
  2. RockyWallbanger

    RockyWallbanger

    Joined:
    Mar 16, 2014
    Posts:
    85
    You need to counter the rotation of the parent gameObject by an equal and opposite amount. Put this script on the parent gameObject and assign a reference to the child Transform you don't want to rotate in the inspector.

    Code (CSharp):
    1. public Transform child;
    2.  
    3. void Update ()
    4. {
    5. child.transform.rotation = Quaternion.Euler (0.0f, 0.0f, gameObject.transform.rotation.z * -1.0f);
    6. }
     
    Last edited: Oct 14, 2016
  3. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    Hi RockyWallbanger,

    I tried your idea, but it still does not work, they still rotate with the parent.

    I changed your script slightly to the following:

    this.transform = Child transform.
    this.transform.parent = the Childs parent transform.

    Code (CSharp):
    1.  
    2. void Update()
    3.  {
    4.   this.transform.rotation = Quaternion.Euler(0.0f, 0.0f, this.transform.parent.rotation.z * -1.0f);
    5.  }
    6.  
     
    rich-gg and Goubermouche like this.
  4. RockyWallbanger

    RockyWallbanger

    Joined:
    Mar 16, 2014
    Posts:
    85
    Interesting, my script works great for me. If you need the script on the child gameObject, I'd debug the execution of your update callbacks. You need the parent rotation to update first. If I remember correctly, unity executes its event functions in the order they're loaded, which is arbitrary.
     
  5. ISNI

    ISNI

    Joined:
    Feb 23, 2018
    Posts:
    3
    You have to use LateUpdate i think!!! :)
     
  6. FlairBot

    FlairBot

    Joined:
    Apr 18, 2018
    Posts:
    1
    If you don't NEED it to be a child, you can un-child it and add a simple follow script to it.


    public class Follow : MonoBehaviour {

    public GameObject following;
    [Range(0.0f, 1.0f)]
    public float interested; //how interested the follower is in the thing it's following :)

    void LateUpdate () {
    transform.position = Vector3.MoveTowards(transform.position, following.transform.position, interested);
    }

    }
     
  7. matthewTandy88

    matthewTandy88

    Joined:
    Jun 14, 2016
    Posts:
    2
    public Transform childObject;

    Void FixedUpdate()
    {
    Quaternion savedRotation = childObject.rotation;
    // Put parent rotation code here...
    childObject.rotation = savedRotation;
    }
     
    Radivarig and deLord like this.
  8. bugsfull

    bugsfull

    Joined:
    Apr 22, 2019
    Posts:
    3
    in order that child object would not rotate together with parent, at itself made so
    before the rotation of parent, remove child object, and then return it

    childObject.parent = null;

    // parentObject.rotate +

    childObject.transform.SetParent(parentObject.transform, true);
     
    Last edited: Sep 9, 2019
    TheDevloper likes this.
  9. vhzhang2020

    vhzhang2020

    Joined:
    Apr 27, 2020
    Posts:
    2
    Interesting, this seems to work for global rotation but not local.
     
    TheDevloper likes this.
  10. Kytra

    Kytra

    Joined:
    Aug 3, 2018
    Posts:
    3
    I just wanted to say that I am using this script of yours in version 2021.3.4f1 and it works great! Thank you
     
    Bakanovskiy95 and CipherPol like this.
  11. Yavuz_Selim

    Yavuz_Selim

    Joined:
    Apr 16, 2021
    Posts:
    11
    It's been years since first question asked but If you come from Google and searching for solution situations like this, here is my solution;


    in Update:
    Code (CSharp):
    1.  transform.rotation = Quaternion.Euler(0,0,0);

    Or, this is applying by parent gameobject to child gameobject.
    Code (CSharp):
    1. childGameObject.transform.rotation = Quaternion.Euler(0, 0, 0);


    Or maybe; let's say If parent gameobject rotating in Y axis by 50. You just need to rotate child gameobject -50 in Y axis.

    Code (CSharp):
    1. childGameObject.transform.rotation = Quaternion.Euler(0, -this.transform.rotation.y, 0);
     
    Last edited: Aug 30, 2022
    artcad likes this.
  12. piggybank1974

    piggybank1974

    Joined:
    Dec 15, 2015
    Posts:
    621
    @Yavuz_Selim

    I cannot even remember what I was doing for this, it's been so long but lets hope that helps others that see this post.
     
  13. Yavuz_Selim

    Yavuz_Selim

    Joined:
    Apr 16, 2021
    Posts:
    11
    Yeah that's why I answered. I hope you've solved easily when you first start this thread lol
     
  14. nhbarboza24

    nhbarboza24

    Joined:
    Aug 22, 2019
    Posts:
    1
    Hi guys, I tried all of your answer,
    the thing here is my parent object is marble and it has a rigidbody.
    what I did is I add a fire particles, and place it inside the marble.
    it keeps rotating, can someone explain it to me? appreciate the help. thank you!
     
  15. AfterGuard

    AfterGuard

    Joined:
    Oct 1, 2022
    Posts:
    1
    Hey all what would i do if i wanted child to only rotate on the Z axis and not on Y