I have a 3D game and I am trying to make it so that it spawns a gameobject where I click with cam.ScreenToWorldPoint(mousePosition), but whenever I click it just inserts the game object at the position of the camera? Why is this and how can I fix it?
void Update()
{
if (Input.GetKeyDown("mouse 0"))
{
Vector3 point = new Vector3();
Vector3 mousePos = Input.mousePosition;
point = Camera.main.ScreenToWorldPoint(mousePos);
Instantiate(g, point, Quaternion.identity);
}
}
private void Update()
{
if(!Input.GetMouseButtonDown(0)) return;
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out var hit)) {
Instantiate(g, hit.transform.position, Quaternion.identity);
}
}