- Home /
Unity 5 - 2d Instantiate at mouse position
Hello,
I've looked through as many search results as i could find, but there is nothing about Unity 5 2D instantiate at mouse position. The problem is that in my editor Camera.ScreenToWorldPoint only comes up with errors saying 'Camera' does not contain a definition for 'ScreenToWorldPoint'. When I use the code below it instantiates the block VERY far away to the right:
if (Input.GetMouseButtonDown(1)) { var block1 = Instantiate(block, new Vector3(Input.mousePosition.x, Input.mousePosition.y, transform.position.z), Quaternion.identity);
}
Answer by relicright · Jun 23, 2015 at 06:26 AM
Ok, I got lucky and Unity fixed the obsolete version of the code:
camera.main.ScreenToWorldPoint(Input.mousePosition);
To:
Vector3 objPosition = GetComponent().ScreenToWorldPoint(mousePosition);