- Home /
Question by
Demigoth · Jun 02, 2014 at 02:36 AM ·
animationanimator controllerparameters
switch animation from blend tree
Hi guys, so I got a question for using an animator controller in 2D. Ok so I got these animations (Run, Walk, Idle, Jump and Death) all of them are simple except jump that uses a blend tree. From code I call to them when needed. But when a condition is met I call the Death animation from the any state block. The problem is it works correctly on every animation except on the Jump state. If I am jumping when the conditions are met then nothing happens and no animation is played.
To be precise I got only the Death Animation on atomic. and the code that controls everything is here.... So any help would be greatly appreciated.
sing UnityEngine;
using System.Collections;
public class RunnerCharacter2D : MonoBehaviour {
// Public variables
public Transform groundCheck;
public UISlider freshBar;
// Private variables
private bool _facingRight = true;
private float _maxSpeed = 10f;
[SerializeField] private float _jumpForce = 400f;
private bool _airControl = false;
[SerializeField] private LayerMask _whatIsGround;
private float _groundedRadius = 0.2f;
private bool _grounded = false;
private Animator _anim;
private float _maxFresh = 3f;
private float _freshness;
private bool _hasRotten;
void Awake() {
_anim = GetComponent<Animator>();
_freshness = _maxFresh;
_hasRotten = false;
}
void Start() {
_anim.SetBool("Alive", true);
}
void FixedUpdate() {
_grounded = Physics2D.OverlapCircle(groundCheck.position, _groundedRadius, _whatIsGround);
_anim.SetBool("Ground", _grounded);
_anim.SetFloat("vSpeed", rigidbody2D.velocity.y);
}
void Update() {
_freshness -= Time.deltaTime;
freshBar.value = _freshness / _maxFresh;
if (_hasRotten == false) {
if (freshBar.value <= 0) {
_anim.SetBool("Alive", false);
NotFresh ();
_hasRotten = true;
}
}
}
public void Move(bool _jump, float move) {
if (_grounded || _airControl) {
_anim.SetFloat("Speed", Mathf.Abs(move));
rigidbody2D.velocity = new Vector2(move * _maxSpeed, rigidbody2D.velocity.y);
if(move > 0 && !_facingRight) {
Flip();
} else if (move < 0 && _facingRight) {
Flip();
}
}
if (_grounded && _jump) {
_anim.SetBool("Ground", false);
rigidbody2D.AddForce(new Vector2(0f, _jumpForce));
}
}
void Flip ()
{
// Switch the way the player is labelled as facing.
_facingRight = !_facingRight;
// Multiply the player's x local scale by -1.
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
public void NotFresh () {
StartCoroutine(PlayDeathOnce("nFresh"));
}
public IEnumerator PlayDeathOnce (string paranName) {
_anim.SetBool (paranName, true);
yield return null;
_anim.SetBool (paranName, false);
}
}
Comment