- Home /
The question is answered, right answer was accepted
Answer by fafase · Dec 13, 2013 at 12:10 PM
Because your mouse is the screen position while your ray origin is the world position. The fact that y is the same is just a coincidence that your camera happen to be positioned properly for this.
Your ray position is depending on the position of the camera in the world, the Input.mousePosition is always a value between 0 and width/height of the screen.
The method you use is converting the screen position to world position. Then it depends what you want to do. With that conversion you have a x and a y, you now need to define the z position. So either you have a plane at some point and cast a ray using:
RaycastHit hit;
Physics.Raycast(ray, out hit);
cube.position = hit.point;
You may want to modify the position a little or your cube will be halfway inside the plane (try and you'll see) or you define where the cube should be in 3D:
Ray mousePosition;
mousePosition = Camera.main.ScreenPointToRay(Input.mousePosition);
cube.position = mousePosition.origin + mousePosition.direction * distance;
Both are pretty much doing the same thing, the second might be faster as I would think there are less calculation.
Follow this Question
Related Questions
Directional mouse movement independent from position? 1 Answer
How can I treat mouse movement as joystick input? 1 Answer
Raycast onto Mesh Collider unreliable. 0 Answers
Player should move to cursor 0 Answers
Axis : top down shooter help 1 Answer