- Home /
How to do Switching Weapon System
Well i'm making an FPS and blah blah...I need more than 1 gun, so, what's the easiest way to do a weapon switching system (C# or JavaScript)? I have my Player Like this: Player > Main Camera > PrimaryWep > M4; Bullet-FirePoint; And my Gun script(Attatched to PrimaryWep, M4 is just the 3D model) works like this:
#pragma strict
//Weapon Script
var bullet : GameObject;
var gunBarrel : Transform;
var reloadSound : AudioClip;
var shootSound : AudioClip;
var emptySound : AudioClip;
var hudAmmo : GUIText;
var hudMags : GUIText;
var ammo : int;
static var mags : int;
var fireRate : boolean;
var shootTime : float;
var aimingDownSights : boolean = false;
function Start () {
ammo = 45;
mags = 225;
fireRate = true;
shootTime = 0;
}
function Update () {
hudAmmo.text = "Loaded:" +ammo;
hudMags.text = "Stored:" +mags;
if(Input.GetButton("Fire1")){
if(ammo > 0){
if(shootTime > 0.1) {
ammo--;
audio.clip = shootSound;
audio.Play();
Instantiate(bullet,gunBarrel.position,gunBarrel.rotation);
shootTime = 0;
if(aimingDownSights){
animation.Play("ADSAnim3");
} else {
animation.Play("FireAnim1");
}
}
}
}
if(ammo == 0 && !audio.isPlaying){
audio.clip = emptySound;
audio.Play();
}
shootTime += Time.deltaTime;
if(Input.GetKeyDown(KeyCode.R)){
if(ammo == 0 && mags > 0) {
audio.clip = reloadSound;
audio.Play();
animation.Play("ReloadAnim");
ammo = ammo + 45;
mags = mags -45;
}
}
if(Input.GetButtonDown("Fire2")){
aimingDownSights = true;
animation.Play("ADSAnim1");
}
if(Input.GetButtonUp("Fire2")){
aimingDownSights = false;
animation.Play("ADSAnim2");
}
}
Well i'm thinking about crating another script like this, but changing the ammo and etc...Also i'll make another Bullet-FirePoint(Position where the bullet is instantiated) and etc...
So what's the easiest way to do this? I was thinking about something like: Inside Main Camera I'll create another empty called SecondaryWep, then i'll attatch the Gun2 script, and put inside another gun model, and the other Bullet-FirePoint...then use "activeSelf" to enable and/or disable the guns when i press 1 or 2...Can someone help me with this script? Also if you know a better way to do this please tell me (and please write the script, if you can)...
Answer by Mmmpies · Dec 11, 2014 at 09:34 PM
Programming is programming so either language is fine. C# is generally faster than JS but JS is still fine. I see you use java yourself, which I'm not familiar with but here's a java tutorial on YouTube.
Your answer
Follow this Question
Related Questions
unlocking weapon (fps) 1 Answer
Multiple Weapon Animation Help 0 Answers
FPS Camera+weapon setup? 0 Answers