- Home /
Animator state not activating immediately.(URGENT)
Hi guys,
I'm trying to get weapon switching to work for my weapons that use animators.
Basically, what should happen is when the player presses 1 the first weapon in the array should be activated and the animator should go from 'any state' to 'equip' so that it looks like the weapon is being equipped. However, activating this weapon for a split second you can see that the weapon is in (if we are standing still) 'idle', before then going to play the'entry' animation.
Anyone know what's going on here?
Here's relevent parts of the script. var availableWeapons : GameObject[]; //an array of all available weapons var animator : Animator; //stores the animator component
function Start () {
animator = GameObject.Find("AUG").GetComponent(Animator);//assigns Animator component when we start the game
//GetComponentInChildren(Animator); //assigns Animator component when we start the game
var currentWeaponObject = availableWeapons[0];//NEW 06/07/2015
//currentWeaponObject.transform.parent = weaponCam.transform; //set the weapon to be a child of the weapon camera
currentWeapon = currentWeaponObject.GetComponent(Weapon_Base); //set the "currentWeapon" to this weapon
}
function Update (){
if (Input.GetKeyDown("1"))
{
SelectWeapon(0); //selects the first weapon when '1' is pressed
animator = GameObject.Find("AUG").GetComponent(Animator);
animator.SetTrigger("Switch");
var currentWeaponObject = availableWeapons[0];
animator.SetTrigger("Switch");
currentWeapon = currentWeaponObject.GetComponent(Weapon_Base);
fireRate = 0.15;
gui_ClipCount.text = ""+currentWeapon.clipAmmo;
gui_ReserveCount.text = ""+currentWeapon.reserveAmmo;
}
if (Input.GetKeyDown("2"))
{
SelectWeapon(1); //selects the second weapon when '2' is pressed
animator = GameObject.Find("M9").GetComponent(Animator);
animator.SetTrigger("Switch");
currentWeaponObject = availableWeapons[1];//sets the current weapon object to the second weapon assigned in the array (through the inspector)
currentWeapon = currentWeaponObject.GetComponent(Weapon_Base);//this is for the weapon base script which holds how many bullets we have
fireRate = 0.5;
gui_ClipCount.text = ""+currentWeapon.clipAmmo;
gui_ReserveCount.text = ""+currentWeapon.reserveAmmo;
}
}
function SelectWeapon(index : int) //FUNCTION FOR SWAPING WEAPONS
{
for(var obj:GameObject in availableWeapons)obj.SetActive(false); //these lines just cycle through the weapons in our array
availableWeapons[index].SetActive(true); //selects one to be active, and the rest to be inactive
}
Your answer
Follow this Question
Related Questions
Referencing an Animator component from a parent object? 1 Answer
Animation driven NavMeshAgent with Speed, AngularSpeed and Direction as parameters. 1 Answer
What can and can't I animate in mecanim? 0 Answers
Proper handling of animation events when setting normalised time 0 Answers
Make a third person controller advanced using animation 0 Answers