Have a GameObject following the mouse

Hey,

I have just started to learn some game development, itโ€™s fun and def. a lot different that my daywork where i do WPF all day :slight_smile:

But back to my, properly a trivial problem; i have a small GameObject and i want it to follow my mouse around on the screen.
I have the mouse position now, but that seems to be relative to both my screen, not the viewport of the game. Which just place the GameObject somewhere near russia in the real world :slight_smile:
And when i move the mouse around, i do move the GameObject, but i just want it to be right under the mouse.

I hope you can understand my problem and my lousy english :slight_smile:
Dont hesitate to ask me for further question if its not clear what i want to achieve

/bjarne

If your camera is static and looking down at the Z and your game object is not on any ground you can simply do:

 Vector3 mouse = Input.mousePosition;
// how far away from the camera
mouse.z = 20.0f;
gameObject.transform.position = Camera.main.ScreenToWorldPoint(mouse);

If you want it to follow some ground, you will need to do some raycasting from the camera.