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

Copying position of several objects makes them lag behind

Discussion in 'Editor & General Support' started by lejean, Jun 18, 2017.

  1. lejean

    lejean

    Joined:
    Jul 4, 2013
    Posts:
    383
    So I'm working on a asset where I need multiple objects to follow each other, like a chain if you will.

    So object A follows B follows C follows D and so on.

    The problem is there is noticeable lag on the objects when the first object in the chain moves, giving it an accordeon like effect, where as I want them to follow the object exactly. (See the lag effect in the picture when moving box A)
    lag.png

    So I've somewhat figured out what's causing this and part of it is the positioning in the hierarchy. (See second image)
    Hierarchy.png

    If A follows B, C, D .... there's no lag, so top to bottom.
    But if the objects follow each other bottom to top, they lag behind.

    Obviously a solution would be to order them correctly then, but this is tedious and another problem arises cause changing their hierarchy order at runtime does not fix this problem.

    It's almost as if a certain order is saved internally on game start.

    Is this expected behaviour?
     
    Last edited: Jun 18, 2017
  2. lejean

    lejean

    Joined:
    Jul 4, 2013
    Posts:
    383
    Anyone?
     
  3. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,244
    Well yeah becasue they don't all get called on the same frame.
     
  4. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,244
    What is your script like? Gimme.
     
  5. lejean

    lejean

    Joined:
    Jul 4, 2013
    Posts:
    383
    nothing special just a follow script to test

    Code (CSharp):
    1. void Update () {
    2.         if (goToFollow != null) {
    3.             GetComponent<RectTransform>().position = rectToFollow.position;
    4.         }
    5.     }
    and like I said, it does work if they follow each other in a particular order.
    The problem is that it doesn't seem to be consistent.

    What I need is the same thing as parent/child positioning, but I don't really want to child each of the objects to the ones they need to follow. (all though that's what I'm prolly gonna have to do)
     
  6. FMark92

    FMark92

    Joined:
    May 18, 2017
    Posts:
    1,244
    Well that's where you're messing up because chain elements are not being called in order.

    in short:

    "->" = follows

    what you have (I presume):
    obj3 -> obj2 -> obj1 -> obj0
    Guess what will happen if update() in obj2 gets called before update() in obj1. (Hint: it's what you have)

    what you want (I EFFIN PRESUME):
    obj1 -> obj0
    obj2 -> obj0
    obj3 -> obj0
     
  7. lejean

    lejean

    Joined:
    Jul 4, 2013
    Posts:
    383
    Dnno man I tried it, something definitely feels wrong.
    There's like 80+ frames/second.
    Shouldn't it still look pretty correct even if it it's a frame behind?

    The order of the hierarchy is certainly a factor.
    I'll just give you an example of the problem.
    Check the gifs in this imgur link

    The first gif is correct and this is where the gameobject follows the one above it in the hierarchy
    In the second gif the gameobject moves around when the player moves and this is when it follows the one beneath it in the hierachy.

    Problem is I don't know if I can rely on this.

    Any unity dev that can comment on this?
     
  8. Remi_Tribia

    Remi_Tribia

    Joined:
    Apr 29, 2015
    Posts:
    40
    Sorry for the necropost, I searched for this same issue and that was the first result in google... So the solution is to put the position logic in LateUpdate() instead of Update(). Might help others landing here...
     
  9. lejean

    lejean

    Joined:
    Jul 4, 2013
    Posts:
    383
    Nah I'm pretty sure I tried that too, the problem is that there isn't a reliable update order in the hierarchy so you have to make it yourself by updating a list of transforms manually in order from top to bottom in the hierarchy