I’m attempting to play animation that changes the facial expressions of characters from a Game Manager but having some trouble. When the routine or method is called from within the class itself, it works fine. However, when invoked from the Game Manager, the expressions do not change. One more odd thing about this is that Movement animations, i.e. clapping or jumping, e.g. works fine when invoked from the Game Manager. Here’s a sample of the code:
// Called from Game Manager:
StartCoroutine(sceneController.setPlayerMood(string sMood, string sPlayer));
// Called within Class
StartCoroutine(setPlayerMood(string sMood, string sPlayer);
// routine in SceneController
private IEnumerator setPlayerMood(string sMood, string sPlayer)
{
Animator animator;
if( sPlayer == "Player1")
animator = Player1.GetComponent<Animator>();
else
animator = Player2.GetComponent<Animator>();
switch (sMood)
{
case "angry":
animator.GetComponent<Playanimation>().angry = true;
break;
case "disgust":
animator.GetComponent<Playanimation>().disgust = true;
break;
case "sad":
animator.GetComponent<Playanimation>().sad = true;
break;
case "happy":
animator.GetComponent<Playanimation>().happy = true;
break;
case "numb":
animator.GetComponent<Playanimation>().numb = true;
break;
case "amazed":
animator.GetComponent<Playanimation>().amazed = true;
break;
}
yield return null;
}
// Playanimation class
public class Playanimation : MonoBehaviour
{
public string anim;
public bool delayed;
public bool happy;
public bool sad;
public bool angry;
public bool amazed;
public bool disgust;
public bool numb;
void Start()
{
GetComponent<Animator>().Play(anim);
if (delayed)
{
StartCoroutine("playanim", anim);
}
if (happy) GetComponent<Animator>().SetLayerWeight(1, 1f);
else if (sad) GetComponent<Animator>().SetLayerWeight(2, 1f);
else if (angry) GetComponent<Animator>().SetLayerWeight(3, 1f);
else if (amazed) GetComponent<Animator>().SetLayerWeight(4, 1f);
else if (disgust) GetComponent<Animator>().SetLayerWeight(5, 1f);
else if (numb) GetComponent<Animator>().SetLayerWeight(6, 1f);
}
IEnumerator playanim(string anim)
{
GetComponent<Animator>().speed = 0.65f;
yield return new WaitForSeconds(Random.Range(0f, 2f));
GetComponent<Animator>().speed = 1f;
GetComponent<Animator>().Play(anim);
}
public void playtheanimation(string newanim)
{
anim = newanim;
StartCoroutine("playanim", anim);
}
}
}
Help is appreciated