Animate object with moving endpoint
« on: March 15, 2016, 11:21:14 PM »
I'm working on an endless runner type game (actually, a vertical endless climber).  In the game the player can gather coins as they climb. When a coin is "collected", I'd like to tween it from its current position to a static location on the screen (where the GUI coin counter is located). While this seemed easy on the surface, it turns out to be harder than I expected.

While I have both a start and end position for the tween in world coords (start = coins current position, end = location of GUI coin counter (top left of screen), converted to world coords).  However, since it's an endless climber, the world coordinate system is changing while the coin is tweening.  I attempted to solve the issue using the following:

Code: [Select]
public class CoinMotion : MonoBehaviour
{
    private float _completionRadius = 0.25f;
    private float _speed = 80;
    private Vector3 _targetLoc = new Vector3(0.075f, 0.85f);

    public void MoveCoin()
    {
        Tweener tweener = gameObject.transform.DOMove(Camera.main.ViewportToWorldPoint(_targetLoc), _speed).SetSpeedBased(true).OnComplete(Finish).Play();
        tweener.OnUpdate(delegate()
        {
            // keep going until we're "close enough"
            float dist = Vector3.Distance(gameObject.transform.position, Camera.main.ViewportToWorldPoint(_targetLoc));
            if (dist > _completionRadius)
            {
                tweener.ChangeEndValue(Camera.main.ViewportToWorldPoint(_targetLoc), true);
            }
        });
    }

    private void Finish()
    {
        // pulse the coin collector GUI once the coin tween finishes...
    }
}

Note the "_targetLoc" vector represents the location of the on-screen "coin collector" in Viewport coords.  While the above mostly works, it takes too long to actually reach the destination (even if sped up to crazy values).  Also, the path seems a bit wonky depending on how fast the world coords system is changing.  At times, the world coords can change very fast (for instance, if the character is currently using a jetpack, they can climb quite fast).

It seems to me that the simple solution is to tween the coin as a "UI element" - using UI coordinates, since those wouldn't change with the world coord system.  However, maybe there's a good way to do in Worldspace also?

Any thoughts appreciated.

Thanks.
« Last Edit: March 16, 2016, 10:54:51 PM by jgodfrey »

*

Daniele

  • Dr. Admin, I presume
  • *****
  • 378
    • View Profile
    • Demigiant
Re: Animate object with moving endpoint
« Reply #1 on: March 28, 2016, 01:25:43 AM »
Hi,

You don't need to necessarily use Unity's UI. You can simply keep the "coin collector" in a separate gameObject that doesn't move (or that follows the camera in case you're moving the camera and not the background), then convert the coordinate system of the coin when it's touched, move it inside the "coin collector" parent, and tween it from there. Or you could use a pure UI approach as you mentioned. Whatever the solution, I would strongly recommend NOT to have a "coin collector" that moves, because indeed it would lead to tons of problems and math/performance/tween-value-updates calculations that are definitely not needed when you can simply solve it otherwise. It's better for the brain and for the overall performance :)

Cheers,
Daniele

Re: Animate object with moving endpoint
« Reply #2 on: March 28, 2016, 07:15:15 PM »
Daniele,

I appreciate the input. I'm sure I'll be able to work out something satisfactory using your suggestions.