Placing object on mouse down

Hi there,

I have a script that places objects:

var ObjectToPlace : Transform;



function Update () {

    if(Input.GetButtonDown("Fire1"))

    {

        var ray = Camera.main.ScreenPointToRay (Input.mousePosition); 

        var hit : RaycastHit; 

        if (Physics.Raycast (ray, hit)) 

            Instantiate(ObjectToPlace, hit.point, Quaternion.identity); 

    }

}

How do I extend this functionality so that the object appears on mouse down (so it follows your mouse across geometry where your mouse is, so you can see where it will be placed) and is placed static on mouse up.

Thanks for the help :slight_smile:

Not sure but i believe hit will do. Hit.point is unessecary.

ScreenToWorldPoint i think, see many do it. But havent got it to work myself.

http://forum.unity3d.com/threads/116381-ScreenPointToRay

You’ll see working examples here.

I tested your code also,
Object placement & pickup with mouse « Unity Coding – Unity3D (see webplayer)

Thanks guys!

Hey mgear, that’s really cool of you to make a demo! and with added feature!

When I use your script the sphere moves towards the camera repeatedly when the mouse is down. Maybe it’s because I havn’t upgraded to 3.5 yet?

Oh, yes…
the sphere needs to be on layer 8

inspector, layer dropdown, add layer, give some name to layer 8
back to sphere inspector, assign layer 8