Customise Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorised as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyse the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customised advertisements based on the pages you visited previously and to analyse the effectiveness of the ad campaigns.

No cookies to display.

[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]