Camera.WorldToScreenPoint and Camera.WorldToViewportPoint giving negative values why?

I am trying to make a simple UI system where a text object appears above an interactable object on the canvas. I am using an object clicking script off the internet to debug this. Everything works with the clicking, it finds the objects and finds their positions. However, when trying to use those positions as a parameter for Camera.WorldToScreenPoint or Camera.WorldToViewportPoint I get completely wrong numbers (often negative). Whereas from the description negative values shouldn’t even be possible if I’m checking something within the viewport, right?

What I’m using to get the target position (debug)

if (Input.GetMouseButtonDown(0))
        { 
            Ray ray = TargetCamera.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                if (hit.transform != null)
                {
                    PushPrompt(hit.transform.position, "Prompt Here"); 
                    
                }

//custom function that gets a vector3 and a string which does the following

public void PushPrompt(Vector3 ObjectPosition, string Line)
        {
            PromptText.text = Line;
            PromptTarget = ObjectPosition;
            
        }

PromptTarget being a vector3 which is then used:

if(PromptText.text != null && PromptTarget != null)
        {

            Vector2 PromptPosition = TargetCamera.WorldToScreenPoint(PromptTarget);
            Prompt.anchoredPosition = new Vector2(PromptPosition.x - TargetCamera.pixelWidth*0.5f, PromptPosition.y - TargetCamera.pixelHeight*0.5f);
            

        }

All of this (if my math is correct) would work only if Camera.WorldToScreenPoint actually gave me values between 0 and screen resolution, but it doesn’t. Please help, this is ridiculous.

Uhm your math doesn’t make much sense to me. Why do you normalize the screen position you get back from WorldToScreenPoint? This doesn’t make much sense. The resulting position would be on a quater circle around the lower left corner of the screen for any position within the viewport. Also keep in mind that you use the object’s transform.position, not the actual point of the mouse. So it is possible to hit an object with your raycast whose position is not inside the view.

Keep in mind that ScreenSpace as well as ViewportSpace has it’s origin at the bottom left.

Sorry to dig up an old thread, but for anyone else who is asking the same question, and confused, the WorldToScreenPoint function will return negative values because the point you are using is off of the screen. The viewport ranges from 0,0 which is the bottom right corner of the screen, to 1,1 with is the top left. If you divided the position by the resolution, you would get the pixel. Since unity needs to work for all sizes of resolution, it chooses to use this method of calculation. To achive what he wants, “Do you maybe know anything that can achieve the above desired effect?” He would just need to check to make sure the returned value lies between 0 and 1 on both axis, otherwise the point isnt visible and should be ignored. Then he should divide the point by the current screen resolution and bam.