I have gotten a basic idea on how to attach a game object to my mouse but can’t seem to do it. I have the script attached to a sphere, this is the script that I have at the moment. Im still learning how to script so I still am not good.
#pragma strict
function Update () {
transform.position = Camera.ScreenToWorldPoint (Input.mousePosition);
}
Thanks for reading
You might want to look at this:
Here’s a hint, It’s not the most efficient way but it’s easy to read and understand. It’s in C# code, didn’t really feel like switching to UnityScript 
public class AttachObjectToMouse : MonoBehaviour {
public Transform sphereTransform;
private Vector3 mouseScreenPosition;
private Vector3 mouseWorldPosition;
void Update ()
{
mouseScreenPosition = Input.mousePosition;
mouseWorldPosition = camera.ScreenToWorldPoint(new Vector3(mouseScreenPosition.x,
mouseScreenPosition.y,
camera.nearClipPlane+1)); //The +1 is there so you don't overlap the object and the camera, otherwise the object is drawn "inside" of the camera, and therefore you're not able to see it!
sphereTransform.position = mouseWorldPosition;
}
I’ve done it along the line of your suggestion, so you feel more familiar with this solution approach. It’s not the only one, nor the best.
Thanks i will try these out. Am I doing the right thing by attaching the script to the sphere or do I attach it to something else?
Sorry about that, you have to attach it to the camera, otherwise the reference to the camera component in camera.ScreenToWorldPoint will not work, cause there’s no camera component to reference in your sphere.
Also don’t forget to define the transform of the sphere in the script in the editor by dragging your sphere from the hierarchy!
Sorry, forgot to mention it!
Thank you:) Will try all of this to get it working. Thanks again:)
Ok I have attached the script to my camera on my first person capsul. I also made a varible for the sphere. I am still having trouble getting it to follow though. I have tried to get what I can from the c# script but not to good at reading it. This is what I have now that isnt working.
#pragma strict
var object : GameObject;
function Update () {
object = transfrom.position(Input.mousePosition);
}
Input.mousePosition gives you the 2d position of the mouse on the screen… you need to change that into a 3d position within your scene somehow (which is what BFGames was pointing you towards above, and what I’ve linked you to in the other copy of this thread that you’ve started… )