- Home /
mousePosition.x value type. (Equations involving coordinates)
Hi guys I really hope someone of you could help me.
I am trying to calculate a position for an object to appear based on my mouse clicks. In this picture http://imgur.com/3Wsx3 "C" is where I click mouse and "D" is where I release the button. I want an object to appear on the same line as these points at the top of the screen (Point "A").
I have laid down the equation, that I would need to calculate the "x" value of the point, but I can not figure out how to do it in script. Can mousePosition.x be used as a float and then back as a position for object or are there some another way?
The complicated part is converting a 2D mouse position into a 3D world position. Look up ScreenPointToRay.
I tried this kind of solution:
void Update () {
Vector3 C = Input.mousePosition;
Vector3 D = Input.mousePosition;
float Width = Camera.main.pixelWidth;
float BF = Camera.main.pixelHeight;
Vector3 Cam = new Vector3(Width,BF,0);
float BC;
float CE;
float ED;
Vector3 A;
float AB;
BC = BF - C.y;
CE = C.y - D.y;
ED = D.x - C.x;
AB = BC / CE * ED;
A = new Vector3((C.x - AB),BF,0);
transform.Translate((C.x - AB),BF,0);
}
Now I am getting "transform.position assign attempt for 'Cube' is not valid. Input position is { NaN, NaN, NaN }." I guess it is because of what @syclamoth said - I have not converted 2d coords to 3d world? Would just adding "Camera.main.ScreenToWorldPoint" for certain Vectors be a reasonable solutions?
Have you checked the documentation those functions? Yes, that is a reasonable solution.
Yes I have, but I am not sure I understand them correctly. I have come up with this:
void Update () {
Vector3 C = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
C = Camera.main.ScreenToWorldPoint(C);
Vector3 D = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 0);
D = Camera.main.ScreenToWorldPoint(D);
float Width = Camera.main.pixelWidth;
float BF = Camera.main.pixelHeight;
Vector3 Cam = new Vector3(Width,BF,0);
float BC;
float CE;
float ED;
Vector3 A;
float AB;
BC = BF - C.y;
CE = C.y - D.y;
ED = D.x - C.x;
AB = BC / CE * ED;
A = new Vector3((C.x - AB),BF,0);
transform.Translate((C.x - AB),BF,0);
}
Still no luck. Any idea why?
What exactly is going wrong? Are you getting errors, or is it just not acting the way you expect it to?
Try ScreenPointToRay, if you want it to remain proportional to the screen.
Another thing- don't mix up the 'screen coordinates' and the 'world coordinates'. Once you transform things out into the 'real world' the screen coordinates are useless to you. Things like 'pixelWidth' are meaningless outside of the screen-space coordinates. You should be using the Camera.ScreenToWorldPoint for the result of your equations, not as a method for obtaining some of the inputs. When you have worked out where you want to move your object in terms of the screen, then you can transform those coordinates out into 3D space for the actual object.
Answer by aldonaletto · Jan 13, 2012 at 07:57 PM
Converting a 2D point to 3D world results in a line, not a point. To convert to a point, you must supply an extra information - usually the distance from the camera. In ScreenToWorldPoint(x, y, z), for instance, x and y are the screen coordinates (Input.mousePosition, for instance) and z is the distance from the camera. If you pass z=0, the camera position is returned, no matter which values you pass for x and y.
If you want to move a scene object while holding the mouse button down, you must create a ray from the mouse pointer and use it in Raycast to find the object; while the button is pressed, the object must follow the mouse pointer, and when the button is released the object rests in peace at the last point. There are two main alternatives for the movement: the object moves at a constant distance from the camera, or the object moves in the plane it was when the user clicked it. I answered a question about this last kind of movement in http://answers.unity3d.com/questions/139784/move-and-x-and-z-axis-in-3d.html (and about the constant distance too, but could not find it!)