Search Unity

Child transform's position not updated as parent moves

Discussion in 'Scripting' started by MiaoBolverk, Apr 12, 2018.

  1. MiaoBolverk

    MiaoBolverk

    Joined:
    Feb 6, 2018
    Posts:
    26
    I have a parent game object Object1. It has a child, which is simply an empty game object called ReferencePoint. ReferencePoint can be any arbitrary point on Object1 itself.

    I have another game object Object2. It has a public field called RotationReference, of type Transform. In the inspector, the ReferencePoint on Object1 is assigned to the RotationReference field on Object2.

    As Object1 moves, I expect ReferencePoint to change its position as well. Object2 is supposed to change its rotation to face its RotationReference -- in this case, the ReferencePoint on Object1:

    Code (csharp):
    1.  
    2.     public override void Rotate()
    3.     {
    4.         Vector3 relativeRotationReferencePosition =
    5.             this.RotationReference.position - this.transform.position;
    6.        
    7.         this.transform.rotation = Quaternion.LookRotation(relativeRotationReferencePosition);
    8.     }
    9.  
    However, I noticed that Object2 is not rotating at all, even when Object1 is moving. When I step through the call stack multiple times, I discover that this.RotationReference.position always has the same value, even though Object1 is moving.

    What am I missing here?
     
  2. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,243
    How often do you call Rotate()?
     
  3. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
  4. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089
    That looks like it would work to me, might be worth changing 'RotationReference' to a gameObject and using RotationReference.transform just to make sure it's definitely getting the right values.

    If you want to upload a basic scene with the problem visible I can have a look.