Here is the testing script (a bit long) to build the whole character:
public class assemble : MonoBehaviour {
Transform me;
Animator anim;
private Vector3 position;
NavMeshAgent agent;
GameObject head;
GameObject torso;
GameObject hands;
GameObject legs;
GameObject feet;
RuntimeAnimatorController contr;
// Use this for initialization
void Start () {
me = transform;
anim = GetComponent<Animator>();
agent = GetComponent<NavMeshAgent> ();
Object test = Resources.Load("vmale-anim");
contr = (RuntimeAnimatorController)RuntimeAnimatorController.Instantiate(test);
anim.runtimeAnimatorController = contr;
GameObject prefab = Resources.Load("vmale-head") as GameObject;
head = Instantiate(prefab) as GameObject;
head.transform.SetParent(me);
head.transform.localPosition = Vector3.zero;
head.transform.localRotation = Quaternion.Euler(Vector3.zero);
head.GetComponent<Animator> ().runtimeAnimatorController = contr;
//build torso
prefab = Resources.Load("vmale-torso") as GameObject;
//prefab = Resources.Load("items/armors/iron_armor") as GameObject;
torso = Instantiate(prefab) as GameObject;
torso.transform.SetParent(me);
torso.transform.localPosition = Vector3.zero;
torso.transform.localRotation = Quaternion.Euler(Vector3.zero);
torso.GetComponent<Animator> ().runtimeAnimatorController = contr;
//build hands
prefab = Resources.Load("vmale-hands") as GameObject;
hands = Instantiate(prefab) as GameObject;
hands.transform.SetParent(me);
hands.transform.localPosition = Vector3.zero;
hands.transform.localRotation = Quaternion.Euler(Vector3.zero);
hands.GetComponent<Animator> ().runtimeAnimatorController = contr;
//build legs
prefab = Resources.Load("vmale-legs") as GameObject;
//prefab = Resources.Load("items/armors/iron_leggins") as GameObject;
legs = Instantiate(prefab) as GameObject;
legs.transform.SetParent(me);
legs.transform.localPosition = Vector3.zero;
legs.transform.localRotation = Quaternion.Euler(Vector3.zero);
legs.GetComponent<Animator> ().runtimeAnimatorController = contr;
//build feet
prefab = Resources.Load("vmale-feet") as GameObject;
feet = Instantiate(prefab) as GameObject;
feet.transform.SetParent(me);
feet.transform.localPosition = Vector3.zero;
feet.transform.localRotation = Quaternion.Euler(Vector3.zero);
feet.GetComponent<Animator> ().runtimeAnimatorController = contr;
}
// Update is called once per frame
void Update () {
if (agent.pathStatus == NavMeshPathStatus.PathComplete) {
anim.SetInteger("CharacterState", 1);
}
if (Input.GetKey (KeyCode.Mouse0)) {
//basic character movement for testing
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit, 1000))
{
position = new Vector3(hit.point.x,hit.point.y,hit.point.z);
agent.SetDestination(position);
anim.SetInteger("CharacterState", 2);
}
}
}
}
The only problem is that it fails to change animations, stays forever in Idle.