I’ve been trying to get a turret working. The turret is 2d and simply has to shoot towards where the mouse is pointed.
Here is my code so far:
var prefabShot:Transform;
function Update () {
var x1 = Input.mousePosition.x;
var y1 = Input.mousePosition.y;
var x2 = screenPos.position.x;
var y2 = screenPos.position.y;
var angle = Mathf.Atan2(y2 - y1,x2 - x1);
transform.rotation = Quaternion.Euler(0,0,angle);
if(Input.GetButtonDown("Shoot"))
{
Instantiate(prefabShot, transform.position + Vector3.right*2, Quaternion.Euler(0, 0, 90));
}
}