Position flickering
« on: March 22, 2016, 01:27:26 AM »
Hi, I have a three part animation, in the first one the code creates a coin and then moves it from a position to another in global coordinates:
Code: [Select]
           GameObject gCoin = GameObject.Instantiate(
                prefabCoinGO, starts[j].position + Vector3.up*2.5f, starts[j].rotation
            ) as GameObject;
            Transform tCoin = gCoin.GetComponent<Transform>();
            Tween twCoin    = tCoin.DOMove(ends[j].position + Vector3.up*2.5f, timeToTravelCoin)
                .OnComplete(() =>
                {
                    RemoveCoin(gCoin);
                })
            ;

the second and third part, are two more animations but in local space, if i do those with this approach:
Code: [Select]
            float t = Time.time;
            twCoin.OnUpdate(() =>
                {
//I tried with a tCoin.Translate(...,Space.Self) too ,same result
                    Vector3 lPos = tCoin.localPosition;
                    tCoin.localPosition = Vector3.Lerp(
                        lPos,
                        lPos + Vector3.up * Mathf.Sin(
                          Mathf.PI * 100 * Time.deltaTime * (Time.time - t)
                        )*4
                    , Time.deltaTime*10);
                    tCoin.Rotate(Vector3.up * Time.deltaTime * Mathf.PI * 60);
                })
            ;

there are some of the coins that flickers in position, jumping with bad Y coordinates, maybe because it crosses the 0,0,0 point to another axis.

But, if i do this:
Code: [Select]
            tCoin.DOLocalMoveY(-0.23f, 0.6f).SetLoops(-1, LoopType.Yoyo)
                .SetEase(Ease.InOutSine);
            tCoin.DORotate(Vector3.up * 360f, 3f, RotateMode.FastBeyond360)
                .SetLoops(-1, LoopType.Restart).SetEase(Ease.OutSine);
it works perfectly, what if i want to use OnUpdate(), does it keep a good track of the localposition or should i change dotween update for fixedupdate?

*

Daniele

  • Dr. Admin, I presume
  • *****
  • 378
    • View Profile
    • Demigiant
Re: Position flickering
« Reply #1 on: March 28, 2016, 01:18:57 AM »
Hi,

The issue there is that you're changing the same property at the same moment. Changing the localPosition of a target is exactly the same as changing its position. The difference is simply in the coordinate system, but they're actually both setting the same property, which is the position.

You should use a parent-child relationship there. Tween the parent, and use OnUpdate to set the localPosition of the child. That would create no conflicts because you would really be setting two different properties (of two different targets).

Cheers,
Daniele

Re: Position flickering
« Reply #2 on: March 29, 2016, 12:26:17 PM »
Hey Daniele, thanks for the tip!
Yes ,I know it's the same var, my error was supposing that if I change the globalPosition with the startPoint.y == endPoint.y == 0 it would not change in the interpolation, but Y is part of the animation computation as a field of position, so of course it's changing over time together with the localPosition tween XD
I feel dumb lol
Well, now i know what i can i do, separate in 2 tweens, for global x and,z positions with DoMoveX, DoMoveZ and do in the update of one of them the localPosition.Y changes :)
Thanks!