Untitled

mail@pastecode.io avatar
unknown
plain_text
14 days ago
3.1 kB
6
Indexable
Never
using System.Collections;
using Mirror;
using StarterAssets;
using UnityEngine;
using UnityEngine.VFX;

public class MirrorShootManager : NetworkBehaviour
{
    public GameObject aimAt;
    public GameObject weaponFirstPlayer;
    public GameObject weaponThirdPlayer;
    private Animator _weaponAnimator;
    private readonly int weaponRPM = 600;
    private float fireRate;
    private StarterAssetsInputs _input;
    private bool _shooting = false;
    private AudioSource[] gunshotSources;
    public AudioClip gunshotClip;
    private int currentSourceIndex = 0;

    [SerializeField]
    private VisualEffect _muzzleFlash;

    private Transform _firePoint;
    public float maxRange = 6;
    public GameObject bulletImpactPrefab;
    public LayerMask layerMask;

    private Camera playerCamera;

    void Start()
    {
        GameObject weapon = isLocalPlayer ? weaponFirstPlayer : weaponThirdPlayer;
        _input = GetComponent<StarterAssetsInputs>();
        _weaponAnimator = weapon.GetComponent<Animator>();
        fireRate = 60f / weaponRPM;
        gunshotSources = weapon.GetComponents<AudioSource>();
        _firePoint = weapon.transform.Find("FirePoint");
        _muzzleFlash = _firePoint.GetComponent<VisualEffect>();
        playerCamera = Camera.main;
    }

    void Update()
    {
        if (!isLocalPlayer)
            return;

        Vector3 newPosition =
            playerCamera.transform.position + playerCamera.transform.forward * maxRange;
        aimAt.transform.position = newPosition;

        if (_input.shoot && !_shooting)
        {
            _shooting = true;
            CmdShoot();
            StartCoroutine(ShootingCooldown());
        }
        if (!_input.shoot && _shooting)
        {
            _shooting = false;
        }
    }

    [Command]
    void CmdShoot()
    {
        RpcPlayShootEffects();
        ShootRaycast();
    }

    [ClientRpc]
    void RpcPlayShootEffects()
    {
        _weaponAnimator.SetTrigger("Shoot");
        PlayGunshotSound();
        _muzzleFlash.Play();
    }

    void ShootRaycast()
    {
        Vector3 rayOrigin = playerCamera.transform.position;
        Vector3 rayDirection = playerCamera.transform.forward;

        if (Physics.Raycast(rayOrigin, rayDirection, out RaycastHit hit, maxRange, layerMask))
        {
            if (hit.collider.CompareTag("Player"))
            {
                if (hit.collider.TryGetComponent<PlayerHealth>(out var playerHealth))
                {
                    CmdDealDamage(playerHealth, 40);
                }
            }
        }
    }

    [Command]
    void CmdDealDamage(PlayerHealth playerHealth, int damage)
    {
        playerHealth.TakeDamage(damage);
    }

    IEnumerator ShootingCooldown()
    {
        yield return new WaitForSeconds(fireRate);
        _shooting = false;
    }

    void PlayGunshotSound()
    {
        gunshotSources[currentSourceIndex].clip = gunshotClip;
        gunshotSources[currentSourceIndex].Play();

        currentSourceIndex = (currentSourceIndex + 1) % gunshotSources.Length;
    }
}
Leave a Comment