I’m “tweening” a sprite from a spot in my 2d world to a specific point in my overlay/HUD. When needed, I create an Image on my UI canvas and tween it to 0, 0, 0.
Problem is, I need to choose the correct screen position of another image, but can’t seem to find the correct values.
I have a canvas gameobject called HUDCanvas used to display my game’s HUD. It’s set to “Screen Space - Overlay” and defaults to a rect transform of x: 608.5, y: 371.
I have an image on this canvas, called “Hotbar”. It’s anchored to x: 0.5, y:0 and has a rect transform position of x: 0, y: 44.
Those display just fine, the anchoring works well across screen sizes.
I want the destination of my "tween"ed sprite to be the “Hotbar” image. 0, 0 takes my tweened image to the bottom left corner of the window, no matter how I adjust the anchor. I can’t figure out any coordinate system to make these all make sense. The “WorldToScreenPoint” position seems correct, the canvas and hotbar graphics show correctly, so I’m not sure what’s missing.
The hotbar position is x: 608.5 y: 44, it’s localPosition is x: 0, y: 44.
If 0, 0 for my tweened sprite is “lower left corner” then none of those coordinate systems are right.
// Convert this world coord to a screen coord
var camera = GameObject.Find("Main Camera").GetComponent<Camera>();
var screenPos = camera.WorldToScreenPoint((Vector3)tile.worldVec);
// We'll render to the HUD canvas
var canvas = GameObject.Find("HUDCanvas");
// Build a game object with the sprite
var gameObject = new GameObject();
gameObject.AddComponent<RectTransform>();
gameObject.AddComponent<Image>();
gameObject.GetComponent<Image>().sprite = drop.gameObject.GetComponent<SpriteRenderer>().sprite;
gameObject.transform.position = screenPos;
gameObject.transform.SetParent(canvas.transform);
gameObject.GetComponent<RectTransform>().anchoredPosition = new Vector2(0.5f, 0);
var hotbar = GameObject.Find("Hotbar");
// Tween it
LeanTween.move(gameObject, new Vector3(0, 0, 0), 0.25f).setEase( LeanTweenType.easeInQuad ).setOnComplete(() => {
Destroy(gameObject);
});