So I have been trying to get points of 4 spheres that are in my scene to world space screen coordinates. My canvas render is set to Camera and NOT screen space overlay for a reason. When I switch to overlay it works because the scale of the rect is 1:1 not so the points are being scaled accordingly to fit inside the Screen Space - Camera Rect. How do I get the world positions of the spheres to the Screen Space - Camera canvas?
Once you have your screen position you could try using ScreenPointToLocalPointInRectangle which will take it from the screen position to your canvas position. Is that what you’re trying to do? As you mentioned, you wouldn’t need to do this in ‘overlay’ mode. Here’s what the docs say on that:
“The cam parameter should be the camera associated with the screen point. For a RectTransform in a Canvas set to Screen Space - Overlay mode, the cam parameter should be null.”
So when you’re not using overlay mode you can pass the RectTransform of your canvas to that function along with your screen point.
Hello, I have the same problem but I don’t seem to be able to solve it.
I have 4 cameras (split screen), each one of them taking up a fourth of the screen. Each camera has it’s own Canvas with Screen Space - Camera mode on. I have some world objects that I want each canvas to track independently.
The problem is that this Items appear so small that are imposible to see or appear too far away from the camera. Here is my code if anyone wishes to help me (I’ve been days trying to achieve this):
//This code is inside the object that I want to be tracked. myCamera is the camera of the canvas.
Vector3 screenPoint = myCamera.WorldToScreenPoint(myObjectTransform.position);
Vector2 result;
RectTransformUtility.ScreenPointToLocalPointInRectangle(transform.parent.GetComponent<RectTransform>(), screenPoint, myCamera, out result);
transform.position = result;
encounter the same problem, solved by code below, not sure if it is the best solution, but it works for me
transform is the GameObject in scene, component Transform
mCanvas is UI canvas, component RectTransform
mCamera is camera used for scene
Thumbs up to the solutionis from @Yuanzezhong … Nothing else here on elsewhere worked, and tbh I have no idea why his solution did either lol. The sizeDelta piece is a magic wand afaik.
// Returns a normalized [0,1] position counted from camera bottom left
public static Vector2 GetRelativePosOfWorldPoint(Vector3 worldPoint, Camera camera) {
Vector3 screenPoint = camera.WorldToScreenPoint(worldPoint);
return new Vector2(screenPoint.x / camera.pixelWidth, screenPoint.y / camera.pixelHeight);
}
// Use it like this:
Vector2 canvasSize = theCanvasImRenderingOn.rect.size;
Vector2 relativePos = GetRelativePosOfWorldPoint(myWorldObject.transform.position, theCameraIAmLookingThrough);
theRectTransformIWantToPosition.anchorMin = theRectTransformIWantToPosition.anchorMax = Vector2.zero; // set bottom left anchor
theRectTransformIWantToPosition.anchoredPosition = relativePos * canvasSize;