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

Question Child rotation problem

Discussion in 'Scripting' started by Grzp, Jan 18, 2023.

  1. Grzp

    Grzp

    Joined:
    Jan 25, 2018
    Posts:
    31
    Hi everyone,
    I have a problem with child rotation problem. So I have an player, and player can rotate in 2d scene, in all directions, and this player has a child and this child has another child- this is the oxygen bar. And I want to rotate a player and it's not a problem, but I want to keep the seme rotation (in relative to world space) for child which keep the oxygen bar and also keep same positions. Like in images:
    upload_2023-1-18_12-15-27.png
    upload_2023-1-18_12-17-2.png
    upload_2023-1-18_12-18-34.png
    upload_2023-1-18_12-19-21.png - that's the hierarchy in prefab.
    How can I do it?
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,938
    Usual technique for this is to move it onto a separate game object that's not a child. Have said game object follow the position of your little Dwarf via code (offset some as well perhaps), so it will move with but not rotate with.

    You can give them both a common parent as well to keep things organised.
    Code (CSharp):
    1. DwarfRoot
    2. |- Dwarf
    3. |-- Text
    4. |- OxygenBar
     
    Grzp likes this.
  3. Grzp

    Grzp

    Joined:
    Jan 25, 2018
    Posts:
    31
    Thanks, I didn't think about it.
     
    spiney199 likes this.
  4. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    233
    just to add to this —

    this is how I always program my games ^
    DrawfRoot has all the movement and collisions. But Dwarf is the only thing that rotates. This way all text and images attached to drawf always stay right side up.
     
    Grzp likes this.