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. Dismiss Notice

Parent and Child Move Animation position

Discussion in '2D' started by BigApp7e, Sep 5, 2020.

?

Parent and Child Move Animation position

  1. Possible Responses

    2 vote(s)
    100.0%
  2. Possible Responses

    2 vote(s)
    100.0%
Multiple votes are allowed.
  1. BigApp7e

    BigApp7e

    Joined:
    Mar 10, 2019
    Posts:
    9
    Hi all. I need little help with unity 2d coordinate system and animation. To be clear let's give you very simple example. In unity 2d project I have two game object parentObj and respectivly childObj. At the beginning both are on position 0,0. in my Update function I have this two lines of code:

    void Update()
    {
    parentObj .transform.Translate(new Vector2(0,1) * Time.deltaTime);
    Debug.Log("parent pos "+ parentObj.transform.position + " child pos "+ childObj.transform.position);
    }

    I expect only parent to change position, child have to stay at position 0,0 because child is in parent coordinate system and child actually don't move;

    parent pos (0.0, 0.1, 0.0) child pos (0.0, 0,0, 0.0)
    parent pos (0.0, 0.2, 0.0) child pos (0.0, 0,0, 0.0)
    parent pos (0.0, 0.3, 0.0) child pos (0.0, 0,0, 0.0)

    Real result is :
    parent pos (0.0, 0.1, 0.0) child pos (0.0, 0.1, 0.0)
    parent pos (0.0, 0.2, 0.0) child pos (0.0, 0.2, 0.0)
    parent pos (0.0, 0.3, 0.0) child pos (0.0, 0.3, 0.0)

    So please if someone know why this happened and how to handle this strange problem, I will be very grateful.
    Thanks
     
  2. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    If the parent moves the child will move the same but it's local position will still be 0,0.
    If you don't want an object to move with something then why would you make it a child?
     
  3. BigApp7e

    BigApp7e

    Joined:
    Mar 10, 2019
    Posts:
    9
    Let's say I have base spaceship (parentObj) with few small spaceships (childObj). The biggest spaceship move forward all the time but the small spaceships (childObj) have range around this big spaceship and they (attack) move forward and backward . This is the reason for my question.
     
  4. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    Ah I understand the confusion. The child spaceships are independent so they can't be a child; child objects will always move with their parent.