Random Animation

Hi,

I have an enemy character that when he gets close to the player he attacks.

What Im trying to do is get the enemy to pick one of 3 attack animations (randomly) to use and use that one animation to attack while in close and attacking.

Then once out of the close attack distance he will run back at the player (chase again) and be in the close attack distance again and choose yet another random attack animation to play while attacking.

I’ve been searching the forums without much luck and came up with this but what this is doing is playing all of the animations randomly at the same time instead of just playing the one that was selected.

How would I structure this to work the way I want?

Solved Below :)

I tried this which looks much better but is still doing somewhat the same thing.

How do I randomly pick one of the 3 attack animations and only play that one when the enemy is close(closeAttack)?

var attacks = ["attack1", "attack2", "attack3"];

function PlayRandom()
{
	var randomAttack = attacks[Random.Range(0,attacks.length)];//attacks.length
	if(!animation.IsPlaying(randomAttack))
	{
		animation.CrossFade("" + randomAttack);
	}
	Debug.Log(randomAttack);		
}

Im doing my best to try and figure it out, hopefully someone smarter than me can suggest something?

Not at home right now but i’d try only calling the function at the end of a specified time frame which would be the length of the animations.

I can’t do much from my phone here but try using timing to make sure an animation doesn’t play immediately after one executes. That’s all I have right now /shrug

Ok finally problem solved :slight_smile:

var arrayAnimations = ["attack1", "attack2", "attack3"];

function GetRandomAnimation() 
{
	var arrayPosition = Random.Range(0, arrayAnimations.Length); //get a random number for the position in the array.
	return arrayAnimations[arrayPosition];
        
}

and when I need it I just call

animation.CrossFade(GetRandomAnimation());

Sweeeeeeet!!!

Hi,

I tried to do this in C# but I can’t find a variable that can hold “animation” values. Anyone knows how to call random animations in C#?

thanks.