This repository has been archived on 2024-11-28. You can view files and clone it, but cannot push or open issues or pull requests.
PMC_Projekt/ProjektUnity/Assets/Scripts/Player/Shooting.cs
Marvin 1eeafca3f2 MOD: Enemy AI
MOD: Player Shooting
MOD: Damage Player and Enemy
2024-09-05 20:40:50 +02:00

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);
}
}
}