- Home /
Drag GameObject with Mouse
I want to be able to drag a game object (a cube, for instance) in 3D space with a mouse (by left click and drag). However, instead of moving the cube up, down, left and right (in X-Y plane), I want to be able to move it forward, backward, left and right (in X-Z plane). The camera is perspective and slightly tilted down to look at the cube.
This is the script I am using now which works only approximate to an effect that I want:
if(Input.GetMouseButton(0)) {
var pos : Vector3 = Input.mousePosition;
pos.z = 10.0f;
var pos2 : Vector3 = Camera.main.ScreenToWorldPoint(pos);
pos2 = Vector3(pos2.x, m_character.transform.position.y, pos2.y);
m_character.transform.position = pos2;
}
When you run this script, the cube moves with a slight offset from where the mouse actually seems to be pointing at. What I am looking for is a drag that accurately follows the mouse pointer, no matter where I click on the screen.
I am fairly new to Unity. So any sort of help will be greatly appreciated!
Thanks much!
Answer by jaycee09 · Feb 28, 2012 at 01:18 AM
Seriously guys, any one with any clue or idea, will be of great help to me!
Thanks!
Your answer
