简体   繁体   中英

how to find a position of an Object from World Space and convert to Canvas UI with render mode : Screen Space - Camera in Unity 2d?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM