Hi
I have a public GameObject
public GameObject cube;
How could I Drag that public GameObject?
My code:
float distance = 100;
public GameObject cube;
void OnMouseDrag()
{
Vector3 mousePosition = new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance);
Vector3 objPosition = Camera.main.ScreenToWorldPoint(mousePosition);
transform.position = objPosition;
}
Thank You
Right now you’re doing transform.position, which means whatever object this script is on is what will move.
You need to target the cube.
cube.transform.position
However, you might want to look into OnDrag, OnEndDrag, OnBeginDrag. Those might have better results for you. But for your current code, yep. Just target the cube.
I assume the gameObject isn’t the one this script is on. That’s a problem because OnMouseDrag is made for the game object that needs to be dragged. The easiest thing to do would be to put that script on the cube game object that needs to be dragged and give it a boolean variable for when you want it to be dragged. Then change the boolean from this script. You would still need to have the cursor over the cube object, not the object this script is on.