I want to get a certain pixel on the screen and be a certain distance from the camera. After pressing the mouse button, I instantiate a prefab at coordinates that would be at the defined pixel and at the defined distance. Here’s the code I have so far:
var preferredPos: Vector3;
var explosion: GameObject;
function Start() {
var newPos = Camera.main.ScreenToWorldPoint(preferredPos);
if (Input.GetButtonDown("Fire1")){
Instantiate(explosion, newPos, Quaternion.identity);
}
}
When I try doing this, it doesn’t work. What is the best solution to the code to make this work?