Gui at Mousepoint

Basically, how can I create a GUI button at mouse click, using function OnMouseDown? If it has to be done with raycasts, fine, but I would really prefer with OnMouseDown as that will make everything a lot easier. The easy part is finding a Vector3 of where it should go (i've done that), the hard part is making it appear their in only 'Vector2' form. Here's my script:

var ship : GameObject;
var clicked = false;
var centre = Vector3.zero;

function Start (){          
centre = transform.position;

}

function OnMouseDown (){

clicked = true;
}

function OnGUI () {
if (clicked == true){
    if (GUI.Button (Rect (20,40,80,20), "Orbit")) {
        print ("Orbit!");
        clicked = false;
        gameObject.Find("Ship").SendMessage ("clickedOrbitButton");
        gameObject.Find ("Ship").SendMessage ("planetPosition", centre);

        }
    if (GUI.Button (Rect (20,70,80,20), "Approach")) {
        print ("Approach!");
        clicked = false;
        gameObject.Find("Ship").SendMessage ("clickedApproachButton");
        gameObject.Find ("Ship").SendMessage ("planetPosition", centre);
    }
}
}

If you need to use a transform position vector3 to get the GUI element position, then use WorldToScreenPoint with the Vector3 to get the screen point of the object.

if you're trying to work with the screen coordinates and the mouse coordinates together, then note that screen and mouse have opposing Y coordinates. This can be resolved like so:

var screenMouseY = Screen.height - Input.mousePosition.y;

Why not do do a raycast in the OnMouseDown event?

You might want to have a look at:

Camera.ScreenPointToRay and Input.mousePosition

This is how I have done it with raycasts in C#:

void Update()
{
    //here we cast a ray from camera to floor
    Vector3 rayDirection = camera.transform.TransformDirection(Vector3.forward);
    RaycastHit hit;

    if (Physics.Raycast(camera.transform.position, rayDirection, out hit))
    {
        crosshair.transform.position = new Vector3(hit.point.x, 0.52f, hit.point.z);
    }
}

The camera is an object which has MouseLook but I have constrained it to always look towards the ground.

Hope this helps, this isn't the full code (need to initialise stuff)