What I'm wanting to do is the following:
I've been at this for a while now and have the first two step complete, but I'm at a lose as to how I can get my spawned object to reset its position. Could someone give me a hand?
My code:
void MoveObject()
{
if(aliveObject != null)
{
if (moveObject == true)
{
Vector3 pos = Input.mousePosition;
pos.z = aliveObject.transform.position.z - Camera.main.transform.position.z;
aliveObject.transform.position = Camera.main.ScreenToWorldPoint (pos);
Vector3 targetPositon = Camera.main.ScreenToWorldPoint (pos);
}
else if (moveObject == false)
{
Vector3 p = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y,10.0f));
aliveObject.transform.position = new Vector3(p.x,p.y, 0.0f);
Debug.Log("placed");
}
}
}
void Update ()
{
MoveObject ();
if (Input.GetMouseButtonDown(0))
{
moveObject = false;
}
}
public void DecreaseCount()
{
// called when UI button is clicked
if(count > 1)
aliveObject = (GameObject)Instantiate(desiredObject,new Vector3(0,0, 0.0f),Quaternion.identity);
}
How I would do this:
Public GameObject spawnObject;
void Update ()
{
DragObject (spawnObject);
}
void DragObject (GameObject item)
{
if ( input.GetMouseButtonDown ())
{
Vector3 pos = input.mousePosition;
if (theItem == null)
{
GameObject theItem = Instantiate (item, pos, new Quaternion (0f, 0f, 0f, 0f)) as GameObject;
}
if (theItem != null)
{
theItem.transform.position = pos;
}
}
}
Then it's up to you to destroy the object. If you make a gameobject in Unity then assign it to spawnObject, it should spawn it and stay wherever the mouse goes. And BTW I typed this on my phone, so check the Lower/Upper cases.
Because it is in the update method, it will lock to the mouse's position. I have done some form of this to lock GameObjects to characters positions in my applications.