Search Unity

Unet: Change Send Speed of Remote Actions

Discussion in 'UNet' started by warrencwwong, Jun 30, 2019.

  1. warrencwwong

    warrencwwong

    Joined:
    Aug 17, 2018
    Posts:
    25
    Hello, I am currently building a multiplayer shooting game. This is my PlayerShoot script:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using System;
    4. using UnityEngine.UI;
    5.  
    6. [NetworkSettings(channel = 1, sendInterval = 0.001f)]
    7. public class PlayerShoot : NetworkBehaviour {
    8.  
    9.     private string PlayerTag = "Player";
    10.    
    11.     [SerializeField]
    12.     private Camera cam;
    13.      
    14.    
    15.     public Gun Currentgun;
    16.  
    17.     public LayerMask mask;
    18.  
    19.     public ParticleSystem MuzzleFlash;
    20.  
    21.     public GameObject HitEffect;
    22.  
    23.     public AudioSource source;
    24.  
    25.     public GameObject gunObject;
    26.  
    27.     public Animation Anim;
    28.  
    29.     private Reload reload;
    30.  
    31.    
    32.     void Start () {
    33.        
    34.         reload = GetComponent<Reload>();
    35.        
    36.         if(cam == null)
    37.         {
    38.             Debug.Log("PlayerShoot: No Camera!");
    39.             this.enabled = false;
    40.         }
    41.      
    42.     }
    43.  
    44.     void Update () {
    45.  
    46.         if (!isLocalPlayer)
    47.         {
    48.             return;
    49.         }
    50.         if(Currentgun != null){
    51.             reload.CurrentGun = Currentgun;
    52.         }
    53.         if (gunObject != null)
    54.         {
    55.             Anim = gunObject.GetComponent<Animation>();
    56.         }
    57.         if (PauseMenu.IsOn)
    58.         {
    59.             return;
    60.         }
    61.         if(Currentgun == null)
    62.         {
    63.             return;
    64.         }
    65.         if(Currentgun.AmmoLeft <= 0)
    66.         {
    67.            
    68.             return;
    69.         }
    70.  
    71.         if(Currentgun.FireRate <= 0)
    72.         {
    73.  
    74.             if (Input.GetButtonDown("Fire1") && Currentgun.AmmoLeft > 0)
    75.             {
    76.                 if (isServer)
    77.                 {
    78.                     CmdShoot();
    79.                     return;
    80.                 }
    81.                 CmdShoot();
    82.  
    83.                
    84.             }
    85.            
    86.             else if (Input.GetButtonUp("Fire1"))
    87.             {
    88.                
    89.                 CancelInvoke("Shoot");
    90.             }
    91.         }
    92.         else
    93.         {
    94.            
    95.             if (Input.GetButtonDown("Fire1")  && Currentgun.AmmoLeft > 0)
    96.             {
    97.                 InvokeRepeating("Shoot", 0f, 1 / Currentgun.FireRate);
    98.             }
    99.             else if (Input.GetButtonUp("Fire1"))
    100.             {
    101.                
    102.                 CancelInvoke("Shoot");
    103.              
    104.             }
    105.         }
    106.      
    107.     }
    108.    
    109.    
    110.     void OnShoot()
    111.     {
    112.         source.Play();
    113.         MuzzleFlash.Play();
    114.     }
    115.  
    116.    
    117.     void OnHit(Vector3 pos, Vector3 normal)
    118.     {
    119.         GameObject go = Instantiate(HitEffect, pos, Quaternion.LookRotation(normal));
    120.         Destroy(go, 2);
    121.     }
    122.    
    123.    
    124.     [Command]
    125.     void CmdShoot()
    126.     {
    127.         RpcShoot();
    128.     }
    129.     [ClientRpc]
    130.     void RpcShoot()
    131.     {
    132.  
    133.         Shoot();
    134.     }
    135.     void Shoot()
    136.     {
    137.         if (Anim != null)
    138.         {
    139.             Anim.Play();
    140.         }
    141.  
    142.         if (Currentgun.AmmoLeft <= 0)
    143.         {
    144.             return;
    145.         }
    146.         reload.DeductAmmo();
    147.         OnShoot();
    148.         RaycastHit _hit;
    149.  
    150.         if (Physics.Raycast(cam.transform.position, cam.transform.forward, out _hit, Currentgun.Range))
    151.         {
    152.             OnHit(_hit.point, _hit.normal);
    153.             if (_hit.collider.tag == PlayerTag)
    154.             {
    155.                 Player1 player = _hit.collider.gameObject.GetComponent<Player1>();
    156.  
    157.                 player.TakeDamage(Currentgun.Damage, transform.name);
    158.  
    159.  
    160.  
    161.             }
    162.         }
    163.     }
    164.  
    165.  
    166.  
    167.  
    168.  
    169.  
    170.  
    171.  
    172. }
    173.  
    This script works fine, but there is a problem: When a client shoots, it goes through the server and the server updates it to all the clients. This process is very slow. When a client presses the "Fire1" input, it would take about 0.5 seconds for him to see the results. Also, as some guns(such as shotguns) need to reload every single time a bullet is fired, but the client would be able to shoot multiple bullets at a time because of the delay. Any workarounds or ways to change the speed of the [command] and the [clientrpc]?
    Thanks in advance. Help would be appreciated.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You're probably using the default value for ConnectionConfig.SendDelay. But you're never going to get the response time down to instantaneous. For your guns that need to reload every time, you should not allow a second shot until the gun has been reloaded after firing the first shot.
     
  3. warrencwwong

    warrencwwong

    Joined:
    Aug 17, 2018
    Posts:
    25
    Thanks Joe