Dragging Objects with the mouse


I want to be able to move a Object with the mouse, like in a TowerDefense game adding turrets. I am just learning javascript so I dont know too much, The API is great, but I cant let it work, this is my current code:

 var turret : Rigidbody;

function Update () {

turret.transform.position = Input.mousePosition;


function OnMouseDown () {


   var placement : Rigidbody;

   placement =  Instantiate (turret, transform.position, transform.rotation);



Why dont you try something like this? :

var turret : GameObject;   
var viewHit : RaycastHit;
function Update(){
        if(Physics.Raycast(transform.position, transform.TransformDirection(Vector3.forward), viewHit, 100)){  //100 is the range
                Instantiate(turret, viewHit.point, Quaternion.identity);// instantiate the turret at the point where the raycast hits
}// update ends

This code should go to your camera that you use, the game object ‘turret’ can have a rigidbody component. Hope this helps! Tell me if it doesnt I am not on my comp right now so not 100% sure :slight_smile: