mouse: position: screen to world coordinates

Is there a command that will convert this? Of course, understanding that it is moving from 2D to 3D, (perhaps I can chose the axis’).

I noticed a few in reference but none precisely for what I want unless, hopefully I missed it.

Thanks
Ren

1 Like

Hello!Perhaps Camera.main.ScreenToWorldPoint(Input.mousePosition) is what you are looking for?

More info here : Unity - Scripting API: Camera.ScreenToWorldPoint

Good luck!

5 Likes

Ok.
Yes, I sw that one but was hestent to use it since it had a Camera pointer.
But I tried it out, however my suspicions were confirmed, at least for what I am doing…

///
first I tried to pop this into my players controls…

function Update()
{
	if(Input.GetMouseButtonUp(0))
	{
		var p : Vector3 = Camera.ScreenToWorldPoint (Input.mousePosition);
}
}

… But since it tries to access Camera and none exists on this body.

///
so, I removed “Camera” and replaced it with rigidbody. No dice then transform. No dice.

///
So, I moved it into the camera follow script… however I get the message.

Unsure what to make of it.

:?

If you attach that script to something else than the camera,you will have to call it as Camera.main,else if you call it just “Camera” it wont work.And Tag your main camera with “Main camera”.Or you can try to call it from a variable,but you will need to define it as var camera1 : Camera; and assign it in your editor.

This is why you got that error.

Ok, great I got it working, thank you. Camera.main, was what I needed to call.

It is a bit disappointing tho since no matter where I click, “camPos2MousePos” == “camera.position”. This is not what I want. I want/expect the camPos2MousePos to vary dependant on the mouse clicked position.

Unsure.

	if(Input.GetMouseButtonUp(0))
	{
		var camPos2MousePos : Vector3 = camera.main.ScreenToWorldPoint (Input.mousePosition);
		print("camPos2MousePos " + camPos2MousePos);
		print("camera.position " + camera.main.transform.position);
	 	print("Input.mousePosition (pixels coordinates) " +  Input.mousePosition);
	 	print("transform.position (camera position world space)" + transform.position); 
	}

Aw,then if your z coord is not defined,you will need to use this :

var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);

(A raycast)

And use Unity - Scripting API: RaycastHit to determine where the raycast hit the object

var particle : GameObject;
function Update () {
if (Input.GetButtonDown ("Fire1")) {
// Construct a ray from the current mouse coordinates
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray,hit)) {
// Create a particle if hit
Instantiate (particle, hit.point, transform.rotation);
}
}
}

Good luck!

Hmm,
thanks. I went over it, tested the command but i get something peculiar returned regarding the rays direction.

For instance, I have my camera motionless. I click the top left corner and it returns a value of the ray direction of (-0.2, 0.3, 0.9). Then I click the top right, and I get a reading (0.2, 0.3, 0.9). How could this be?? How come it returns exact, precise, reflected values when in fact I am clicking un-mirroring vectors?

bump?

It’s easy.It is actually mirroring on the x coord.Its origin is from the camera and its destination is where it hits.

I understand that. My question is based on this…
For instance, say I first click at pos (-2,1,0) it would read (again, example, not at computer) direction(-2,1,0). Then I click ( 3, 3, 0) and it will print direction (2,1,0)

This does not make any sense unless rays only move in quadrants, which I doubt. I know I must be missing something. Someone please explain.

Thanks.

I am pretty sure that must be : (camera.transform.position - hit.point).normalized

Btw,if talking of directions,it cannot be larger than 1 or smaller than -1.So if you get such results something must be wrong

use Camera.main.ScreenToWorldPoint(Input.mouse.Position)