So right now i have an invisible button in my 3d game scene that when clicked i need to find the right worldspace position to drop the newly instantiated coin. I am close BUT if i don’t click in the center of the button and drop straight down then the prefabs drop in strange places along the x axis. I need to limit the “drop zone” of the instantiated prefab to the width of the button so they only fall onto the playable area of the scene.
//variables
public Button dropButton;
public GameObject coin;
Ray ray;
RaycastHit hit;
// Start is called before the first frame update
void Start()
{
Button button = dropButton.GetComponent<Button>();
button.onClick.AddListener(DropCoin);
}
// Update is called once per frame
void Update()
{
}
public void DropCoin()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
GameObject instantiatedCoin;
instantiatedCoin = Instantiate(coin, new Vector3(hit.point.x, 9.5f, 10.5f), Quaternion.identity) as GameObject;
}
}
}