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

Get Parent to rotate like child but only in Y axis

Discussion in 'Scripting' started by Fjur, Nov 14, 2015.

  1. Fjur

    Fjur

    Joined:
    Oct 12, 2015
    Posts:
    13
    I am trying to get the parent object to rotate like the child only in the y axis, I can't seem to figure it out. The parent is moving forward in the Z axis and the child should control its rotation.

    There are no rigidbody's in the objects and i tried adding and locking in the editor but it didn't work.

    How do I lock it so it only rotates in the y axis?

    I have tried Quaternion.LookRotation but I find it is two times too slow and sometimes my camera is facing forward while my object is moving backwards. I need the parent and child to face the same way

    I have tried this code but when I rotate the child the parent starts spinning out of control in all the axis'.

    Code (csharp):
    1.  
    2. void Start () {
    3. theChild = transform.GetChild (0);
    4. theParent = transform;
    5. }
    6.  
    7. void FixedUpdate () {
    8. theParent.rotation = theChild.rotation;
    9. }
    I have also tried this but it doesn't seem to work at all
    Code (csharp):
    1.  
    2. theParent.rotation = Quaternion.Euler (newVector3 (0, theChild.rotation.y, 0));
    3.  
    This code is for a google cardboard VR app so the person walks forward the direction they are facing
     
    Last edited: Nov 14, 2015
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    The parent is going to rotate according to the change in the Y axis but then the child is parented so it too moves when the parent moves so it moves even more so it continues to accelerte and so your parent starts to spin out of control.
     
  3. Fjur

    Fjur

    Joined:
    Oct 12, 2015
    Posts:
    13
    how do i stop that then?
     
  4. dolims

    dolims

    Joined:
    Sep 13, 2014
    Posts:
    61
    Use the 'target' option of the CardboardHead to point to a game object other than the parent (grandparent, perhaps).

    Or another way to do this is to not rotate the parent but to just move it according to the direction in world space that the user is looking:

    Vector3 fwd = Camera.main.transform.forward;
    fwd.z = 0;
    parent.transform.position += speed * fwd.normalized;