- Home /
Select Animations Freezing on First Frame
To start, yes; other people have asked this question, but those other people either A. have not been answered or B. did not use Mecanim, so it's hard to tell if their solution is the correct one.
At some point, seemingly without reason, all of my animations except for my idle animations freeze on frame 1 and, therefor, do not exit at end of when the animation should finish.
I have removed, edited, and completely scrapped all related code, restarted Unity multiple times, and completely remade my animations and Mecanim setup twice; none of these have worked, however.
Here are the related scripts:
public class Entity:MonoBehaviour{
public Animator animator;
public Rigidbody2D rigidbody2D;
public float direction;
public int maxHP = 3;
public int HP = 3;
public int maxStamina = 10;
public int stamina = 10;
public string name = "Enemy";
public float moveSpeed = 1;
public float recoverSpeed = .75f;
public int state = 2;
public bool isThrust = false;
public bool isReact = false;
public bool isParry = false;
public float timer;
public bool timerOn = false;
public float animTimer;
public bool animTimerOn = false;
public virtual void Start(){
animator = GetComponent<Animator>();
rigidbody2D = GetComponent<Rigidbody2D>();
GetComponent<SpriteRenderer>().color = new Color(Random.Range (0.1f, .99f), Random.Range (0.1f, .99f), Random.Range (0.1f, .99f), 1);
}
public virtual void Update(){
if(!timerOn){
if(timer <= 0){
timer = Random.Range(.1f, 3);
state = Mathf.RoundToInt(Random.Range(.6f, 3.4f));
}else{
timer -= Time.deltaTime;
}
}else if(timerOn){
if(timer > 0){
timer -= Time.deltaTime;
}else{
timerOn = false;
isThrust = false;
isReact = false;
isParry = false;
}
}
animator.SetFloat("State", state);
animator.SetBool("IsThrust", isThrust);
animator.SetBool("IsReact", isReact);
animator.SetBool("IsParry", isParry);
}
public void Flip(){
transform.localScale = new Vector2(transform.localScale.x * -1, transform.localScale.y);
}
public void Thrust(){
isThrust = true;
isReact = false;
isParry = false;
timer = .625f;
timerOn = true;
}
public void React(){
isThrust = false;
isReact = true;
isParry = false;
timer = .5f;
timerOn = true;
}
public void Parry(){
isThrust = false;
isReact = false;
isParry = true;
timer = .5f;
timerOn = true;
}
public void OnTriggerStay2D(Collider2D other){
if(isThrust){
if(!animTimerOn){
animTimerOn = true;
animTimer = .25f;
}else if(animTimer > 0){
animTimer -= Time.deltaTime;
}else{
animTimerOn = false;
if(other.tag == "Entity"){
Entity entity = other.GetComponent<Entity>();
print(entity.name + " is in combat!");
int dirTo = transform.position.x - entity.transform.position.x > 0 ? 1 : -1;
if(entity.state != state){
entity.Parry();
entity.HP--;
entity.rigidbody2D.velocity = new Vector2(recoverSpeed * .5f * -dirTo, 0);
}else{
React();
entity.Parry();
rigidbody2D.velocity = new Vector2(recoverSpeed * dirTo, 0);
}
}
}
}
}
}
along with the player script,
public class Player:Entity{
public bool leftDown = false;
public float leftDownTime;
public bool rightDown = false;
public float rightDownTime;
public void OnEnable(){
name = "Player";
}
public override void Update(){
direction = transform.localScale.x;
if(!timerOn){
if(Input.GetMouseButton(1)){
state = 1;
}else if(Input.GetMouseButton(0)){
state = 3;
}else{
state = 2;
}
if(Input.GetButtonDown("Space")){
Thrust();
return;
}
if(Input.GetButton("Horizontal")){
//isMove = true;
//timer = .3f;
//timerOn = true;
rigidbody2D.velocity = new Vector2(moveSpeed * Input.GetAxisRaw("Horizontal"), rigidbody2D.velocity.y);
if(direction != Input.GetAxisRaw("Horizontal")){
//Flip();
}
//Vector2.Lerp(transform.position, new Vector2(transform.position.x + Input.GetAxis("Horizontal"), transform.position.y), moveSpeed * Time.deltaTime);
}else{
rigidbody2D.velocity = new Vector2(0, rigidbody2D.velocity.y);
//isMove = false;
}
}else{
if(timer > 0){
timer -= Time.deltaTime;
}else{
timerOn = false;
//isMove = false;
isThrust = false;
isReact = false;
isParry = false;
}
}
animator.SetFloat("State", state);
//animator.SetBool("IsMove", isMove);
animator.SetBool("IsThrust", isThrust);
animator.SetBool("IsReact", isReact);
animator.SetBool("IsParry", isParry);
}
}
Any help would be highly appreciated
Update: I made a completely new project and redid the code, animations, and mecanim layout completely to no avail. the problem persists.
Can you include some screenshots of the animations in the inspector, how they're imported, and the animator controller for at least one of the problem animations? The code looks okay, at first glance, so the problem might be in the components setup.
Answer by noonoox · Jul 03, 2015 at 04:54 AM
Hey guys, sorry to waste your time. I rewrote my code and, while I'm still not entirely sure, I believe it was caused by some strange bug of me using all those bools instead of triggers
Your answer
Follow this Question
Related Questions
How do you check to see if an animation is playing in a Sub-State machine? 0 Answers
mecanim dynamic animation choice 0 Answers
Mecanim issue with mirror animations and blend 1 Answer
Is there a way to optimize animators? 3 Answers
Create 2D box colliders based on current frame of animation 0 Answers