Need help with Instantiating a Prefab at a Location

I’m working on an RPG and i need some help with with instantiating a prefab at my mouse location, I have a click to move player script, and instead of clicking LMB to move to my mouse, i want to press “2” to instantiate a prefab at my mouse’s location. If it helps here is my Move Script, im using JavaScript

var smooth:int; 
 
     
 
        private var targetPosition:Vector3;
 
       
 
       
 
        var speed = 60;
 
       
 
         
 
        function Update () {
 
            if(Input.GetKeyDown(KeyCode.Mouse0))
 
            {
 
           
 
            smooth=1;
 
           
 
                var playerPlane = new Plane(Vector3.up, transform.position);
 
                var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 
                var hitdist = 0.0;
 
               
 
                if (playerPlane.Raycast (ray, hitdist)) {
 
                    var targetPoint = ray.GetPoint(hitdist);
 
                    targetPosition = ray.GetPoint(hitdist);
 
                    var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
 
                    transform.rotation = targetRotation;
 
                   
 
                     
 
                }
 
             
 
            }
 
           
 
                var dir:Vector3 = targetPosition - transform.position;
 
    var dist:float = dir.magnitude;
 
    var move:float = speed * Time.deltaTime;
 
    if(dist > move){
 
    transform.position += dir.normalized * move;
 
    } else {
 
    transform.position = targetPosition;
 
    }
 
           
 
           
 
            transform.position += (targetPosition - transform.position).normalized * speed * Time.deltaTime;
 
           
 
         
 
         
 
         
 
         }

I just tried doing that, it does work, but the prefab i am trying to spawn is a small cube, but then it gets created in the game it is in the air and is really big.

If it’s a default cube is it parented to another object?

You can set up a prefabs scale, and then Instantiate them or if you have it in the scene already just adjust it’s scale (and if it’s an imported model you can set it’s import scale factor).

Hope that helps, just have a play when unity is running/paused you can mess with the settings and they are not kept.

Thanks, i got it to work except my problem now is that when i press “Fire2” it spawns at my camera, and not down on the ground, i have a Dota/LoL/Diablo style camera this is my script for instantiating the cube

var testObject : Transform;
 
function Update () 
    {
    if (Input.GetButtonUp ("Fire2")) 
        {
        var mousePos = Input.mousePosition;
        mousePos.z = 2.0;       // we want 2m away from the camera position
 
        var objectPos = Camera.main.ScreenToWorldPoint(mousePos);
 
        var myObject= Instantiate(testObject, objectPos, Quaternion.identity);
        }
    }

How can i change the code so instead of positioning it 2m away from the camera i can spawn it on the ground. Sorry if its a noobish question im not the greatest at js

you can raycast :
http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

Wow i found something that fixed my problem. Incase anyone is having same problem as me, take a look at
http://answers.unity3d.com/questions/13577/using-raycast-to-get-mouse-input.html

Ive ran into a problem, when i instantiate the prefab i made, its a particle system, when its created it is on the same angle as my main camera, how can i change it to be parallel with the ground floor

Check out the Raycast HitInfo there is a normal in there which is the direction you want your system to point in.