unity – Cash Not Transferring to Canvas Place When Digicam Strikes

unity – Cash Not Transferring to Canvas Place When Digicam Strikes

[ad_1]

I’m at the moment creating a 2D sport in Unity, and I am having a problem with shifting cash to a selected place within the UI Canvas when they’re spawned. I would like the cash to animate from their spawn place within the sport world to a degree within the Canvas. Nonetheless, when the digicam strikes, the cash don’t accurately attain their vacation spot; they appear to go off-screen.

Right here is the context of my problem:

Setup:

I’ve a CoinManager script that’s accountable for spawning cash and animating them to the UI Canvas.
The cash are instantiated at a place based mostly on the participant’s interactions, and I would like them to maneuver to a hard and fast place within the Canvas when collected.

The Code:

Right here is the related a part of my CoinManager script:

public class CoinManager : MonoBehaviour
{
    public static CoinManager occasion;

    [SerializeField] personal GameObject coinPrefab;
    [SerializeField] personal Remodel coinParent;
    [SerializeField] personal RectTransform coinEndRectTransform; // The goal level within the Canvas
    [SerializeField] personal int maxCoinAmount;
    [SerializeField] personal float coinPerDelay;
    [SerializeField] personal float moveDuration;
    [SerializeField] personal float totalDelay;
    [SerializeField] personal Ease moveEase;

    personal void Awake()
    {
        occasion = this;
    }

    public void OnButtonPressed()
    {
        int coinAmount = Grave.Occasion.GetCurrentCoins();
        coinAmount = Mathf.Min(coinAmount, maxCoinAmount);
        coinPerDelay = totalDelay / coinAmount;

        for (int i = 0; i < coinAmount; i++)
        {
            var targetDelay = i * coinPerDelay;
            ShowCoin(targetDelay);
        }

        Invoke(nameof(OnAnimationDone), totalDelay + moveDuration);
    }

    public void OnAnimationDone()
    {
        Grave.Occasion.UpdateCoinText();
    }

    public void ShowCoin(float delay)
    {
        GameObject graveObject = GameObject.FindWithTag("Grave");

        if (Grave.lastGraveWithCoins != null)
        {
            var coinObject = Instantiate(coinPrefab, coinParent);
            var offset = new Vector3(Random.Vary(-.5f, .5f), Random.Vary(-.5f, .5f), 0f);
            var startPos = Grave.lastGraveWithCoins.rework.place + offset;
            coinObject.rework.place = startPos;

            // Calculate the place in world house for the coinEndRectTransform
            Vector3 worldPosition;
            RectTransformUtility.ScreenPointToWorldPointInRectangle(
                coinEndRectTransform,
                coinEndRectTransform.place,
                Digicam.principal,
                out worldPosition
            );

            // Scale the coin earlier than shifting
            coinObject.rework.localScale = new Vector3(.1f, .1f, .1f);
            coinObject.rework.DOScale(Vector3.one, delay);

            // Transfer the coin to the calculated world place
            coinObject.rework.DOMove(worldPosition, moveDuration)
                .SetEase(moveEase)
                .SetDelay(delay)
                .OnComplete(() =>
                {
                    coinObject.SetActive(false);
                });
        }
    }
}

Challenge:

The primary downside I’m dealing with is that when the digicam strikes, the cash don’t attain the designated place within the Canvas accurately. They often go off-screen or don’t transfer to the supposed level.

I want the cash to at all times animate towards the fastened place within the Canvas, whatever the digicam’s place or motion. I think that there could also be a problem with how the place is calculated or utilized, particularly when the digicam will not be stationary.

Further Info:

The coinEndRectTransform is ready up within the UI Canvas, and it ought to signify the goal place the place I would like the cash to go.
The sport is utilizing Cinemachine for digicam management, and I am undecided if this impacts the habits of the cash.

I might respect any insights or various strategies to make sure the cash at all times animate to the proper place within the Canvas, even when the digicam strikes.

[ad_2]