I am making a 2d game where I need my player to fire towards my mouse position when the space key is pressed.
I already have the shooting that works, but the aiming towards the mouse is where I get stuck.
Thank you very much for your help!
using UnityEngine;
using System.Collections;
public class PlayerShooting : MonoBehaviour
{ public GameObject bulletPrefab;
public Transform bulletSpawn;
public float coolDown = 3f;
void Update ()
{
//print ("the cooldown time is " + coolDown);
CoolDownTimer();
if(coolDown <= 0)
{
if(Input.GetKeyDown(KeyCode.Space))
{
Instantiate(bulletPrefab,bulletSpawn.position,bulletSpawn.rotation);
coolDown = 1f;
}
}
}
void CoolDownTimer()
{
coolDown -= Time.deltaTime;
}
}