using UnityEngine; public class PlayerShoot : MonoBehaviour // Bullet speed is dependant on the distance from the player to the mouse pos BUG { public GameObject bulletPrefab; public float bulletSpeed = 25f; void Update() { if (Input.GetMouseButtonDown(0)) // Left mouse button { Shoot(); } } void Shoot() { // Get mouse pos Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition); mousePos.z = -transform.position.z; // Keep same Z as player so bullets are same speed no matter where the mouse is // Direction from player to mouse position Vector2 shootDirection = (mousePos - transform.position).normalized; GameObject bullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity); SoundEffectManager.Play("Shoot"); bullet.GetComponent().linearVelocity = new Vector2(shootDirection.x, shootDirection.y) * bulletSpeed; Destroy(bullet, 2f); // Destroy bullet after 2 seconds } }