Question by
THESUBMATRIX · Apr 23, 2017 at 10:20 PM ·
c#fpsinvoke
Rapid Fire Script issues
using UnityEngine.Networking;
using System.Collections.Generic;
using UnityEngine;
public class PlayerShoot : NetworkBehaviour {
private const string PLAYER_TAG = "Player";
private PlayerWeapon currentWeapon;
[SerializeField]
private Camera playerCam;
[SerializeField]
private LayerMask mask;
private WeaponManager weaponManager;
void Start()
{
if (playerCam == null)
{
Debug.LogError("PlayerShoot: No Camera Reference");
enabled = false;
}
weaponManager = GetComponent<WeaponManager>();
}
void Update()
{
currentWeapon = weaponManager.GetCurrentWeapon();
if(currentWeapon.fireRate <= 0)
{
if (Input.GetButtonDown("Fire1"))
{
Shoot();
}
}
else
{
if (Input.GetButtonDown("Fire1"))
{
InvokeRepeating("Shoot", 0f, 1f / currentWeapon.fireRate);
}
else if (Input.GetButtonUp("Fire1"))
{
CancelInvoke("Shoot"); ;
}
}
}
[Command]
void CmdOnShoot()
{
RpcDoShootEffect();
}
[ClientRpc]
void RpcDoShootEffect()
{
weaponManager.GetCurrentGraphics().muzzleFlash.Play();
}
[Client]
void Shoot()
{
Debug.Log("Shoot");
if (!isLocalPlayer)
{
return;
}
CmdOnShoot();
RaycastHit _hit;
if (Physics.Raycast(playerCam.transform.position, playerCam.transform.forward, out _hit, currentWeapon.range, mask))
{
if(_hit.collider.tag == PLAYER_TAG)
{
CmdPlayerShot(_hit.collider.name, currentWeapon.damage);
}
}
}
[Command]
void CmdPlayerShot (string _playerID, int _damage)
{
Debug.Log(_playerID + " has been shot.");
PlayerManager _player = GameManager.GetPlayer(_playerID);
_player.RpcTakeDamage(_damage);
}
}
The Shoot Method will not Repeat, Why is this?
Comment