Advertisement
Munchy2007

Unet5TutPt4PlayerShooting

Feb 21st, 2016
4,330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.01 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.Networking;
  4.  
  5. public class PlayerShoot : NetworkBehaviour {
  6.     [SerializeField] float fireRate = 0.5f;
  7.     LineRenderer laser;
  8.     float nextFireTime;
  9.  
  10.     public override void OnStartLocalPlayer ()
  11.     {
  12.         enabled = true;
  13.     }
  14.  
  15.     public override void OnStartClient ()
  16.     {
  17.         laser = transform.Find("Laser").GetComponent<LineRenderer>();
  18.         laser.enabled = false;
  19.         enabled = false;
  20.     }
  21.  
  22.     void Update () {
  23.         if(Input.GetAxis("Fire1") != 0 && canFire)
  24.         {
  25.             nextFireTime = Time.time + fireRate;
  26.             Fire();
  27.         }
  28.     }
  29.  
  30.     void Fire()
  31.     {
  32.         StartCoroutine(ShowLaser());
  33.         CmdShowLaser();
  34.     }
  35.        
  36.     [Command]
  37.     void CmdShowLaser()
  38.     {
  39.         RpcShowLaser();
  40.     }
  41.  
  42.     [ClientRpc]
  43.     void RpcShowLaser()
  44.     {
  45.         if(isLocalPlayer) return;
  46.             StartCoroutine(ShowLaser());
  47.     }
  48.  
  49.     IEnumerator ShowLaser()
  50.     {
  51.         laser.enabled = true;
  52.  
  53.         yield return new WaitForSeconds(0.1f);
  54.         laser.enabled = false;
  55.     }
  56.  
  57.     public bool canFire {
  58.         get {return Time.time >= nextFireTime;}
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement