Multiplayer FPS weapon changing

Updated on June 4, 2018 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
6 on June 2, 2018

hey

I am following tutorials Multiplayer FPS and i want to change weapon

I want to add some weapon such as AWP and etc

its been weeks and still i dont know what to do

It will be a very big help

thanks

 

using UnityEngine;
using UnityEngine.Networking;

public class WeaponManager : NetworkBehaviour
{
    [SerializeField]
    private string WeaponLayerName = “Weapon”;
    [SerializeField]
    private Transform weaponHolder;

   [SerializeField]
   private M4 primeryWeapon;
    [SerializeField]
    private AWP snipe;
    private M4 currentWeapon;
    private WeaponGraphics currentGraphics;
    void Start()
    {
        EquipWeapon(primeryWeapon);
    }
    public M4 GetCurrentWeapon()
    {

        return currentWeapon;
    }
    public WeaponGraphics GetCurrentGraphics()
    {

        return currentGraphics;
    }

    void EquipWeapon(M4 _weapon)
    {
        currentWeapon = _weapon;

        GameObject _weaponIns = (GameObject)Instantiate(currentWeapon.graphics, weaponHolder.position, weaponHolder.rotation);
        _weaponIns.transform.SetParent(weaponHolder);

        currentGraphics = _weaponIns.GetComponent<WeaponGraphics>();
        if (currentGraphics == null)

            Debug.Log(“No Weapon Graphics component on The Weapon” + _weaponIns.name);

        if (isLocalPlayer)
            Util.SetLayerRecursively(_weaponIns, LayerMask.NameToLayer(WeaponLayerName));
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            EquipWeapon(primeryWeapon);
        }

        if (Input.GetKeyDown(KeyCode.Alpha2))
        {

        }

    }

}

  • Liked by
Reply
0 on June 2, 2018

All of your weapon have their own class (M4, AWP, etc.

So, of course, none of the code you wrote to work with M4 will work for the AWP.

Like your currentWeapon variable can never be anything but an M4 with the way you have it set up. Likewise for the primaryWeapon, and snipe can only ever be an AWP.

 

You need to have 1 single class called Gun, or Weapon, or whatever, that either defines all of your weapons, or that all your subsequent weapon classes inherit from. Then you can use that class for all of your switching and other arbitrary (non weapon specific) tasks.

  • Liked by
Reply
Cancel
3 on June 2, 2018

I was following the tutorials but its not complete

of course he would change it if continued

anyway i have some scripts for m4 awp such as these with different values

using UnityEngine;
[System.Serializable]
public class M4
{
    public string name = “M4”;
    public int Damage = 20;
    public float Range = 100f;

    public float firerate = 5f;
    
    public GameObject graphics;
}

can i use them in that scripts or i must do some thing else

Iam not pro

I would be so thankfull if change some of my scripts and tell me what to do

Wise
on June 2, 2018

You don’t need to change much is this is what your M4 script is.

Basically you want all of your weapons to be defined as a unified class, like Gun.

All of your guns would be defined as a Gun, just swap the values. So M4, would stay like it is, AWP would get a new name, higher damage and range, but lower firerate.

 

Then, since all of your guns are just defined as Gun. In your original WeaponManager script, instead of taking M4 classes as arguments, it would take Gun. Since all of your Weapons are defined under a single class, it’s no problem to just swap out references for things like CurrentWeapon, and SecondaryWeapon.

 

on June 4, 2018

First of all I really appreciate for your good helps,

I made the class gun and defined the guns and want to ask, if I want to use specific particle effects for every weapon can I use the class for that ? or it just take the values?

and if its possible guide me a little about how to change my weapon manager script , even a hint

tnx dude

Wise
on June 4, 2018

Happy to help.

 

The particle effects could just be saved on the gun’s prefab. So you could still customize each one and save it to the gun. The script would just tell the particle system to play. It doesn’t care abut the details of the system.

 

To adjust your manager script, just replace everywhere you have M4, or AWP with Gun (or what ever you named the general weapon class) Since all guns are now equal, it doesn’t matter what the actual gun is. You just basically save a reference to the gameObject through the gun class, as well as access values like damage and range.

Show more replies
  • Liked by
Reply
Cancel
0 on June 4, 2018

the weapon manager script is above

  • Liked by
Reply
Cancel