How do i parent a gameobject to the mouse

Hey I’m new to unity and I’m a JavaScript programmer, what I’m trying to do is parent a gameobject to the mouse. For example, when i click i want a cube to appear at my mouse and when i move my mouse around i want the cube to follow it until i click again. Please help me with this Ive looked everywhere!

Austen

Here’s a starting point:

http://unity3d.com/support/documentation/ScriptReference/Input-mousePosition.html

This will instantiate an object using a raycast. You’ll want to instantiate the object at the hit.point, not at the transform.position shown in the above example. Also, you’re not really “parenting” the object. I believe there may be a “drag” script in standard assets, but I’m not sure. Haven’t used it. If not, I think you’ll find many postings about the issue of dragging an object at the mouse position. Hope this at least serves a first step.

look into raycasting and you’ll find your answer.

thank you very much for the reply ill look in to dragging and raycasting more.

Sorry for the cryptic answer. Just didn’t have much time. Basically, you have to keep in mind that a mouse cursor is a 2D object, it only moves in the XY planes. Your game world is 3D. So when you click on something and want to interact with it, you have to cast a ray (like a laser beam) from the camera plane straight back along the Z axis into your world and see if it hits something. Then in your code, you figure out what to do with that information (distance, position, what was hit). That’s what the link above does in a nutshell.