OnMouseDown: instantiate a prefab and keep it dragged

Hi all, I don’t know if this kind of question has already been asked.

So I have a 3D Text with a script on it. The desirable behavior is the following:

on the OnMouseDown event, an object should be instantiate in the current position of the mouse. This part is working correctly: a prefab is instantiating in the mouse position.

When the mouse button is still Down I’d like to drag the gameObject already created, in order to move it in another point of the space, until the mouse button return Up.

So far the gameObject is created, but I need to click on the object a second time, in order to start the Drag event.

Is it clear the situation? Can you suggest me some ideas?

Thanks thanks thanks!

In the future, try to post the existing script to start with…

Anyway, here’s how to do this–

basically, when you create the object, you are storing its reference in some var, then use that to access its position… You can use GetMouseButtonDown,GetMouseButton, and GetMouseButtonUp to keep track of the current state of everything:

var objectToInstantiate : GameObject;
private var myCurrentObject : GameObject;

function Update(){
if(Input.GetMouseButtonDown(0)){
myCurrentObject = Instantiate(objectToInstantiate,Input.mousePosition,Quaternion.identity);
    }
if(Input.GetMouseButton(0) && myCurrentObject){
myCurrentObject.transform.position = Input.mousePosition;
    }
if(Input.GetMouseButtonUp(0) && myCurrentObject){
myCurrentObject = null;
    }
}

this is a simple example of how this could work, hope this helps!

EDIT: sorry, just noticed the object is a textmesh… my above answer uses the input mouse position directly, which might work for say, a GUI element, which uses similar coordinates… however, for a normal gameobject, we would need to cast the mouse position to a world position:

var objectToInstantiate : GameObject;
private var myCurrentObject : GameObject;

function Update(){
if(Input.GetMouseButtonDown(0)){
myCurrentObject = Instantiate(objectToInstantiate, camera.ScreenToWorldPoint(Input.mousePosition),Quaternion.identity);
    }
if(Input.GetMouseButton(0) && myCurrentObject){
myCurrentObject.transform.position =  camera.ScreenToWorldPoint(Input.mousePosition);
    }
if(Input.GetMouseButtonUp(0) && myCurrentObject){
myCurrentObject = null;
    }
}

for example
(this script would be attached to your camera… )
also, you may need to adjust the z position in this scenario, which could be done directly after setting the rest, by adding the line:

myCurrentObject.transform.position.z = 10;   // or whatever

immediately after setting the position in GetMouseButton, and also after the Instantiate line…

...
 if(Input.GetMouseButton(0) && myCurrentObject){
    myCurrentObject.transform.position =  camera.ScreenToWorldPoint(Input.mousePosition);
    myCurrentObject.transform.position.z = 10; 
        }
...

Thanks, great! It seems to work!

I’m having only some issues with the z position… but i’ll figure it out :slight_smile:

Thanks!