Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Placing object on mouse down

Discussion in 'Scripting' started by GizmoBradwell, Jan 8, 2012.

  1. GizmoBradwell

    GizmoBradwell

    Joined:
    Dec 27, 2010
    Posts:
    67
    Hi there,

    I have a script that places objects:

    Code (csharp):
    1. var ObjectToPlace : Transform;
    2.  
    3.  
    4.  
    5. function Update () {
    6.  
    7.     if(Input.GetButtonDown("Fire1"))
    8.  
    9.     {
    10.  
    11.         var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    12.  
    13.         var hit : RaycastHit;
    14.  
    15.         if (Physics.Raycast (ray, hit))
    16.  
    17.             Instantiate(ObjectToPlace, hit.point, Quaternion.identity);
    18.  
    19.     }
    20.  
    21. }
    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 :)
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,700
    Not sure but i believe hit will do. Hit.point is unessecary.
     
  3. snortop

    snortop

    Joined:
    Dec 16, 2011
    Posts:
    194
  4. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,700
    Last edited: Jan 8, 2012
  5. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,466
  6. GizmoBradwell

    GizmoBradwell

    Joined:
    Dec 27, 2010
    Posts:
    67
    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?
     
  7. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,466
    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