Search Unity

Instantiate object at mouse position

Discussion in 'Scripting' started by tatelax, May 30, 2011.

  1. tatelax

    tatelax

    Joined:
    Feb 4, 2010
    Posts:
    1,168
    Hello,

    I am not a programmer at all and I have been trying to teach myself how to instantiate an object at the mouse position with X distance away from the main camera. Can someone please clarify how this is done? Keep in mind I have very very little knowledge of javascript.

    -Thanks
     
  2. Quietus2

    Quietus2

    Joined:
    Mar 28, 2008
    Posts:
    2,058
  3. tatelax

    tatelax

    Joined:
    Feb 4, 2010
    Posts:
    1,168
    I have been working on this a little more and have been making some progress. So far I have a script that instantiates cubes when the left mouse button is pressed. Here are the problems with that so far,

    -The cubes do not instantiate at the mouse position
    -The script instantiates a lot of cubes a soon as the mouse button is pressed. I need it to instantiate 1 when the mouse button is pressed.
    -When I instantiate the cubes, I have to be touching an object, because I am casting a ray I believe.
    -The cubes must destroy in 5 seconds after being instantiated.

    IMPORTANT: My game is 2.5D

    Heres what I have:

    Code (csharp):
    1.  
    2. var platform: GameObject;
    3.  
    4. function Update() {
    5.  
    6.      if(Input.GetMouseButton(0))
    7.             // Construct a ray from the current mouse coordinates
    8.         var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    9.         if (Physics.Raycast (ray)) {
    10.             Instantiate (platform, transform.position, transform.rotation);
    11.         }
    12.        
    13.  
    14. }
    15.  
    16. function OnDrawGizmosSelected () {
    17.     var p : Vector3 = camera.ScreenToWorldPoint (Vector3 (100,100,camera.nearClipPlane));
    18.     Gizmos.color = Color.yellow;
    19.     Gizmos.DrawSphere (p, 0.1);
    20. }
    21.  
    Quietus, I got the code you suggested working, but I am not sure how to use that. Thanks!
     
    Last edited: May 30, 2011
  4. tatelax

    tatelax

    Joined:
    Feb 4, 2010
    Posts:
    1,168
    Okay I figured out how to do this! Hopefully someone will be able to use my scripts!

    Code to instantiate object:
    Code (csharp):
    1.  
    2. var testObject : Transform;
    3.  
    4. function Update ()
    5.     {
    6.  
    7.     if (Input.GetButtonDown ("Fire1"))
    8.         {
    9.         var mousePos = Input.mousePosition;
    10.         mousePos.z = 15.0;       // we want 2m away from the camera position
    11.  
    12.         var objectPos = Camera.main.ScreenToWorldPoint(mousePos);
    13.  
    14.         var myObject= Instantiate(testObject, objectPos, Quaternion.identity);
    15.        
    16.  
    17.         }
    18.     }
    19.  
    Code to destroy the object:

    [/CODE]
    var testObject: GameObject;
    var destroyTime = 1;

    function Update () {
    Destroy (testObject, destroyTime);
    }
    [/CODE]