Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.Networking;
- public class PlayerShoot : NetworkBehaviour {
- [SerializeField] float fireRate = 0.5f;
- LineRenderer laser;
- float nextFireTime;
- int score;
- public override void OnStartLocalPlayer ()
- {
- enabled = true;
- }
- public override void OnStartClient ()
- {
- laser = transform.Find("Laser").GetComponent<LineRenderer>();
- laser.enabled = false;
- enabled = false;
- }
- void Update () {
- if(Input.GetAxis("Fire1") != 0 && canFire)
- {
- nextFireTime = Time.time + fireRate;
- Fire();
- }
- }
- void Fire()
- {
- Ray ray = new Ray(laser.transform.position,transform.forward);
- RaycastHit hit;
- if(Physics.Raycast(ray, out hit, 10f))
- {
- var hitPlayer = hit.collider.gameObject;
- CmdDoShotSomeone(gameObject, hitPlayer);
- }
- StartCoroutine(ShowLaser());
- CmdShowLaser();
- }
- [Command]
- void CmdDoShotSomeone(GameObject fromPlayer, GameObject hitPlayer)
- {
- hitPlayer.GetComponent<HealthAndDamage>().TakeDamage(fromPlayer);
- }
- [Command]
- void CmdShowLaser()
- {
- RpcShowLaser();
- }
- [ClientRpc]
- void RpcShowLaser()
- {
- if(isLocalPlayer) return;
- StartCoroutine(ShowLaser());
- }
- IEnumerator ShowLaser()
- {
- laser.enabled = true;
- yield return new WaitForSeconds(0.1f);
- laser.enabled = false;
- }
- public bool canFire {
- get {return Time.time >= nextFireTime;}
- }
- [Server]
- public void AddScore()
- {
- RpcAddScore();
- }
- [ClientRpc]
- void RpcAddScore()
- {
- if(isLocalPlayer)
- {
- score+=5;
- HUD.instance.DisplayScore(score);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement