Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Parent matches child's rotation

Discussion in 'Scripting' started by apeMike, Nov 30, 2016.

  1. apeMike

    apeMike

    Joined:
    Jan 31, 2015
    Posts:
    11
    hi,

    I'd like the parent's rotation to match the rotation of it's child at some point.

    I tried many solutions without success. My basic approach was :

    parent.transform.rotation = child.transform.rotation;

    as rotation is world based, I expected it to work but... no.

    any idea ?
     
  2. JC_SummitTech

    JC_SummitTech

    Joined:
    Nov 1, 2016
    Posts:
    78
    it should work, but they won't align.
    lets say Parent is at 5 degrees
    child is at 10 degrees
    after your operation,
    parent = 10 degrees.
    but since child is .. a child, it will be moved accordingly, so since child's local rotation is 5 degrees (relative to parent) child will now be 15 degrees, still 5 degrees more than the parent.

    to have them align, just call child.transform.localRotation = Vector3.zero immediately after your operation.
     
  3. apeMike

    apeMike

    Joined:
    Jan 31, 2015
    Posts:
    11
    I don't want them to align at the end. I just want the parent to align to the child before rotation. So in your example, the parent = 10 degrees and child = 15 degrees is exactly what I want.

    But I found another way to achieve what I want, thanks.