I am using the new UI system (that is a UI Document) in which I have a four visual elements nested in each other.
The first three are in position Relative and the fourth one is position Absolute and is referenced using _goalBoxes[goalId].
I want to set the position of a GameObject with a particle system to play the particles where the fourth visual element appears on the screen.
How do I calculate this?
I have tries several things but with no success:
`var root = GetComponent<UIDocument>().rootVisualElement.Q<VisualElement>("rootContainer");
var pos = root.LocalToWorld(_goalBoxes[goalId].layout.position);
pos = new Vector2(pos.x, Screen.height - pos.y);
var worldPos = Camera.main.ScreenToWorldPoint(new Vector3(pos.x, pos.y, 0));
_goalCounterExplosionVFX[goalId].transform.SetPositionAndRotation(new Vector3(worldPos.x, worldPos.y, 0), quaternion.identity);`
This doesn’t work as the worldPos received is (0, 0, -10). Even if I try Camera.main.nearClipPlane instead of the z=0 to calculate the worldPos, I eventyally received coordianates which are not in the viewport.
Ziv V is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.