- Home /
 
 
               Question by 
               SPIGS · Jan 01, 2014 at 02:47 AM · 
                c#2dangleprojectile  
              
 
              How to get rotation for projectile to fire at cursor?
Ok, I'm creating a 2-d game. I would like my character to be able to shoot projectiles from a spawnpoint in front of him in the direction of the cursor upon clicking on the screen.

I can already spawn the projectile at the spawn point and apply forces to it and make it move. However I do not know how to find the angle for the projectile to be turned to when spawned so that I can apply force and shoot in the direction of the cursor.
Answers Appreciated.
 
                 
                diagram.png 
                (6.7 kB) 
               
 
              
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by robertbu · Jan 01, 2014 at 02:52 AM
 #pragma strict
  
 function Update () {
 
     if (Input.GetMouseButtonDown(0)) {
         var pos = Camera.main.WorldToScreenPoint(transform.position);
         var dir = (Input.mousePosition - pos).normalized;
         // Whatever firing code you want goes here.  You fire 
         //   by using 'dir' for the direction.
     }
 }
 
               The script assumes it is on the spawn point. This code assumes you want to use a left mouse button click to fire the projectile. Modify as appropriate for your game.
Your answer