Sign in to follow this  
EricMarsh

Unity Gun Animation Problems in Unity 3.5.2

Recommended Posts

EricMarsh    110

Programs: Unity 3.5.2, 3dsMax 2012
Problem: Two of five animations are not playing appropriately.

Language: Javascript

I have a gun with an animation imported via .FBX from 3dsMax 2012. The animation from 3dsMax runs from frames 0-190 and contains the following animations that I've separated inside of Unity 3.5.2:

Frames 0-9 LMBShoot
Frames 9-18 RMBShoot
Frames 20-50 Reload
Frames 50-62 LMBBurst
Frames 62-190 idle

My problem arises in executing the LMBBurst animation and having the idle play constantly while no other animations are playing; the other three animations (LMBShoot, RMBShoot, and Reload) all work when they're called upon in the script, however.

I have a separate script for RMBShoot, which works fine considering it has no alternative fire option.

Both of these scripts are on an Empty Game Object named "BarrelNode", which is a child of the gun root that contains all of the animations, allowing me to freely relocate the origin of the particle effects. "Gun" and "AmmoCapsule" are separate objects used in the animations to simulate wrist rotation and ammo container rotation and repositioning animations.

Here's an example of the heirarchy:
First Person Controller

Graphics
Main Camera
Weapon
FullGun
BarrelNode
Gun
AmmoCapsule


And the code, which you may use for your own devices if you wish; thanks for the help:

    var ammoMax = 21; //Maximum ammo per magazine
    var damage = 5.0; //Damage caused per projectile
    var ammoCarried = 105; //Amount of ammo carried, allows for pickups to replenish ammunition
    var ammoCount = ammoMax; //ammoCount = current ammunition in magazine
    var burstFireAmount = 3; //Ensures 3 shots per burst
    var burstFireDelay = .1; //Delay between individual shots in burst
    private var ammoRemainder = 0; //Hidden comparison variable to clean up code
    private var bBurstFire = false; //Burst Fire Toggle Variable
    private var isFiring = false; //Is Firing Boolean Variable to restrict overlapping fire, simulate real gun
    public var dryFireAudio : AudioClip; //play audio of a click instead of normal gunshot
     
    function Start()
    {
        //Don't know if this is necessary or not
        audio.clip = dryFireAudio;
        //This isn't working, apparently.
        transform.parent.animation.CrossFade("idle", .1);
    }
     
    function Update()
    {
        //Check if isFiring is true, if not, proceed, if it is, do nothing
        if(isFiring != true)
        {
            //Input Manager has "Fire1" set to Left Ctrl and Mouse0, left mouse click
            if(Input.GetButtonDown("Fire1"))
            {
                //Check if burst fire is active, if not, proceed to fire normally
                if(bBurstFire!=true)
                {
                    isFiring = true;
                    NormalFire();
                    isFiring = false;
                }
                //If burst fire is active, proceed to burst fire
                else
                {
                    BurstFire();
                }
            }
            //Reload function call
            if(Input.GetButtonDown("Reload"))
            {
                //This works, plays the reload animation, yield WaitForSeconds() in Reload() is hard-coded for reload animation duration atm
                transform.parent.animation.CrossFade("Reload", .1, PlayMode.StopAll);
                Reload();
            }
            //Button press for BurstFire/SingleShot toggle
            if(Input.GetButtonDown("ChangeFireType"))
            {
                if(bBurstFire == false)
                {
                    bBurstFire = true;
                }
                else
                {
                    bBurstFire = false;
                }
            }
        }
        //Attempt at forcing idle to play, doesn't work. In theory, this should check every frame whether an animation is playing and, if there is none, start the idle sequence
        else
        {
            if(!animation.IsPlaying)
                transform.parent.animation.Play("idle", PlayMode.StopAll);
        }
    }
     
    function Reload()
    {
        //Basic arithmetic to check for remaining ammo supply and refill current supply with carried supply, checks for running out of ammo
        isFiring = true;
        ammoRemainder = ammoCarried - (ammoMax - ammoCount);
        if(ammoRemainder >= 0)
        {
            ammoCarried -= (ammoMax - ammoCount);
            ammoCount = ammoMax;
            yield WaitForSeconds(reloadTime);
        }
        else if(ammoCarried > 0)
        {
            ammoCount += ammoCarried;
            ammoCarried = 0;
            yield WaitForSeconds(reloadTime);
        }
        else
        {
        }
        isFiring = false;
    }
     
    //burstFireAmount Shot Function
    function BurstFire()
    {
        isFiring = true;
        //This also isn't working, apparently.
        transform.parent.animation.Play("LMBBurst", PlayMode.StopAll);
        if(ammoCount>0)
        {
            for(var i = 0; i < burstFireAmount; i++)
            {
                NormalFire();
                yield WaitForSeconds(burstFireDelay);
            }
        }
        else
        {
            audio.Play();
        }
        isFiring = false;
    }
     
    //Single Shot Function
    function NormalFire()
    {
        if(ammoCount > 0)
        {
            if(bBurstFire != true)
            transform.parent.animation.CrossFade("LMBShoot", .1, PlayMode.StopAll);
           
            var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
       
            instantiatedProjectile.velocity = transform.TransformDirection ( Vector3 ( 0, 0, speed));
           
            Physics.IgnoreCollision (instantiatedProjectile.collider, transform.root.collider);
           
            ammoCount--;
        }
        else
        {
            if(bBurstFire != true)
            audio.Play();
        }
    }

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By dobbey
      Who are you?
      We call ourselves Dobbey. Our team currently consists of two people.
      We are both early 20's and come from Germany. At the moment this is our hobby.
      What's the name of the game and what is it about?
      The game is called Insane and it is a 2D adventure puzzle game with horror elements.
      Insane is about a scientist who has been doing human experiments for years.
      His most successful experiment is number 73, which seems to be mentally labile.
      This experiment manages to flee one day but can't distinguish between reality and imagination.
      In this game you will find a creepy atmosphere with difficult and varied puzzles.
      What inspired you?
      The style of the game is inspired by Limbo. But we were also inspired by Jump 'n' Run games and movies.
      Of course, we have also our own elements like an inventory system, butterfly effect, the "INSANE" effect, using a QR-Code..
      Which program do you use to develop the game?
      We are programming it with Unity. In our opinion, it is the best program for indie games.
      And of course we are working with Photoshop to create the graphics.
      What platform will Insane be available on?
      For PC.
      How can I support you?
      Twitter:        http://bit.ly/20POkfM    
      YouTube:     http://bit.ly/2rLXoM0
      Instagram:   http://bit.ly/2qwU2bl
      Facebook:    http://bit.ly/2p3nwRQ
      Website:       http://bit.ly/2s8buqU
       

      Here you can see some of our screenshots.

       

      (You can find the animation on Twitter/Instagram.)
       

       

       

      (You can find the animation on YouTube.)

      What do you think about it?
       
      Yo can see more screenshots or videos on Twitter, Instagram or YouTube.
    • By Liquid1Phantom
      Hello, my name is Thomas and I am currently starting up a project. 
      The project is going to be a very hardcore, and tactical FPS that is similar to Escape from Tarkov. It will be made within Unity, and will be programmed in C#. Currently, I have a Unity project started, an organized Discord setup, and as well as a very organized Google Drive. The Google Drive has art references, and plenty of folders for organization. Lastly, the payment for this project will be solely rev-share as I have no money.
      I am looking for anyone who can contribute: 2D artists, 3D artists, composers, programmers, etc.
      If you would like to help out and be of use, please email me at: thomasmunson2277@gmail.com
      Alternatively, add my Discord: Thomas#3788
    • By ilovegames
      Score points by shooting enemies! Before you is one of the best representatives of the simulator sniper games genre, with stunning graphics, quality effects, and realistic ballistics of bullets. It will not be easy to win for sure. Are you ready? Then take a sniper rifle and fight!
      Download http://falcoware.com/Sniper.php




  • Popular Now