Unity - Camera ScreenToWorldPoint returning odd values

unity screentoworldpoint
unity world to screen point not working
unity mouse position to screen space
unity screen to world point perspective
unity screen point to world space
unity pixel to world space
cinemachine screen to world point
unity to world space

The main camera's output is set to a render texture, which is applied to a material, which is applied to a quad that's scaled up to 128x72. The secondary camera is set to only see what is rendered to the child quad, who has the material with the render texture on it.

However Camera.main.ScreenToWorldPoint(Input.mousePosition) is returning values that aren't even close to the GameObject. I.E. The GameObject is instantiated at (0, 0, 0), and hovering over it shows the mouse at (307, 174). Moving the Rotating Object to the right edge of the screen will only return an x position of 64 (half of the 128px wide quad) so I'm not sure where the 300+ is coming from. Not sure if the quad/camera set up is responsible for this.

EDIT: Using a single orthographic camera, all properties the same except for using a render texture, instead of the setup I have now results in accurate ScreenToWorldPoint output.

The Input.mousePosition property will only return the x and y axis of the mouse position in pixels.

ScreenToWorldPoint requires the z axis too which Input.mousePosition doesn't provide. The z-axis value supposed to be the nearClipPlane of the camera. It will give you a position that's right in front of the camera.

Depending on the size of the 3D object you want to instantiate where mouse button is pressed, you will need to apply an offset to it to make it totally visible to the screen. For a simple cube created in Unity, an offset of 2 is fine. Anything bigger than that, you will need to increase the offset.

Below is a complete example of how to properly use ScreenToWorldPoint with Camera.nearClipPlane and an offset to instantiate a 3D object where mouse is clicked:

public GameObject prefab;
public float offset = 2f;

void Update()
{
    if (Input.GetMouseButtonDown(0))
    {
        Camera cam = Camera.main;

        Vector2 mousePos = Vector3.zero;

        mousePos.x = Input.mousePosition.x;
        mousePos.y = Input.mousePosition.y;

        Vector3 worldPoint = cam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, cam.nearClipPlane + offset));

        Instantiate(prefab, worldPoint, Quaternion.identity);
    }
}

Unity - Camera ScreenToWorldPoint returning odd values, ScreenToWorldPoint(Input. mousePosition) is returning values that aren't even close to the GameObject . I.E. The GameObject is instantiated at (0, 0, 0) , and hovering over it shows the mouse at (307, 174) . ScreenToWorldPoint returning wrong position even when given Z plane. I have a NavMeshAgent placed on a character model. When I log agent.transform.position I get (4.5, 0.1, 4.5) which is correct (this is a 2.5d game and movement happens along the x and z axes).

You may not be calling the Camera.ScreenToWorldPoint method correctly. In particular, the z position of the screen position parameter that's passed to this method should be defined as world units from the camera. See the Unity documentation on Camera.ScreenToWorldPoint.

Instead of Camera.main.ScreenToWorldPoint(Input.mousePosition), I think this is the correct way to call Camera.ScreenToWorldPoint:

var cameraPosition = Camera.main.transform.position;

// assuming `transform` is the transform "Virtual Screen Quad"...
float zWorldDistanceFromCamera = transform.position.z - cameraPosition.z;

var screenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, zWorldDistanceFromCamera);
var worldPoint = Camera.main.ScreenToWorldPoint(screenPoint);

Debug.LogFormat("mousePosition: {0} | zWorldDistanceFromCamera: {1} | worldPoint: {2}",
                Input.mousePosition,
                zWorldDistanceFromCamera,
                worldPoint.ToString("F3"));

(If this isn't working, could you update your question or reply to this post with a comment with details showing the values that are logged at each step?)

ScreenToWorldPoint returning different values at different screen , I'm having a weird issue where everything works fine at 4:3, but at another 16:9​), ScreenToWorldPoint appears to be returning an inaccurate value. At 16:9, clicking at the camera's center (x = 5, y = 3… Unity Courses · Ask. I have a question about Unity's ScreenToWorldPoint. Whenever I use ScreenToWorldPoint(Input.mousePosition) and display the output in console this always gives me the same coordinates. MyCode: Vec

Since using a default camera was returning the correct values, I simply added another one to detect the mouse position independent of the render texture/quad setup.

unity, The problem I'm facing is that the coordinates returned by Camera. agent.​transform.position.y as the value for z on ScreenToWorldPoint . I trying to use Camera.main.ScreenToWorldPoint(), but its returning coordinates of camera, not of the point clicked, doesnt metter if i use static camera or camera fixed on player. I attached this script to CharacterRobotBoy prefab from standard unity assets.

Scripting API: Camera.ScreenToWorldPoint, Returns. Vector3 The worldspace point created by converting the screen space point at the provided distance z from the camera plane. Displaying 3D animation on 2D Canvas in Unity. Displaying 3D animation on 2D Raw Image in Unity-1. Unity - Camera ScreenToWorldPoint returning odd values.

Make a 2D Grappling Hook Game in Unity – Part 1, If you need to level up your Unity skills, work through our tutorials on Getting Started to review Physics Joints in Unity 2D as well and then return to this tutorial. of the mouse cursor using the camera's ScreenToWorldPoint method. You're only interested in the Z value, as you're using a 2D camera, and 

Unity camera screentoworldpoint, Tutorial Unity 5, como utilizar a função Raycast - Physics. How to create a Raycast in Unity - How to return the distance of a gameObject hit by a Raycast. new script called GazeManager to the Camera Rig>Camera(head)>Camera(eye)​. This is odd for me, as far as I. How to Markerless GPS Based Augmented Reality.

Comments
  • Can you post a screenshot of your entire hierarchy window?
  • This question was posted twice. stackoverflow.com/questions/50135097/…
  • @Immorality - The first image is the entire hierarchy window. I'm only testing this with the camera (and children) and the rotating object.
  • Does the rotating object contain a collider/trigger?
  • Unfortunately, as with the comment above, changing the z distance does nothing to change the values output from ScreenToWorldPoint.
  • Can you edit your question with your new current code ?
  • I have no code updates. However, I have another idea that might help so i've updated the question.
  • @EvanWard When people provide answer and it didn't help, instead of replying with "nothing changes" or "it does not work", Edit your question and add a new code you are using after reading the answer. Without that it's not possible to tell what you are doing wrong.
  • Apologies. I did try both examples provided as written as well as trying to change some values/references, though since the outcome was the same in every situation I figured simply showing the code provided was unnecessary.
  • Adjusting the z distance changes nothing in the values logged. I even set up a slider to manually test varying z distances. A z value of -10 ends up logging the same values as a z value of 10 and everything in between.
  • Hmm, could you post a screenshot of your hierarchy window for the single orthographic camera example that is working? As well as the associated code and the logged output at each step? That could help viewers narrow down the problem by comparing your problem vs the single camera case.
  • Also, you say that "hovering over it shows the mouse at (307, 174)", but that you're not sure where the 300+ is coming from. Note that the mouse position is in screen coordinates, and Input.mousePosition returns a value as follows "The bottom-left of the screen or window is at (0, 0). The top-right of the screen or window is at (Screen.width, Screen.height)." Source: Unity Input.mousePosition documentation. So the right of your screen may correspond to 64 for your Rotating Object, but for your mouse it's Screen.width.