- Home /
Question by
AsAnAsterisk · Jun 24, 2017 at 02:49 AM ·
animationscripting problemanimatorslider
How to play an animation when a slider is at a certain value.
I have an enemy, I already have all the animations and transitions set up for it in the animator, and the enemy plays an injury animation when hit. I have a "vulnerable" animation for when its healthbar is at zero but I have no clue how to script it in a way so that its "IsVulnerable" animation plays when the healthbar is at a certain value.
public class Chase : MonoBehaviour {
public Transform player;
public CapsuleCollider cc;
private Animator anim;
private Rigidbody myRigidbody;
private Vector3 moveDirection;
public Collider attack;
public Slider healthbar;
void OnTriggerEnter(Collider attack)
{
if (attack.gameObject.tag == "PlayerAttack") {
healthbar.value -= 20;
anim.SetBool("IsInjured", true);
}
}
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
myRigidbody = GetComponent<Rigidbody>();
cc.enabled=false;
}
// Update is called once per frame
void Update()
{
moveDirection = Vector3.zero;
if (Vector3.Distance(player.position, this.transform.position) < 4)
{
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
Quaternion.LookRotation(direction), 0.3f);
anim.SetBool("IsIdle", false);
if (direction.magnitude > .8)
{
moveDirection = direction.normalized;
anim.SetBool("IsMoving", true);
anim.SetBool("IsBiting", false);
anim.SetBool("IsInjured", false);
}
else
{
anim.SetBool("IsBiting", true);
anim.SetBool("IsMoving", false);
}
}
else
{
anim.SetBool("IsIdle", true);
anim.SetBool("IsMoving", false);
anim.SetBool("IsBiting", false);
}
}
public void ActiveCollider(int active){
if (active == 0)
cc.enabled = false;
if (active == 1)
cc.enabled = true;
}
void FixedUpdate()
{
myRigidbody.AddForce(moveDirection * 70f);
}
}
Comment
Best Answer
Answer by cstooch · Jun 24, 2017 at 02:52 AM
Have you tried... ?
if (healthbar.value <= 0)
anim.SetBool("isVulnerable", true);
Where "isVulnerable" is a new (or existing) animation parameter that is the transition to your Vulnerable animation?