Question about Screen to World Point
I'm trying to do what should be fairly simple - have a 3D GameObject (a sphere planet) be drawn in the middle of a UI rectangle (a planet info column). I pass the anchoredPosition of the RectTransform to the ScreenToWorldPoint function, but it never seems to draw the object where it needs to be. I have also tried using the position of the RectTransform and converting it to a screen coordinate using WorldToScreenPoint, but that doesn't work either. I am using the main Camera (and yes, my Z coordinate is not 0). My Camera is shooting straight on the Z axis, so there is no angle. There are 2 considerations that may be janking this process up:
1) My Camera is at a Z-2000 coordinate (for zooming in, like a dolly to a star system), and the planet should be drawn about 120 Z-units from the camera plane; so this may be causing issues
2) My System Canvas (the Canvas where the planet boxes are drawn and where the planet position would be derived) is a Screen Space-Camera, not sure if that would cause an issue.
Here's the relevant code:
// first I get a list of Vector3 positions for each UI box and send it to a UIManager to allow the drawing function to use. This works fine
foreach (GameObject gO in systemObjectsDrawnList)
{
Vector3 position = gO.transform.GetComponent<RectTransform>().anchoredPosition;
uiManagerRef.PlanetLocationsSystemView.Add(position);
}
// then I draw each planet. Here's where it gets janky
if (planetsToDraw[x] != null)
{
// planet draw routine
PlanetData pData = new PlanetData();
Vector3 uiPoint = new Vector3(uiManagerRef.PlanetLocationsSystemView[totalPlanets].x, uiManagerRef.PlanetLocationsSystemView[totalPlanets].y, -120f);
Vector3 planetPoint = mainCamera.ScreenToWorldPoint(new Vector3(uiPoint.x, uiPoint.y, mainCamera.transform.position.z));
Vector3 position = new Vector3(planetPoint.x, planetPoint.y, -120f); // set the planet position using the planetPoint coordinates from above and the y-value
GameObject planet = Instantiate(planetsToDraw[x], position, Quaternion.Euler(0, 180, 0)) as GameObject; // draw the planet
}
But... this is not working for some reason the way I want. Any thoughts? I'm using 2020.1.17 BTW. Thanks!
Your answer
![](https://koobas.hobune.stream/wayback/20220613024714im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Move camera between canvases in UI menu 0 Answers
Camerashake doesn´t work expected in Build c# 0 Answers
Text Objects do Lerp 0 Answers