ScreenToWorldPoint error

hello, i have a scene with a player prefab that contains a main capsule with various scripts, an orthographic camera that is a child of that capsule, and a (-n obsolete) spawnpoint.

i'm using this bit of code to draw a Gizmo on the screen and calculating where the world coordinates are, and it seems off:

var cam : Camera;
var Val : int = 8;

    function OnDrawGizmosSelected()
     {

         var p : Vector3 = cam.ScreenToWorldPoint(Vector3(Input.mousePosition.x,Input.mousePosition.y, Val));
         Gizmos.color = Color.yellow;
         Gizmos.DrawSphere(p,1);

         }
    function Update()
{
 print(Input.mousePosition);
}

and it looks perfect in the game view, but in the scene view.... it's a little off, and if i were to put a transform.position = ScreenToWorldPoint (my main goal), it would be wrong,

so is there a way to make the screen coords the ACTUAL world coords so that if i put my mouse on an object say at game(world) 0,5,0 in the game mode, then in the scene view, it would move the Gizmo to the scene(world) 0,5,0?

as always, any help is appreciated!

thank you!

I had the same problem. changing the nearClipPlane value to the ‘z’ position value of the camera times -1! really wierd, but it worked:

var target : Transform;function Update () {

var screenPos : Vector3 = Camera.main.WorldToScreenPoint (target.position);
print ("target is " + screenPos.x + " pixels from the left");
print ("target is " + screenPos.y + " pixels from the bottom");
var worldPos : Vector3 = Camera.main.ScreenToWorldPoint(Vector3(screenPos.x,screenPos.y, (Camera.main.transform.position.z*-1)));
print(worldPos);

}

Only thing is that z will always be 0.

The thing is, if you define a point on the screen, in 3D, it defines a line that goes from the camera, through that screen point, and runs out to infinity.

As you’re finding, it doesn’t tell you where to stop. (‘depth’ coordinate)

I haven’t had any experience with this yet, but what I have read about people doing is using a RayCast from the camera, through the Screen Point, and read the point where it hits something in your scene as the point you want.

Does this make any sense?

yes, that definetely makes sense :slight_smile:
thanks for your answer.