49 lines
1.2 KiB
C#
49 lines
1.2 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
public class Shooting : MonoBehaviour
|
|
{
|
|
private Camera mainCam;
|
|
private Vector3 mousePos;
|
|
|
|
public GameObject bulletPref;
|
|
public Transform bulletTransform;
|
|
public bool canFire;
|
|
private float timer;
|
|
|
|
public PlayerStats playerStats;
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
mainCam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
|
|
playerStats = gameObject.GetComponentInParent<PlayerStats>();
|
|
}
|
|
|
|
// Update is called once per frame
|
|
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(0, 0, rotZ);
|
|
|
|
if(!canFire){
|
|
timer += Time.deltaTime;
|
|
if(timer > playerStats.fireRate){
|
|
canFire = true;
|
|
timer = 0;
|
|
}
|
|
}
|
|
|
|
if(Input.GetMouseButton(0) && canFire){
|
|
canFire = false;
|
|
Instantiate(bulletPref, bulletTransform.position, Quaternion.identity);
|
|
}
|
|
}
|
|
}
|