Search Unity

Question UNET weapon picking up

Discussion in 'UNet' started by theCyberStallion, Apr 1, 2021.

  1. theCyberStallion

    theCyberStallion

    Joined:
    Oct 3, 2020
    Posts:
    29
    I am developing a game called cyberlight because my friend wanted to see a multiplayer game come out of me for once. i got pretty far, and i got stuck on picking up weapons. i only need a few kinds so thats why theyre hard coded. after i tested it without networking, it was fine. if i added networking however, it just wouldnt work. i cant pick up weapons and the weapons wouldnt even sync up as in positionally. here's the grab code, aka the camera code.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. public class camera : NetworkBehaviour
    7. {
    8.     public LayerMask weapon, player1;
    9.     GameObject inHand;
    10.     public camera cam1;
    11.     public float ZOOM, distance;
    12.     public int ak47Damage, pistolDamage, shotgunDamage, awpDamage, rocketDamage, healthcoinHealth;
    13.     public int ak47Ammo, pistolAmmo, shotgunAmmo, awpAmmo, rocketAmmo;
    14.     int currentak47Ammo, currentpistolAmmo = 5, currentshotgunAmmo, currentawpAmmo, currentrocketAmmo;
    15.     int currentak47Mags = 5, currentpistolMags = 5, currentshotgunMags, currentawpMags, currentrocketMags;
    16.     public int ak47ReloadTime, pistolReloadTime, shotgunReloadTime, awpReloadTime, rocketReloadTime;
    17.     public GameObject player, GunPosition, recoilpos, aimpos, spark;
    18.     bool hasitem;
    19.     float firetimer;
    20.     ParticleSystem ps;
    21.     bool isaiming;
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.         Cursor.lockState = CursorLockMode.Locked;
    26.         hasitem = false;
    27.         gameObject.GetComponent<Camera>().fieldOfView = 60;
    28.        
    29.    
    30.     }
    31.  
    32.     // Update is called once per frame
    33.     void Update()
    34.     {
    35.        
    36.         if (!isLocalPlayer)
    37.         {
    38.             // exit from update if this is not the local player
    39.             return;
    40.         }
    41.  
    42.         if (Input.GetKeyDown(KeyCode.E))
    43.         {
    44.             RaycastHit hit;
    45.             if (Physics.Raycast(transform.position, transform.forward, out hit, 3.5f, weapon))//raycast from the center of the screen
    46.             {
    47.                 Debug.Log("hit!");
    48.  
    49.                 if (hasitem)
    50.                 {// if i already have an item, drop it, then grab the new one
    51.                     inHand.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
    52.  
    53.                     inHand.GetComponent<Rigidbody>().isKinematic = false;
    54.                     inHand.GetComponent<Rigidbody>().detectCollisions = true;
    55.                     inHand.GetComponent<Rigidbody>().velocity = transform.forward * 5f;
    56.                     inHand.GetComponent<Rigidbody>().angularVelocity = new Vector3(13, 5, 20);
    57.                     inHand = hit.collider.gameObject;
    58.                 }
    59.                 else
    60.                 {//otherwise, grab the new one
    61.                     inHand = hit.collider.gameObject;
    62.                     inHand.GetComponent<Rigidbody>().isKinematic = true;
    63.                     hasitem = true;
    64.                 }
    65.             }
    66.             else if (hasitem)
    67.             {//if i did not hit, my guy still pressed E so i guess ill drop the current weapon
    68.                 inHand.GetComponent<Rigidbody>().velocity = transform.forward * 5f;
    69.  
    70.                 inHand.GetComponent<Rigidbody>().isKinematic = false;
    71.                 inHand.transform.parent = null;
    72.                 inHand.GetComponent<Rigidbody>().detectCollisions = true;
    73.                 inHand.GetComponent<Rigidbody>().angularVelocity = new Vector3(0.1f, 0.1f, 0.1f);
    74.                 inHand.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
    75.                 inHand.GetComponent<Rigidbody>().velocity = transform.forward * 5f;
    76.                 inHand.GetComponent<Rigidbody>().angularVelocity = new Vector3(13, 5, 20);
    77.                 inHand = null;
    78.                 gameObject.GetComponent<Camera>().fieldOfView = 60;
    79.  
    80.                 hasitem = false;
    81.             }
    82.  
    83.         }
    84.         if (hasitem)
    85.         {//if i have a weapon, move it to the hand position.
    86.             inHand.GetComponent<Rigidbody>().velocity = Vector3.zero;
    87.         inHand.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezeAll;
    88.         inHand.GetComponent<Rigidbody>().isKinematic = true;
    89.         inHand.GetComponent<Rigidbody>().detectCollisions = false;
    90.             if (!isaiming)
    91.         { // if i am not aiming, move it to the default hand position
    92.             inHand.transform.position = Vector3.Slerp(inHand.transform.position, GunPosition.transform.position, Mathf.Clamp(25f * Time.deltaTime, 0, 0.7f));
    93.             gameObject.GetComponent<Camera>().fieldOfView = Mathf.Lerp(gameObject.GetComponent<Camera>().fieldOfView, 60, 0.1f);
    94.                 inHand.transform.rotation = Quaternion.Slerp(inHand.transform.rotation, GunPosition.transform.rotation, Time.deltaTime * 10);
    95.             }
    96.         else
    97.         {// if i AM aiming, move it to the aim position.
    98.             inHand.transform.rotation = Quaternion.Slerp(inHand.transform.rotation, GunPosition.transform.rotation, Time.deltaTime * 10);
    99.             gameObject.GetComponent<Camera>().fieldOfView = Mathf.Lerp(gameObject.GetComponent<Camera>().fieldOfView, ZOOM, 0.1f);
    100.         }
    101.             ps = inHand.GetComponent<ParticleSystem>();
    102.         }
    103.        
    104.        
    105.        
    106.  
    107.         transform.localRotation = Quaternion.Lerp(transform.localRotation, Quaternion.Euler(0, 0, 0), 10f * Time.deltaTime);
    108.  
    109.  
    110.         //  |-----------------------------------------------------------------------|
    111.         //  |-----------------------------WEAPONSYSTEM------------------------------|
    112.         //  |-----------------------------------------------------------------------|
    113.  
    114.  
    115.         RaycastHit bullet;
    116.         if (!(firetimer < 1))
    117.             firetimer -= 1 * Time.deltaTime;
    118.         if (hasitem)
    119.         {
    120.             if (inHand.tag == "pistol")
    121.                 if (Input.GetButtonDown("Fire1") && firetimer < 1)
    122.                 {
    123.                    
    124.                     if (currentpistolAmmo != 0)
    125.                     {
    126.                         ps.Play();
    127.                     Debug.Log("FIRE");
    128.                     inHand.transform.position = recoilpos.transform.position;
    129.                     inHand.transform.rotation = recoilpos.transform.rotation;
    130.                     firetimer = 0.25f;
    131.                     transform.localRotation = Quaternion.Euler(transform.localRotation.x - 0.5f, transform.localRotation.y, transform.localRotation.z);
    132.                     if (Physics.Raycast(aimpos.transform.position, transform.forward, out bullet, 9999999f))
    133.                     {
    134.                         if (bullet.collider.gameObject.layer == player1)
    135.                         {
    136.                             bullet.collider.gameObject.GetComponent<AI_bot>().Hp -= pistolDamage;
    137.                         }
    138.                         else
    139.                         {
    140.  
    141.                             Instantiate(spark, bullet.point, Quaternion.Euler(Vector3.zero));
    142.                         }
    143.                         currentpistolAmmo -= 1;
    144.  
    145.                     }
    146.                      
    147.                    
    148.                     } else
    149.                         {
    150.                             Reload();
    151.                         }
    152.  
    153.                 }
    154.             if (inHand.tag == "ak47")
    155.                 if (Input.GetButton("Fire1") && firetimer < 1)
    156.                 {
    157.                     ps.Play();
    158.                     Debug.Log("FIRE");
    159.                     inHand.transform.position = recoilpos.transform.position;
    160.                     inHand.transform.rotation = recoilpos.transform.rotation;
    161.                     firetimer = 1.125f;
    162.                     transform.localRotation = Quaternion.Euler(transform.localRotation.x + Random.Range(-2f, 2f), transform.localRotation.y + Random.Range(-2f, 2f), transform.localRotation.z);
    163.                     if (Physics.Raycast(aimpos.transform.position, transform.forward, out bullet, 9999999f))
    164.                     {
    165.                         if (bullet.collider.gameObject.layer == player1)
    166.                         {
    167.                             bullet.collider.gameObject.GetComponent<AI_bot>().Hp -= ak47Damage;
    168.                         }
    169.                         else
    170.                         {
    171.  
    172.                             Instantiate(spark, bullet.point, Quaternion.Euler(Vector3.zero));
    173.                         }
    174.                     }
    175.                 }
    176.             if (inHand.tag == "rpg")
    177.                 if (Input.GetButton("Fire1") && firetimer < 1)
    178.                 {
    179.                     ps.Play();
    180.                     inHand.GetComponent<Rlauncher>().Fire = true;
    181.                     inHand.gameObject.transform.gameObject.transform.gameObject.transform.localPosition = inHand.transform.gameObject.transform.gameObject.transform.position;
    182.                     Debug.Log("FIRE");
    183.  
    184.                     inHand.transform.position = recoilpos.transform.position;
    185.                     inHand.transform.rotation = recoilpos.transform.rotation;
    186.                     firetimer = 4;
    187.                     transform.localRotation = Quaternion.Euler(transform.localRotation.x - 2f, transform.localRotation.y, transform.localRotation.z);
    188.  
    189.                 }
    190.             if (inHand.tag == "mg")
    191.                 if (Input.GetButtonDown("Fire1") && firetimer < 1)
    192.                 {
    193.                     if (Physics.Raycast(aimpos.transform.position, transform.forward, out bullet, 9999999f))
    194.                     {
    195.                         if (bullet.collider.gameObject.layer == player1)
    196.                         {
    197.                             bullet.collider.gameObject.GetComponent<AI_bot>().Hp -= shotgunDamage;
    198.                         }
    199.                         else
    200.                         {
    201.  
    202.                             Instantiate(spark, bullet.point, Quaternion.Euler(Vector3.zero));
    203.                         }
    204.                     }
    205.  
    206.                     ps.Play();
    207.  
    208.                     Debug.Log("FIRE");
    209.                     inHand.transform.position = recoilpos.transform.position;
    210.                     inHand.transform.rotation = recoilpos.transform.rotation;
    211.                     firetimer = 1.6f;
    212.                     transform.localRotation = Quaternion.Euler(transform.localRotation.x - 17f, transform.localRotation.y, transform.localRotation.z);
    213.  
    214.                 }
    215.             if (inHand.tag == "sniper")
    216.             {
    217.  
    218.                 if (Input.GetButtonDown("Fire2"))
    219.                 {
    220.                     isaiming = true;
    221.                     player.GetComponent<player>().sensMultiplier = 0.125f;
    222.                 }
    223.                 if (Input.GetButtonUp("Fire2"))
    224.                 {
    225.                     isaiming = false;
    226.                     player.GetComponent<player>().sensMultiplier = 1f;
    227.                 }
    228.                 if (isaiming)
    229.                     inHand.transform.position = aimpos.transform.position;
    230.  
    231.  
    232.                 if (Input.GetButtonDown("Fire1") && firetimer < 1)
    233.                 {
    234.                     ps.Play();
    235.  
    236.                     Debug.Log("FIRE");
    237.                     gameObject.GetComponent<Camera>().fieldOfView = ZOOM + 10;
    238.                     inHand.transform.position = recoilpos.transform.position;
    239.                     inHand.transform.rotation = recoilpos.transform.rotation;
    240.                     firetimer = 2f;
    241.                     transform.localRotation = Quaternion.Euler(transform.localRotation.x - 0.1f, transform.localRotation.y, transform.localRotation.z);
    242.                     if (Physics.Raycast(aimpos.transform.position, transform.forward, out bullet, 9999999f))
    243.                     {
    244.                         if (bullet.collider.gameObject.layer == player1)
    245.                         {
    246.                             bullet.collider.gameObject.GetComponent<player>().Hp -= awpDamage;
    247.                         }
    248.                         else
    249.                         {
    250.  
    251.                             Instantiate(spark, bullet.point, Quaternion.Euler(Vector3.zero));
    252.                         }
    253.                     }
    254.                 }
    255.  
    256.             }
    257.  
    258.         }
    259.     }
    260.     void Reload()
    261.     {
    262.         Debug.Log("Reloading...");
    263.         if (inHand.tag == "pistol")
    264.         {
    265.             if (currentpistolMags != 0)
    266.             {
    267.                 currentpistolMags -= 1;
    268.  
    269.                 currentpistolAmmo = pistolAmmo;
    270.                 inHand.GetComponent<Animation>().Play("spinnyboi");
    271.             }
    272.                
    273.         }
    274.     }
    275. }
    276.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Looks like there is hardly any network code here. When you pick up an item, you're doing everything locally, so only the client which picks it up sees any change. If these are networked GameObjects you need to have the client tell the host/server you're picking up the item. Then the host can tell all the clients.

    Understand when you make changes to things like Rigidbody properties, none of that propagates. If you need those changes on more than just this 1 client, then you need to send a Command to the server, have the server do the same, and then have the server send a ClientRPC to tell all the clients to do the same. You need to think of your game as entirely separate game instances running on all these different computers, which sync up only when you specifically tell the others to sync up.
     
  3. theCyberStallion

    theCyberStallion

    Joined:
    Oct 3, 2020
    Posts:
    29
    i mean it wont even show up for the client. i actually cant pick anything up at all, interact with buttons or anything, as the raycasts never hit and i can see this because on line 47 theres a debug.log it should tell me when it hits.
    also by "sync positions" i mean when i run over them with my player, nothing changes even though it has a network transform set to transform sync mode.
    3rd, what is a client rpc?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    It looks to me like 3.5 units you're using is very short for a raycast from the camera. You need to verify 3 things from what I can see. First that the camera is actually within 3.5 units of the object you are raycasting at. Verify that the object actually does have a collider and is on the correct layer. And verify that the "weapon" layermask has been set correctly.

    I don't see any running code here, but the way Unet works is a networked GameObject can either have client authority or host authority. If it is set for client authority then you do your movement of the object on the client with authority, and the network transform sends that movement to the host, which then sends that movement to all other clients.

    If the object is using host authority, and you want the client to control it, you need the client to tell the host to do some movement. The host then actually does the movement locally, and through the network transform the movement is then sent to all clients (including the client that initially send the message to the host to do some movement).

    What is really important though, is you can't mix the two. If the object has host authority, and you move it on the client, the movement is not propagated by the network transform, because the client doesn't have authority to make that change.
    It is how the host tells clients to run a specific function. The inverse of that is the Command where the client tells the host to run a specific function. But they are essentially the same concept. Otherwise all functions called in your code are only run on that specific computer, and nothing that happens within them is shared to any other computer running the game.

    SyncVars are another thing to be aware of. They are variables that are automatically synced from the host to all clients. They do not sync the other way. Generally you only set them on the host with just a few exceptions.
     
  5. theCyberStallion

    theCyberStallion

    Joined:
    Oct 3, 2020
    Posts:
    29
    i fixed the raycast to the weapon, it turns out i needed to put my raycasting and pickup script in a public void then call it from the main player script. i cant get the positions right though, how do you send "commands" to a host (or server)
    EDIT: figured it out, and found this wonderful thing called mirror. i switched.
     
    Last edited: Apr 3, 2021
    Joe-Censored likes this.