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:
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.