I am trying to get the code to create a new object when clicking in an empty space, but when clicking an existing object, the object should follow the mouse until released. Not sure how to get there. Suggestions are very welcome!
public class clickAddOrDrag : MonoBehaviour {
private int i;
void Start () {
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
print("Mouse is down");
RaycastHit hitInfo = new RaycastHit();
bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
if (hit)
{
//if object hit drag object
print("Hit " + hitInfo.transform.gameObject.name);
Vector3 mPos = Input.mousePosition;
mPos.z = 2.0f;
Vector3 pos = Camera.main.ScreenToWorldPoint(mPos);
hitInfo.transform.position = pos;
}
else
{
//if not hit create object
print("No hit");
Vector3 mPos = Input.mousePosition;
mPos.z = 2.0f;
Vector3 pos = Camera.main.ScreenToWorldPoint(mPos);
GameObject obj = Instantiate(Resources.Load("Prefabs/m"),pos,Quaternion.Euler(90,0,0)) as GameObject;
obj.gameObject.name = "mon (" + i + ")";
i++;
}
}
}
}