still need help with animation array
the script is such that when the enemy walks into a trigger zone, it disables his attack script ( that works) and then is SUPPOSE to play a random assortment of animations from my array. As it is....his attack script does disable and he will randomly play ONE of the animations in my array then STOP. I want him to play a random animation, and when it is finished play another from the array. Anybody help me on this? I have sound array scripts that work and I can't see why this doesn't. I have also tried it with OnTriggerStay but what that does is pick a random animation but play the same animation over and over
using UnityEngine;
using System.Collections;
public class AnimationArray : MonoBehaviour {
private MonoBehaviour Attack;
public AnimationClip[] clips;
void Start () {
Attack = GetComponent<EnemyBehaviourDestruct>();
animation.clip = clips[Random.Range(0, clips.Length)];
}
void OnTriggerEnter(Collider other) {
if(other.tag == "worker1")
{
Attack.enabled = false;
//animation.clip = clips[Random.Range(0, clips.Length)];
animation.Play();
}
}
}
Answer by Alseer7 · Aug 15, 2021 at 12:26 PM
You have animation.clip = clips[Random.Range(0, clips.Length)];
in your Start method, which is only going to run once, hence the single play animation. I would add another If statement inside of your first if method:
using UnityEngine;
using System.Collections;
public class AnimationArray : MonoBehaviour {
private MonoBehaviour Attack;
public AnimationClip[] clips;
void Start () {
Attack = GetComponent<EnemyBehaviourDestruct>();
//animation.clip = clips[Random.Range(0, clips.Length)];
}
void OnTriggerStay(Collider other) {
if(other.tag == "worker1")
{
Attack.enabled = false;
if(!animation.isPlaying)
{
animation.clip = clips[Random.Range(0, clips.Length)];
animation.Play();
}
}
}
}
This will (hopefully) make it so that it checks if you are presently playing an animation, if you are, it does nothing and runs the OneTriggerStay method again until it finally can play another animation, and it runs it back through the animation randomizer and then plays the newly selected animation. I hope this helps you out, man!
Answer by junk_rat_angel · Aug 14, 2021 at 04:28 AM
I think you probably want to call Play as many times as the number of clips you play and change the clip it plays each time, so in onTriggerEnter you probably want to set a state which you can reset once all the clips are over and as long as that state is active i.e. clipCity = true,1, etc. (ill leave you to decide how you wanna handle that) you can call a function to handle their queuing with something like: if (anim.isPlaying) { return; } else { animation.clip = clips[Random.Range(0, clips.Length)]; animation.Play(); }
Your answer

Follow this Question
Related Questions
How to change a game objects tag after it has been randomly selected? 0 Answers
is it possible toggle bool at editor by script? 1 Answer
Why do I keep getting 'The object you want to instantiate is null' warning? 1 Answer
Choose random object from array not the same as the randomly chosen object before 1 Answer
C# script, light in array randomic 0 Answers