using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { [SerializeField] private float speed; public Camera sceneCamera; // Gun Variables [SerializeField] private GameObject bullet; [SerializeField] private Transform firePoint; [Range(0.1f, 2f)] [SerializeField] private float fireRate = 0.5f; private Rigidbody2D rb; private float moveX; private float moveY; private float fireTimer; private Vector2 mousePos; private void Start() { rb = GetComponent(); } private void Update() { moveX = Input.GetAxisRaw("Horizontal"); moveY = Input.GetAxisRaw("Vertical"); mousePos = sceneCamera.ScreenToWorldPoint(Input.mousePosition); float angle = Mathf.Atan2(mousePos.y - transform.position.y, mousePos.x - transform.position.x) * Mathf.Rad2Deg - 90f; transform.localRotation = Quaternion.Euler(0, 0, angle); if (Input.GetMouseButtonDown(0) && fireTimer <= 0f) { Shoot(); fireTimer = fireRate; } else { fireTimer -= Time.deltaTime; } } private void FixedUpdate() { rb.velocity = new Vector2(moveX, moveY).normalized * speed; } private void Shoot() { Instantiate(bullet, firePoint.position, firePoint.rotation); } private void OnCollisionEnter2D(Collision2D other) { if (other.gameObject.CompareTag("EnemyBullet")) { Destroy(gameObject); } } }