using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerShoot : MonoBehaviour { // The bullet you want to shoot // Requires : Rigidbody2D public GameObject bulletPrefab; // Speed for the bullet to shoot out at public float bulletSpeed = 1; // Delay between shots public float shootDelay = 2; // Stores the time since the last shot private float timer = 0; Vector3 aimPosition; void Update() { // First we find where the mouse is looking at by using a function on the camera // Then set the Z to zero because we are 2D aimPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position; aimPosition.z = 0; // Then we find the angle our player should be at and look there //float angle = Mathf.Atan2(aimPosition.y, aimPosition.x) * Mathf.Rad2Deg; //transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward); // The time since the last shot has been longer than the delay if (timer > shootDelay) { // Is the left mouse key down if (Input.GetKey("mouse 0")) { // Make sure aimPosition is between -1 and 1 aimPosition.Normalize(); // Shoot the bullet ShootBullet(aimPosition); // Reset the timer timer = 0; } } // Increment the timer every update timer += Time.deltaTime; } // Shoots a bullet in the given direction void ShootBullet(Vector2 direction) { // Create a bullet at the players current position GameObject bullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity); // Make sure the bullet does not hit the player // You may have to change the two collider types depending // on what you are using Physics2D.IgnoreCollision(bullet.GetComponent(), GetComponent()); // Add force to the bullet so it flies off Rigidbody2D rb = bullet.GetComponent(); //bullet.transform.rotation.SetLookRotation(rb.velocity); rb.AddForce(direction * bulletSpeed); Vector2 v = new Vector2(aimPosition.x, aimPosition.y); float angle = Mathf.Atan2(v.y, v.x) * Mathf.Rad2Deg; bullet.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward); } }