- Home /
How can I use just one script to move randomly several characters from an array?
Hi guys!.. I have trying for a couple of days to do that, but now I'm stuck.
I have a List of Characters. After several time, I want to select one randomly and I give it the animation "walk" until the character arrive to a point. When the character arrives to the point, it needs to stay there for a while and then, after several seconds, it needs to turn around (eulerAngles(0, 180, 0)) and walk back until a point and them I need to destroy that character and select a new one from the list and do the same thing!
This is the script I have so far:
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Please : MonoBehaviour { public List CharacterList = new List(); public Vector3 Target = new Vector3(-18.03273f, 0.09936082f, 1.479195f); float speed = 3f; public GameObject characterChosen;
bool doIt = true;
void Start ()
{
ChooseCharacter();
}
void Update ()
{
Invoke ("Move", 5);
}
void Move(){
if (characterChosen.transform.position.z <= 1.910322) {
characterChosen.transform.animation.CrossFade ("walk");
characterChosen.transform.Translate (Vector3.forward * Time.deltaTime * speed);
}
else {
characterChosen.transform.animation.CrossFade("idle1");
Invoke("GoBack", 10);
StartCoroutine (MoveRest());
}
}
IEnumerator MoveRest(){
yield return new WaitForSeconds(10);
if (doIt == true) {//This is for avoid that ChooseCharacter is selected several times
doIt = false;
ChooseCharacter();
}
if (characterChosen.transform.position.z <= 1.910322) {
characterChosen.transform.animation.CrossFade ("walk");
characterChosen.transform.Translate (Vector3.forward * Time.deltaTime * speed);
}
else {
characterChosen.transform.animation.CrossFade("idle1");
Invoke("GoBack", 5);
}
}
void GoBack(){
if (characterChosen.transform.position.z >= -37.63817) {
characterChosen.transform.eulerAngles = new Vector3(0,180,0);
characterChosen.transform.animation.CrossFade ("walk");
characterChosen.transform.Translate (Vector3.forward * Time.deltaTime * speed);
}
}
void ChooseCharacter(){
if (CharacterList.Count >= 1) {
int characterIndex = Random.Range (0, CharacterList.Count);
characterChosen = CharacterList[characterIndex];
CharacterList.RemoveAt (characterIndex);
}
}
} Please, I really need your help! So, How can I use this script to select the characters from a random List and do what I wrote below above?
Thanks so much in advance!
Your answer
![](https://koobas.hobune.stream/wayback/20220613070609im_/https://answers.unity.com/themes/thub/images/avi.jpg)