- Home /
 
               Question by 
               Mentalist4006 · Nov 12, 2011 at 02:51 AM · 
                camerainstantiatecoordinatesscreentoworldpoint  
              
 
              Need help putting an object at a point based on screen coordinates
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?
               Comment
              
 
               
              Answer by syclamoth · Nov 12, 2011 at 04:10 AM
You are running this inside of Start! At the moment, you would have to press the fire button on the same frame that the game started for the explosion to instantiate. Move that code into the 'Update' function instead, so that it gets polled every frame!
 function Update()
 {
     if(Input.GetButtonDown("Fire1"))
     {
         // blah blah blah boom
     }
 }
Thank you, I did not think about that. It it now working.
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                