using UnityEngine; public class ShootToMouseDir2D : MonoBehaviour { public GameObject bullet; public Transform bulletPos; public bool canFire = true; public float timeBetweenFiring = 0.3f; public float bulletSpeed = 250f; public float bulletTime = 5f; Camera mainCam; Vector3 mousePos; float timer; void Start() { mainCam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent(); } void Update() { mousePos = mainCam.ScreenToWorldPoint(Input.mousePosition); Vector3 rotation = mousePos - transform.position; float rotZ = Mathf.Atan2(rotation.y, rotation.x) * Mathf.Rad2Deg; transform.rotation = Quaternion.Euler(0f, 0f, rotZ); if (!canFire) { timer += Time.deltaTime; if (timer > timeBetweenFiring) { canFire = true; timer = 0; } } if (Input.GetButtonDown("Fire1") && canFire) { canFire = false; GameObject go = Instantiate(bullet, bulletPos.transform.position, transform.rotation); //print(go.transform.forward); go.GetComponent().AddForce(go.transform.right * bulletSpeed); Destroy(go, 5f); } } }