Calculate object position in screen space - camera canvas
I'm in need of calculate the correct position of a 3D object which I'm displaying over a RawImage in my UI. If I put my UI image in the center of the screen I can calculate it perfectly, if I move my image around my UI canvas I'm getting results which are off by an offset, depending on the side I'm moving. I've taken a couple of screenshot of what I mean, the orange side is my 3D quad, the white square is just an image debugging where my point should be calculated.
The setup I have is this: - a world camera pointing on a 3D quad - a ui canvas with a dedicated perspective camera (Screen space - Camera) - a panel in my ui canvas displaying the 3D quad
The code:
var worldPoint = t.Value.MeshRenderer.bounds.min; //t.Value is the 3D quad
var screenPoint = worldCamera.WorldToScreenPoint(worldPoint);
screenPoint.z = (baseCanvas.transform.position - uiCamera.transform.position).magnitude; //baseCanvas is the UI canvas
var pos = uiCamera.ScreenToWorldPoint(screenPoint);
debugger.transform.position = pos; //debugger is just the square image used to see where my calculated point is landing
Another test:
var screenPoint = worldCamera.WorldToScreenPoint(t.Value.MeshRenderer.bounds.min);
Vector2 localPoint;
RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, screenPoint, uiCamera, out localPoint); //rectTransform is my UI panel
debugger.transform.localPosition = localPoint
But I always get the same result, how can I make the correct calculation considering the offset?
Have you already found a solution for this problem? Because I'm stuck at the same problem too.
Your answer
![](https://koobas.hobune.stream/wayback/20220612214639im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Mouse Events with Render Texture 0 Answers
Canvas not rendered by camera 1 Answer
Hololens HUD 0 Answers
Screen space overlay canvas problems. 1 Answer
Camera ProjectionMatrix and Canvas Scaling - different results in 2018.3 0 Answers