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

Make child not inherit parent's transforms

Discussion in 'Scripting' started by NCreation, May 8, 2010.

  1. NCreation

    NCreation

    Joined:
    May 8, 2010
    Posts:
    4
    Hi, it's my first post here. I was having fun with UDK until Unity's simplicity called me back.
    Anyhow, I'm wondering if there's a clever way to make a child not inherit its parent's movement/rotation. I need this because I'm attempting to simplify networking later for me by attaching the spells that are cast to the player that cast them. The problem with that is that fireballs now rotate and move with the player.
    And that just looks weird.
    So is there any way to change this?
    Since the fireballs are always moving, I can't just fix them in one spot.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Children by definition inherit the parent's movement/rotation, otherwise they wouldn't be children. Maybe you could make both the player and the spells children of an empty game object that doesn't move.

    --Eric
     
    umair_ahmad likes this.
  3. NCreation

    NCreation

    Joined:
    May 8, 2010
    Posts:
    4
    That's true I suppose. But with the message broadcasting system it seems more fitting to have the option of disabling the inheritance.
    Either way, adding another parent to the player worked nicely. Thanks
     
  4. rsx

    rsx

    Joined:
    Nov 4, 2009
    Posts:
    87
    There are two classic ways to do this in 3D.


    1) Calculate everything using the parent's parent space. Children only follow parents because they are defined in their local space. If you just calculate using another space the object will behave is if it has a different parent.


    2) Negate everything the parent does. In essence, do the opposite. Undo it.
     
  5. SiliconDroid

    SiliconDroid

    Joined:
    Feb 20, 2017
    Posts:
    302
    Necrobump:
    For dynamic scene management having option to turn off inheritance for:

    Rotation and/or Translation and/or Scale

    Would be super handy.

    For example worldspace helper objects associated with some game object; you want them to get destroyed when you destroy the main object, otherwise you gotta remember to do it yourself.

    Also you can keep whole scene heirachy cleaner and see what helper objects are associated with what main objects.

    I used the option in Shiva3D lots and really miss it here.

    Currently writing custom AI locomotion for bots, want to be able to have various worldspace marker objects per bot to drop down and not worry about cleaning them up when bot gets destroyed.
     
    MercurialKid likes this.
  6. Rispat-Momit

    Rispat-Momit

    Joined:
    Feb 14, 2013
    Posts:
    261
    Hi there! I figure out this solution :D

    Just set this script to your child ;) (It works also in Edit Mode so that you can set up the scale you need.

    At the **FixeScale** set up the size of your child you need and at the **parent** set your parent model.


    Code (CSharp):
    1.  
    2.  
    3.     using System.Collections;
    4.     using System.Collections.Generic;
    5.     using UnityEngine;
    6.    
    7.     [ExecuteInEditMode]
    8.     public class FixedScale : MonoBehaviour {
    9.    
    10.      public float FixeScale =1 ;
    11.         public GameObject parent;
    12.        
    13.         // Update is called once per frame
    14.         void Update () {
    15.             transform.localScale = new Vector3 (FixeScale/parent.transform.localScale.x,FixeScale/parent.transform.localScale.y,FixeScale/parent.transform.localScale.z);
    16.    
    17.                 }
    18.             }
    19.  
    20.  
    21.  
    22.  
     
    MikuNuonuo likes this.
  7. VolkertJansz

    VolkertJansz

    Joined:
    Jan 6, 2020
    Posts:
    8
    Hi Rispat

    What would the script look like if I would also like the child to ignore the parents rotation?

    So I want only the child's position to follow the parent.

    Thanks a lot, in advance!
     
  8. CarvalhoLO

    CarvalhoLO

    Joined:
    Nov 29, 2017
    Posts:
    8
    I think it is easy to set a fixed rotation in the childs using transform.rotation = Quaternion.Euler(rotation you need for the childs) in the void update;

    example:
    void Update()
    {
    transform.rotation = Quaternion.Euler(-4.5f, 180f, 0f);
    }