I'm developing (or rather butchering) plugin for Rust, which was build in Unity, and I need to create a targeting system. Unfortunately I got stuck on WorldToScreenPoint and WorldToViewportPoint. It seems that the Camera.main is not what player sees and is not moving at all. Also it has different resolution from what the game is set up.

Unfortunately I can't use Unity and all the work needs to be done in VS.

Does anyone has a suggestion how to get a "proper" camera for this?

Thank you in advance


As requested this is my code: Debug.log(Camera.main.WorldToScreenPoint(bAnimalNPC.transform.position).x.ToString()) Debug.log(Camera.main.WorldToScreenPoint(bAnimalNPC.transform.position).y.ToString()) Debug.log(Camera.main.WorldToScreenPoint(bAnimalNPC.transform.position).z.ToString())

The rest is just getting the bAnimalNPC and doing some checks.

  • 1
    \$\begingroup\$ Can you get a list of all cameras in the scene with FindObjectsOfType<Camera> and investigate whether the player's view camera has any distinguishing characteristics? \$\endgroup\$ – DMGregory May 28 at 18:51
  • \$\begingroup\$ I've tried listing all cameras present but I've used different method and it returned only Camera.main. I'll try FindObjectsOfType<Camera> to see if it will give me same result. Question - is it possible that there is only one camera? I mean logically players eyes should be a camera too. \$\endgroup\$ – Wujaszkun May 28 at 18:55
  • 1
    \$\begingroup\$ I have finally got time to check this and there is only one camera in the scene - Camera.main. So it must be player view camera. This however raises a question why WorldToViewportPoint and WorldToScreenPoint calculate values wrong, e.g. if the object I'm trying to target does not move (but the player does) the values do not change. When the target moves but I follow it with my "eyes" the values change. I'm missing something out but i don't know what... \$\endgroup\$ – Wujaszkun May 30 at 9:25
  • \$\begingroup\$ @Wujaszkun Is the camera viewing the animal? \$\endgroup\$ – Mohammad Zamanian Jun 2 at 19:30
  • \$\begingroup\$ It's an fps so look from my character eyes. \$\endgroup\$ – Wujaszkun Jun 2 at 19:31

Main Camera

main is a property of Camera that returns


Which is not really performant and you need to cash it in a variable at the start, but with that said, you could just tag your desired camera with MainCamera tag and it works.


Current Camera

You can also get the current camera which unity is currently rendering, like this:



All Cameras

If you can't access the unity scene, you could get all the cameras in the scene via code and check if it's your desired camera.

you can get all enabled cameras in the scene, using this :


Which returns Camera[].



Put this code on the animal and see what happens:

void OnGUI(){
    Vector3 screenPos = Camera.main.WorldToScreenPoint (transform.position);
    GUI.Box(new Rect(screenPos.x - 30, (Screen.height - screenPos.y) - 15, 60, 30), "Text");
  • \$\begingroup\$ I have actually tried Camera.allCameras and it returned only one camera: Main So this didn't work for me. As for the Camera.current it is read-only and afaik only for use in specific situations. \$\endgroup\$ – Wujaszkun May 31 at 10:04
  • \$\begingroup\$ is the Camera returned by allCameras tagged "MainCamera"? use Debug.Log(Camera.allCameras[0].CompareTag("MainCamera")); \$\endgroup\$ – Mohammad Zamanian May 31 at 10:19
  • \$\begingroup\$ Yep it's the MainCamera, and there are no other cameras according to Camera.allCameras. \$\endgroup\$ – Wujaszkun Jun 2 at 18:44
  • \$\begingroup\$ @Wujaszkun I guess there is something else wrong with what you're doing, please edit your question and insert your WorldToScreenPoint and WorldToViewportPoint code which is the real issue. \$\endgroup\$ – Mohammad Zamanian Jun 2 at 18:56
  • \$\begingroup\$ As requested I have added my code. \$\endgroup\$ – Wujaszkun Jun 2 at 19:13

Mohammed Zamanian explained a lot already, but let me provide you with a generic, practical solution.

You should create a Singleton class, which you put on an invisible gameObject on your scene. Assign your main camera to a public Camera variable. If you use multiple ones, with a fixed amount, use multiple variables.

public class CameraManager
    public static CameraManager Instance;

    // Refer to these by "CameraManager.Instance.PrimaryCamera"
    public Camera PrimaryCamera; // Remember to assign the camera on the gameObject.
    public Camera SecondaryCamera; // Assign this too.

    public Camera CurrentCamera; // This is your currently active camera set by the methods below.

    protected void Awake ()
        Instance = this;

    public void ActivatePrimaryCamera ()
        CurrentCamera = PrimaryCamera;
        SecondaryCamera.enabled = false; // Do the camera transition here.
        PrimaryCamera.enabled = true;

    public void ActivateSecondaryCamera ()
        CurrentCamera = SecondaryCamera;
        PrimaryCamera.enabled = false; // Do the camera transition here.
        SecondaryCamera.enabled = true;

Now you can set your camera as the current camera and can efficiently refer to them with your own code. I advise you to assert control over your cameras, so that you don't just arbitrarily search for whatever is currently active, and always know which one is active.

The Unity camera functions should be seen as backup functions, as they are not performant. Do not use them unless necessary. Rely on your own code.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.