ViewportToWorldPoint not working as expected

Hi

Im making a little prototype in it Im trying to clamp the movement of the player to that of the view of the screen so I began experimenting with ViewportToWorldPoint but for some reason its not working right, I tried getting the top, bottom, left and right borders by creating two vector3 variables and using ViewportToWorldPoint(1,1,0) and ViewportToWorldPoint(0,0,0) to get the top right and bottom left but both variables return nearly identical values almost as if ViewportToWorldPoint was working as “ScreenToWorldPoint”

 ViewPort = Camera.main.ViewportToWorldPoint(new Vector3(1, 1, Camera.main.nearClipPlane));
            ViewPortNegative = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, Camera.main.nearClipPlane));
            Debug.Log(ViewPort);
            Debug.Log(ViewPortNegative); 
            _Controller.Move(Movement.normalized);
            _Position = _Controller.transform.position;
            _Position.x = Mathf.Clamp(_Controller.transform.position.x, ViewPortNegative.x, ViewPort.x);
            _Position.y = Mathf.Clamp(_Controller.transform.position.y, ViewPortNegative.y, ViewPort.y);
            _Controller.transform.position = _Position;

ViewPort = Camera.main.ViewportToWorldPoint(new Vector3(1, 1, Camera.main.nearClipPlane)); Returns (24.2, 2.8, 0.3)

ViewPortNegative = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, Camera.main.nearClipPlane)); Returns (23.8, 2.5, 0.3)

isnt that weird? :eyes:

When you use AnythingToWorldPoint, the third parameter is the distance from the camera. Unless your camera is orthographic, (anything, anything, 0) is going to get you pretty much the same point.

I didnt literally used 0 in my code, but you are right, I didnt took into account the distance between the camera and the object. For some reason I thought it wouldnt matter :roll_eyes: