Unity 2D, Prefab text with rising animation is spawned on touch, correct x position but always spawns at y 0?

18 Views Asked by At

The bonus text particle has an animation where it is at x 0 the whole time, but the y rises from 0 to 1. When playing the game, tapping the screen, the particle is created at the correct finger position x level but is always starts at y 0 instead of the finger position in y.

I want it to spawn at the exact finger position. I think it's because of the animation but I have no idea how to fix it.

screen recording

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using BreakInfinity;
using UnityEngine.Pool;

public class GameManager : MonoBehaviour
{
    [Header("Stats")]
    public static BigDouble notes = 0;
    public static BigDouble tap = 10;
    public static BigDouble auto = 0;

    [Header("Taps")]
    [SerializeField] private GameObject bonusParticlePrefab;

    float tapCooldown = 0.04f;
    float lastTapTime = 0f;

    private ObjectPool<GameObject> bonusParticlesPool;

    void Start()
    {
        bonusParticlesPool = new ObjectPool<GameObject>(CreateFunction, ActionOnGet, ActionOnRelease, ActionOnDestroy);
    }

    private GameObject CreateFunction()
    {
        return Instantiate(bonusParticlePrefab, transform);
    }

    private void ActionOnGet(GameObject bonusParticle)
    {
        bonusParticle.SetActive(true);
    }

    private void ActionOnRelease(GameObject bonusParticle)
    {
        bonusParticle.SetActive(false);
    }
    
    private void ActionOnDestroy(GameObject bonusParticle)
    {
        Destroy(bonusParticle);
    }

    void Update()
    {
        foreach(Touch touch in Input.touches)
        {
            if (touch.phase == TouchPhase.Began)
            {
                if (Time.time - lastTapTime > tapCooldown)
                    {
                        bool isOverUI = false;
                        for (int i = 0; i < Input.touchCount; i++)
                        {
                            if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(i).fingerId))
                            {
                                isOverUI = true;
                                break;
                            }
                        }

                        if (!isOverUI) // if not over UI elements
                        {
                            IncreasePoints();
                            Vector3 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
                            touchPosition.z = 0;

                            lastTapTime = Time.time; // Update the last tap time 

                            GameObject bonusParticleInstance = bonusParticlesPool.Get();
                            bonusParticleInstance.transform.position = touchPosition;

                            LeanTween.delayedCall(1, ()=> bonusParticlesPool.Release(bonusParticleInstance));
                        }
                        
                    }
            }
        }
    }

    void IncreasePoints()
    {
        notes += tap;        
    }
}

1

There are 1 best solutions below

0
Zbajnek On

Assuming that the animation of rising particles on the y axis is done with animator and animation we can deduct that this is the reason why your touchPosition is being ignored when you get the particle from the pool.

You are spawning the particle at the touchPosition but it's immediately told by the animation to move from 0 to 1 on the y axis.

To fix this, I would avoid using animation for animating the bonusParticle because you want to dynamically tell the particle to move from y to y + 1 and that dynamic behavior is not possible with pure animation (from what I know).

You can use the LeanTween library to do that:

GameObject bonusParticleInstance = bonusParticlesPool.Get();
bonusParticleInstance.transform.position = touchPosition;

LeanTween.moveY(bonusParticleInstance, touchPosition.y + 1f, 1f);