How to Parent object to camera OnMouseDown? (javascript)

Hey again.

What I'd like to do here is include a function onmousedown which will parent the object the script is attached to, to the camera. The end effect would be holding the object ala Half Life 2 or other Source games.

As always I'm here to learn, so please don't just include a script; help me understand why it works.

Thanks everyone!

I'd love to explain the code, but your question is so extremely straightforward that it's hard to think of anything that would need further explanation. Just have an if() in your update function that checks if the left mouse button is down, and in case it is - parent the object to the camera.

function Update () {

    if(Input.GetMouseButtonDown(0)) {

        transform.parent = Camera.main;

    }

}

If you are not using the main camera you could declare a camera variable at the top (var cam : Camera) and then through the inspector drag 'n drop the camera into the slot.

But I don't think this is exactly what you want, because attaching this to EVERY object you want to apply this to would be impracticable and slow down your system if you have a lot of them. What I would do is when you get the mouse down cast a ray forward from your mouse into your scene, see if it hit something, check if that object is tagged "whatever" and then parent it. This way you would be able to click on an object to have it follow the camera.


var inHandPosition : Vector3; //where is it when 'in your hand'
var endPosition : Vector3; //where is it when it's on the grill

var objectPrefab : GameObject; //prefab of the to be inst. obj.

var cam : Camera = Camera.main; //change this if you don't use the main cam

private var spawnObject : GameObject;

function Update () {

    if(Input.GetMouseButtonDown(0)) { //did you click?

        var ray : Ray; //the ray you will be casting
        ray = cam.ScreenPointToRay(Vector3(Camera.main.pixelWidth*cursor.transform.position.x,Camera.main.pixelHeight*cursor.transform.position.y,0));
            //this bit is a little confusing pixelWidth/Height is how many pixels large the screen is,
            //cursor.transform.position.x/y is at what percentage of width/height the mouse is,
            //so multiply to get the x/y pixelposition of your cursor, which is where we want the ray to start
            //ScreenPointToRay automatically makes the ray go towards the direction you are facing with your camera from the pixel position

        var hit : RaycastHit //that which will be hit (if any)

        if(Physics.Raycast (ray.origin,ray.direction, hit)) { //true if there is a hit, else false

            if(hit.collider.gameObject.tag == "tag you gave the start object") { //if the object you clicked on is the one you wanted..

                spawnObject = Instantiate(objectPrefab,inHandPosition,Quaternion.Identity);

                spawnObject.transform.parent = cam.transform;

            }

            else if(hit.collider.gameObject.tag == "tag you gave the end object" && spawnObject.transform.parent == cam.transform) { //if you have the object in-hand and you click on the target

                spawnObject.transform.parent = hit.collider.gameObject;

                spawnObject.transform.position = endPosition;

            }

        }

    }

}

There you go. Enjoy!

you know for the top simple code it says it cannot convert the main camera to transform