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.
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.