Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.2 kB
2
Indexable
Never
using UnityEngine;
public class PlayerShooting : MonoBehaviour
{
    Animator animator;
    public GameObject prefab;
    public GameObject shootPoint;
    public ParticleSystem muzzleEffect;
    public AudioSource shootSound;
    public int bulletsAmount;
    float lastShootTime;
    public float fireRate;
    void Awake()
    {
        animator = GetComponent<Animator>();
    }
    void Update()
    {
        if (Input.GetKey(KeyCode.Mouse0) && bulletsAmount > 0 &&
        Time.timeScale > 0)
        {
            animator.SetBool("Shooting", true);
            var timeSinceLastShoot = Time.time - lastShootTime;
            if (timeSinceLastShoot < fireRate)
                return;
            lastShootTime = Time.time;
            bulletsAmount--;
            muzzleEffect.Play();
            shootSound.Play();
            GameObject clone = Instantiate(prefab);
            clone.transform.position = shootPoint.transform.position;
            clone.transform.rotation = shootPoint.transform.rotation;
        }
        else
        {
            animator.SetBool("Shooting", false);
        }
    }
}