Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by joker71 · Apr 23, 2016 at 04:45 PM · weaponchangingswitch objects

Switching Weapons and RELOAD animation

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image joker71 · Apr 24, 2016 at 05:40 PM 0
Share

Can somebody help? Or at least suggest a weapon switch script?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MerpySoup · Apr 23, 2021 at 08:31 PM

5 years late but for anyone who finds this post, make sure your animations all have keyframes at the start and end for everything that could potentially change in another animation. If your reload moves the left arm, but your equip anim only rotates the gun, then the left arm will stay in the pose it was in when you play the equip animation.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

56 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Pickup weapons in 2D scroller game 1 Answer

Many weapons to a Unit 0 Answers

Won't fire when switching weapons? 0 Answers

Weapon Select Menu 0 Answers

Weapon Switching in Mobile Game 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges