- Home /
How to move gameobject (main camera) to another gameobject (UI camera) position ?
Hi ! I try to make a common effect for my game, in fact I want to move an item picked by the player toward an UI element (like a lot of mobile game i guess).
The problem I have is my gameobject is on the main camera view, while my UI element is obviously on my UI camera. So when I use a "move toward", with my UIelement.transform.position, my item reach the UIelement's position in my scene view (so off screen).
Is there a method to make this effect easily? I've try RectTransformUtility, camera.main.worldtoscreenpoint, etc ... But I couldn't get my UIelement's position correctly.
Thanks in advance ! :)
Answer by DamDamDuSixZero · Aug 15, 2019 at 09:46 AM
Okkkk so I ve randomly find the solution lmao
I put it here in the case of someone need to achieve the same effect !
To get the coordinate of your UI element in your world :
RectTransformUtility.ScreenPointToWorldPointInRectangle(UIelement.GetComponent<RectTransform>(), gameObject.GetComponent<Camera>().WorldToScreenPoint(UIelement.transform.position), Camera.main, out positionToReturn);
Where : - UIelement is the element you need to know the world position - gameObject.GetComponentCamera() is the UI camera - positionToReturn, obviously the vector3 variable where you stock the UIelement's position.