Search code examples
unity-game-enginerotationfreeze

How to stop rotating sub child in u nity 3d


I have three game objects A, B and C. A is the parent of B and B is the parent of C. A is rigidbody and main moving object. B is non rigid transform object which rotates from 0,0,0 to 0,0,45 constantly. I want that when When B rotates the B's child C also rotate but its local y axis must be 0 (locked). For example each wiper of old school buses have also a sub wiper when main wiper moves, the sub wiper also moves bus its axis remains vertical. Thats what i want. Plz any body help...

enter image description here


Solution

  • The simplest solution is to simply have the C object update its rotation so that transform.up is always the same value.

    Vector3 upDir;
    
    void Awake() 
    {
        upDir = transform.up;
    }
    
    void Update()
    {
        transform.up = upDir;
    }
    

    If you're interested in keeping its rotation the same as the grandparent, just set its rotation to be equal to that of the grandparent:

    transform aTransform;
    
    void Awake()
    {
        aTransform = transform.parent.parent;
    }
    
    void Update()
    {
        transform.rotation = aTransform.rotation;
    }