Gun Animation Problems in Unity 3.5.2

Started by
0 comments, last by EricMarsh 11 years, 1 month ago

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();
        }
    }

Advertisement

Fixed the problem.

Turns out I was using an older version of the .FBX.

Lesson learned!

This topic is closed to new replies.

Advertisement