Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Resolved How to Mimic Orbit of Object with Respect to Another Objects Rotation

Discussion in 'Scripting' started by jwcowand, May 24, 2024.

  1. jwcowand

    jwcowand

    Joined:
    Dec 17, 2023
    Posts:
    11
    Hello, I have been struggling with this one. I don't want to parent object B to object A, but I need object B 's position to mimic the position change it would receive from a y-axis rotation of object A, if it was parented to object A. In other words, when parented, B would orbit around object A with the offset distance that object B is away from A . I want this same functionality through c# because I can't parent object B to A for various reasons (I don't want this to occur when A is rotated on other axes). Please let me know if you need further clarification on what I am trying to accomplish. Thank you.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,922
    Why?

    Perhaps restricting yourself is unnecessary. After all you can unparent and reparent at any time.

    The simplest alternative is to have an invisible dummy game object that is parented to A, and B simply copies that object's position (and rotation if needed) every frame. Ideally in LateUpdate to avoid a possible 1-frame delay.
     
    samana1407 likes this.
  3. jwcowand

    jwcowand

    Joined:
    Dec 17, 2023
    Posts:
    11
    Because I don't want rotations on x or z axis of A to affect the position of B. Object A is constantly moving with all different rotations, so simply setting the position of B to A or anything that is a child of A will not suffice. I need specifically, only the position of where B would be based on y axis rotation of A without any other rotations of A affecting it.
     
    Last edited: May 24, 2024
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,922
    Then you could parent B to a dummy object which only copies the position of object A but not its rotation. You can set the rotation of the dummy object to whatever orbit you want, if you ever want it to not orbit parallel to the X/Z plane.
     
    jwcowand and Bunny83 like this.
  5. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,190
    Sometimes I don't understand why people abstract their problem that much and just talking about object A and B. It would help when you know the actual usecase, what those objects represent so we could propose a different approach to reach to the same result. You said object A is "constantly moving and rotating". How does it get moved / rotated? Based on what input or trigger?
     
  6. jwcowand

    jwcowand

    Joined:
    Dec 17, 2023
    Posts:
    11
    Sorry, didn't think it was necessary to get into the details. Object A is the main camera of an XRRig, thus it constantly moves and rotates based upon the headset moving and rotating. I have a Head VR Target which acts as the offset from the camera of where to put the IK Full Body Rig. Head VR Target is an empty game object NOT parented to the main camera because when you look up (Rotation on X axis) it causes the Head VR Target to swing forward and thus the IK Body swings forward (or back depending on if you look down or up). This Head VR Target is giving me issues because when I look forward in one direction the body is right where it needs to be, but when I turn around (camera rotates 180), the Head VR Target is still offset behind me. Thus I need it to follow the camera rotation on y axis so it always stays the same distance in from the camera with respect to the cameras y rotation.