Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Can someone help with this script? JS

Discussion in 'Getting Started' started by zippydpt, Jan 21, 2015.

  1. zippydpt

    zippydpt

    Joined:
    Mar 20, 2014
    Posts:
    3
    Hello! I am working on a game based off of the BootCamp Unity Demo and I need some help. How do I add more weapons to the gun manager script? I would like this to happen:

    If "3" is pressed, change model to "ModelNameHere" change fire rate to semi automatic with a 1 second delay, damage of the gun is set to high. Can there also be an animation played when keys are pressed such as right_mouse shows the character raising the gun, or if shift is pressed the character starts to sprint. w,a,s,d cause character to play walking animation, etc.

    the code:

    #pragma strict
    #pragma implicit
    #pragma downcast

    class GunManager extends MonoBehaviour
    {
    public var Local : boolean;
    public var guns : GunKeyBinder[];

    @HideInInspector
    public var currentGun : Gun;
    public var oldi : int;
    @HideInInspector
    public var currentWeapon : byte;
    public var oldcurrentWeapon : int;

    public var soldier : SoldierController;

    public var hud : HudWeapons;

    function Start()
    {
    for(var i : int = 0; i < guns.length; i++)
    {
    guns.gun.enabled = false;
    }
    currentWeapon = 0;
    guns[0].gun.enabled = true;
    currentGun = guns[0].gun;
    }

    function Update()
    {
    if(Local)
    {
    for(var i : int = 0; i < guns.length; i++)
    {
    if(Input.GetKeyDown(guns.keyToActivate))
    {
    ChangeToGun(i);

    }
    }

    hud.selectedWeapon = currentWeapon;
    hud.ammoRemaining[currentWeapon] = guns[currentWeapon].gun.currentRounds;

    if(oldcurrentWeapon != currentWeapon)
    {
    transform.parent.SendMessage("SetGunInfoRPC",currentWeapon, SendMessageOptions.DontRequireReceiver);
    oldcurrentWeapon = currentWeapon;
    }
    }
    }

    function FireSimulate(_hashtable : Hashtable)
    {

    var weapon : byte = _hashtable["Weapon"];

    var cGun : Gun = guns[weapon].gun;
    for(var j : int = 0; j < guns.length; j++)
    {
    guns[j].gun.enabled = false;
    }
    cGun.enabled = true;
    currentWeapon = weapon;
    cGun.Local = Local;
    cGun.reloading = _hashtable["reload"];
    if (_hashtable["OutPutPoint"] != null)
    cGun.OutputPoint = _hashtable["OutPutPoint"];

    cGun.Point = _hashtable["Point"];
    cGun.fireRemote = _hashtable["Fire"];
    cGun.fire = _hashtable["Fire"];
    }

    function ChangeToGun(gunIndex : int)
    {
    var cGun : Gun = guns[gunIndex].gun;

    if(cGun.enabled)
    {
    if(guns[gunIndex].switchModesOnKey)
    {
    switch(cGun.fireMode)
    {
    case FireMode.SEMI_AUTO:
    cGun.fireMode = FireMode.FULL_AUTO;
    break;
    case FireMode.FULL_AUTO:
    cGun.fireMode = FireMode.BURST;
    break;
    case FireMode.BURST:
    cGun.fireMode = FireMode.SEMI_AUTO;
    break;
    }
    }
    }
    else
    {
    for(var j : int = 0; j < guns.length; j++)
    {
    guns[j].gun.enabled = false;
    }

    cGun.enabled = true;
    currentGun = cGun;
    currentWeapon = gunIndex;
    }
    }

    }
     
    Last edited: Jan 21, 2015
  2. dterbeest

    dterbeest

    Joined:
    Mar 23, 2012
    Posts:
    389
  3. zippydpt

    zippydpt

    Joined:
    Mar 20, 2014
    Posts:
    3
    I don't understand. Please explain.
     
  4. zippydpt

    zippydpt

    Joined:
    Mar 20, 2014
    Posts:
    3
    Please can you help me?