1
votes

I am working in a Game which is pretty similar to Mario. So when player touches the coin object in World Space, I need to animate by moving that coin object to Coin meter, when the render mode of Canvas is Screen Space - Overlay, I can get the sprite object position easily with below code

CoinSprite Code

GameObject coinCanvasObject = Instantiate(prefab, canvas.transform);//Instantiate coin inside Canvas view
coinCanvasObject.transform.position = Camera.main.WorldToScreenPoint(coinSpriteObject.transform.position);//getting coin position from World Space and convert to Screen Space and set to coinCanvasobject position 
AnimateCoin animate = coinCanvasObject.GetComponent<AnimateCoin>();
animate.animateCoin(coinSpriteObject.transform.position);
coinSpriteObject.SetActive(false);

AnimateCoin

public class AnimateCoin : MonoBehaviour
{
    private float speed = 0f;
    private bool isSpawn = false;
    private Vector3 screenPos;

    public void animateCoin(Vector3 screenPosTemp, Camera cam, Canvas canvas)
    {
        screenPos = Camera.main.WorldToScreenPoint(screenPosTemp);
        isSpawn = true;
    }

    private void Update()
    {
        if (isSpawn)
        {
            speed += 0.025f;
            transform.position = Vector3.Lerp(screenPos, targetObject.transform.position, speed);
            if (Vector3.Distance(transform.position, targetObject.transform.position) <= 0)
            {
                StartCoroutine(deActivateCoin());
            }
        }
    }

    private IEnumerator deActivateCoin()
    {
        isSpawn = false;
        yield return new WaitForSeconds(0.2f);
        gameObject.SetActive(false);
    }    
}

Since I need to bring particle effect into Canvas view, I am changing the Canvas render mode to Screen Space - Camera. When I change the Canvas to this render mode I could not get the exact sprite object position to trail the coin effect.

1
But if the coin is a child of the Canvas anyway can't you simply spawn your trail effect at the same local position also as child of the canvas?derHugo
Initially the coin will be in world space and when player catch the coin, I Instantiate another coin object inside the Canvas and set the world space coin object position to this newly Instantiated object and then do the animation.Aaron Ebinezer
Ah I see, just for my understand you want to have something like: Have a 3D coin -> User clicks it -> is replaced with a 2D coin Sprite in the canvas -> animate/move into a certain position in the canvas with trail?derHugo
I am having the same issue. Have you solved it? I want particle effects on GUI so I need Screen Space-Camera but when you have this on, you cant get the world position of the GUI. And the player is moving continuously in my case so the world position of rectransform is changing...Aykut Karaca
No I could not solve this issue. I need to show confetti effect in GUI and I solved it by getting animation sprite from my designer. But all particles effects cannot be done through animation that is why I am afraid off.Aaron Ebinezer

1 Answers

0
votes

Hope this helps:

public Camera           cam;    // Camera containing the canvas
public Transform        target; // object in the 3D World
public RectTransform    icon;   // icon to place in the canvas
public Canvas           canvas; // canvas with "Render mode: Screen Space - Camera"

void Update()
{
    Vector3 screenPos = cam.WorldToScreenPoint(target.position);
    float h = Screen.height;
    float w = Screen.width;
    float x = screenPos.x - (w / 2);
    float y = screenPos.y - (h / 2);
    float s = canvas.scaleFactor;
    icon.anchoredPosition = new Vector2(x, y) / s;
}

PD: It worked perfectly for me in a 2D video game, I didn't test it in a 3D game, but I think it should work too.