Hi, I’m needing help with moving a gameobject on a 2d plane in front of the camera (using position.x and position.y). I am updating mouseposition and trying to assign the variables with a gameobject but I seem to be missing something. Over the many codes I’ve tried, all of them did not work.
1st: I run the item pickup function: This instantiates the item and makes it a child object and positions the item relative to the parent.
void RunItemPickup(int itmp) {
string modelstring = pinv.invItemModels[itmp].ToString();
if(!pickupitemmodel) {
pickupitemmodel = (GameObject)Instantiate(Resources.Load(modelstring));
}
pickupitemmodel.transform.parent = (GameObject.Find("ItemPickupOrigin").transform);
pickupitemmodel.transform.position = (GameObject.Find("ItemPickupOrigin").transform.position);
pickupitemmodel.transform.localScale = new Vector3(.1f,.1f,.1f);
pickupitemmodel.transform.rotation = new Quaternion();
}
2nd: I assign the position of the gameobject to the mouse position. This is the code I am having issues with.
void ItemPickupMove() {
if(pickupTrigger == true) {
pickupitemmodel.transform.position = Camera.main.ScreenToWorldPoint(mousePos);
}
}
This does not seem to work, and depending on the code it either sets the position to the camera, or instantiates the item and translates the position infinitely. So in short, I need help with a code that moves the gamobject on screen and remains static in the z-position, like moving an item in your inventory. Any ideas or thoughts?