Question by
jlkriechbaum · Apr 25, 2019 at 03:08 AM ·
scripting problemanimator
Animator.Play not working for me in a c# script
I've been making a little pixelart healthbar and am using animations for the different states based on percentage. For whatever reason, when i use Animator.Play, followed by the string of my states, the state is not played. The script otherwise works fine and gives no error messages. Thanks for any help you can give me.
public class Health : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
anim = GetComponent<Animator>();
}
public float maxhealth;
public float currenthealth;
private Animator anim;
// Update is called once per frame
void Update()
{
maxhealth = this.transform.parent.transform.gameObject.GetComponent<EnemyHealth>().maxhealth;
currenthealth = this.transform.parent.transform.gameObject.GetComponent<EnemyHealth>().currenthealth;
Collider2D[] colliders = gameObject.GetComponent<stayOnEnemy>().colliders;
if (colliders.Length > 0)
{
if ((currenthealth / maxhealth) * 100 > 80) //(∞,80)
{
anim.Play("State1", 0);
}
if ((currenthealth / maxhealth * 100 <= 80) && (currenthealth / maxhealth * 100 > 60)) //[80,60)
{
anim.Play("State2", 0);
}
if ((currenthealth / maxhealth * 100 <= 60) && (currenthealth / maxhealth * 100 > 40)) //[60,40)
{
anim.Play("State3", 0);
}
if ((currenthealth / maxhealth * 100 <= 40) && (currenthealth / maxhealth * 100 > 20)) //[40,20)
{
anim.Play("State4", 0);
}
if ((currenthealth / maxhealth) * 100 <= 20 && (currenthealth / maxhealth) * 100 > 0) //[20,0)
{
anim.Play("State5", 0);
}
if ((currenthealth / maxhealth) * 100 <= 0) //[0,-∞)
{
anim.Play("State6", 0);
}
}
}
}
unityproblem.png
(21.6 kB)
Comment
Your answer

Follow this Question
Related Questions
Delay while using GetButtonDown and Animator 0 Answers
How to modify animator transition conditions via script 0 Answers
HELP,Watched tutorial https://www.youtube.com/watch?v=Xnyb2f6Qqzg 1 Answer
how do I trigger a different game object outside a script? 0 Answers
Animator does not show up 0 Answers