I am working on an FPS game and everthing is working just fine but whenever i want to switch between the two Weapons, the previous weapon (or the hidden weapon, you name it) is basiclly not working anymore no matter what i do.
For example: when i press the “2” key and the rifle is equipped and it’s reload animation is playing it switches to my sidearm but when I switch back to the rifle it simply stuck and wont shoot or reload any more. I hope that it was clear.
In the Hierarchy panel it is set like this:
FPSController
FirstPersonCharacter (Which is the camera)
EmptyGameObject with the weapon script (Parented to the FirstPersonCharacter )
WeaponModel With animations (this is a child of the EmptyGameObject)
This the Weapon Script:
var ReloadSound : AudioSource; //reload audio source
var DrySound : AudioSource; //empty magazine audio source
var FireModeSound : AudioSource; //fire mode switching audio source
var Shell : GameObject; // bullet shelll 3d object
var ShellSpawn : Transform; // where the bullet shelll is going to be instantiated from
public var ShootAnim : Animation; // the shooting animation
public var ReloadAnim : Animation; // the reload animation (magazine is not empty)
var ReloadEmptyAnim :Animation; // the reload animation (magazine is empty)
public var IdleAnim : Animation; // the idle animation
public var canShoot : boolean = true; // can the player shoot?
public var canReload : boolean = false; // can the player reload?
public var canAim : boolean = true; // can the player aim down sights?
var clip = 30; // number of bullets in the magazine
var ammoLeft = 400; // maximum ammo you can carry
var TimeBetweenShots : float = 0.08; // controlls the fire rate
var timestamp : float;
var Effect : Transform; // the effect that will be displayed at the bullet's hit position (like: bullet holes, blood, etc..)
var TheDammage = 10; // the damage of a single bullet hit
var AimSound : AudioSource; //aim audio source
var maxBulletSpreadAngle : float = 1.5f; // the bullet spread angle
public var IsAiming : boolean = false; // is the player aiming?
var MagazineDisplay : UnityEngine.UI.Text; // mag HUD
var AmmoLeftDisplay : UnityEngine.UI.Text; // Ammo left HUD
//Muzzle Flash Variables
var MuzzleFlash : Renderer; // muzzle flash texture
var MuzzleLight : Light; // point light
var SmokePuff : GameObject; // smoke particle
var SmokeSpawn : Transform; // position of the smoke particle
public var FireSound : AudioClip; // audio file of a single shot
var SoundSpawn: AudioSource; // audio source for the FireSound file
var FullAuto : boolean = true; // boolean checking for full auto or semi auto
var IsReloading : boolean = false; // is the player reloading?
var finishReload : boolean = false; // player finished reloading?
function Start () {
MuzzleFlash.enabled = false; // disable the muzzle flash texture on start
MuzzleLight.enabled = false; // disable the point light on start
SoundSpawn = GetComponent.<AudioSource>();
}
function Update () {
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*2, Screen.height*2, 0));
var fireDirection : Vector3 = transform.forward;
var fireRotation : Quaternion = Quaternion.LookRotation(fireDirection);
var randomRotation : Quaternion = Random.rotation;
fireRotation = Quaternion.RotateTowards(fireRotation,randomRotation,Random.Range(0.0f,maxBulletSpreadAngle));
if(Input.GetButton("Fire1") && clip > 0 && Time.time >= timestamp && canShoot == true && FullAuto == true){
SoundSpawn.PlayOneShot(FireSound, 0.7F);
clip -= 1;
Fire();
if (Physics.Raycast (transform.position,fireRotation*Vector3.forward, hit, 100))
{
Debug.DrawLine (transform.position, hit.point, Color.green);
var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(particleClone.gameObject, 2);
hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
}
}
if(Input.GetButtonDown("Reload") && clip != 0 && ammoLeft != 0 && canReload == true && IsReloading == false ){
canAim = false;
canShoot = false;
Reload();
}
if(clip == 0 && ammoLeft > 0 && IsReloading == false){
ReloadSound.Play();
ReloadEmptyAnim.Play("reload_empty");
canAim = false;
canShoot = false;
AutoReload();
}
if(clip != 30 && ammoLeft != 0){
canReload = true;
}
if(Input.GetButton("Fire1") && clip == 0 && ammoLeft == 0 && !ReloadAnim.IsPlaying("reload")){
DryGun();
}
if(Input.GetButtonDown("FireMode") && FullAuto == true){
FullAuto = false;
FireModeSound.Play();
}else if(Input.GetButtonDown("FireMode") && FullAuto == false){
FullAuto = true;
FireModeSound.Play();
}
if(Input.GetButtonDown("Fire1") && clip > 0 && Time.time >= timestamp && canShoot == true && FullAuto == false){
SoundSpawn.PlayOneShot(FireSound, 0.7F);
clip -= 1;
Fire();
if (Physics.Raycast (transform.position,fireRotation*Vector3.forward, hit, 100))
{
Debug.DrawLine (transform.position, hit.point, Color.green);
particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(particleClone.gameObject, 2);
hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
}
}
MagazineDisplay.text = "" + clip;{
AmmoLeftDisplay.text = " /" + ammoLeft;
}
if (hit.collider.tag == "EnemyHead");{
TheDammage = TheDammage*2;
}
}
function Fire() {
timestamp = Time.time + TimeBetweenShots;
ShootAnim.Play("shoot");
if(ShootAnim.IsPlaying("shoot"))
ShootAnim.Rewind("shoot");
}
function Reload() {
IsReloading = true;
canAim = false;
canReload = true;
ReloadAnim.Play("reload_full");
ReloadSound.Play();
yield WaitForSeconds(3.7);
var totalAmmo = clip + ammoLeft;
if(totalAmmo <= 30){
clip = totalAmmo;
ammoLeft = 0;
}else{
var shotsFired = 30 - clip;
clip = 30;
ammoLeft -= shotsFired;
}
canShoot = true;
canReload = false;
canAim = true;
IsReloading = false;
finishReload = true;
yield WaitForSeconds(0.1);
finishReload = false;
}
function AutoReload() {
yield WaitForSeconds(0.3);
IsReloading = true;
canAim = false;
canReload = false;
ReloadAnim.Play("reload");
ReloadSound.Play();
yield WaitForSeconds(2.9);
totalAmmo = clip + ammoLeft;
if(totalAmmo <= 30){
clip = totalAmmo;
ammoLeft = 0;
}else{
shotsFired = 30 - clip;
clip = 30;
ammoLeft -= shotsFired;
}
canReload = false;
canShoot = true;
canAim = true;
IsReloading = false;
finishReload = true;
yield WaitForSeconds(0.1);
finishReload = false;
}
function DryGun(){
DrySound.Play();
}
This is the switch script: (this script is attached to the camera)
var primary : GameObject; //the first weapon gameobject
var secondary : GameObject; // the second weapon gameobject
var knife : GameObject; // knife game object
var reloadAnim : Animation;
var KnifeAttackAnim : Animation;
var QuickStab : boolean = true;
function Start () {
Cursor.LockState = true;
primary.SetActive(true);
secondary.SetActive(false);
knife.SetActive(false);
}
function Update () {
if(Input.GetButtonDown("Primary")){ // The Button Primary In the parenthesis is set to number "1" on the keyboard
primary.SetActive(true);
secondary.SetActive(false);
knife.SetActive(false);
}
if(Input.GetButtonDown("Secondary")){ // The Button Secondary In the parenthesis is set to number "2" on the keyboard
primary.SetActive(false);
secondary.SetActive(true);
knife.SetActive(false);
}
if(Input.GetButtonDown("Knife")){ // The Button "Knife" In the parenthesis is set to number "1" on the keyboard
primary.SetActive(false);
secondary.SetActive(false);
knife.SetActive(true);
}
if(Input.GetButtonDown("QuickKnife") && primary.active == true && QuickStab == true ){
QuickKnifePrimary();
}
if(Input.GetButtonDown("QuickKnife") && secondary.active == true && QuickStab == true ){
QuickKnifeSecondary();
}
}
function QuickKnifePrimary () {
primary.SetActive(false);
secondary.SetActive(false);
knife.SetActive(true);
KnifeAttackAnim.Play("Attack");
yield WaitForSeconds (0.5);
primary.SetActive(true);
secondary.SetActive(false);
knife.SetActive(false);
}
function QuickKnifeSecondary () {
primary.SetActive(false);
secondary.SetActive(false);
knife.SetActive(true);
KnifeAttackAnim.Play("Attack");
yield WaitForSeconds (0.5);
primary.SetActive(false);
secondary.SetActive(true);
knife.SetActive(false);
}
If it wasn’t clear let me know, I will explain it better but i really need some help with this issue. It’s been a long time since I have encountered this problem and still no solution.