- Home /
Problem with animation and void fixedupdate()
Look i'm trying to make a 2D game with unity 4.3, and i have a problem, don't need the code you just need to know my logic. I'm using fixed update to create a moving control with two buttons, right and left, why i use fixed update? because i have a jump button, if i put everything on update when i jump and press right or left the character fall slowly freezing like problem with Update, so i moved it to fixedupdate() and just jump is on Update(). now the problem is fixed but a new one appear, i use animator, and a bool parameter, if i press right or left it animate, as i'm using now right and left on fixed update sometimes it miss the bool animation parameter and let my character running when IDLE animation supposed to be running or walking freezed as IDLE animation. Did you guys catched my logic?
If i use all in update, everything okay, except jump. If i use fixedupdate for moving, everything is okay, except for animation
I'm developing to android
The control code
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
public Transform player;
public float animatorSpeed = 0;
bool grounded = false;
public Transform groundCheck;
float groundRadius = 0.2f;
public LayerMask whatIsGround;
public float jumpForce = 700f;
public GUITexture jumpButton;
bool facingRight = true;
public float speed = 10f;
public GUITexture leftButton;
public GUITexture rightButton;
public static int currTouch = 0;
Animator anim;
// Use this for initialization
void Start () {
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update () {
grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
anim.SetBool ("Ground", grounded);
anim.SetFloat ("vSpeed", rigidbody2D.velocity.y);
if(Input.touches.Length <= 0)
{
//if no touches then execute this code
}
else{ //if there is a touch
//loop through all the the touches on screen
for(int i = 0; i < Input.touchCount; i++){
currTouch = i;
Debug.Log(currTouch);
//executes this code for current touch (i) on screen
if(Input.GetTouch(i).phase == TouchPhase.Stationary){
if(leftButton != null && (leftButton.HitTest(Input.GetTouch(i).position))){
//if current touch hits our guitexture, run this code
player.Translate(-1 * Vector2.right * speed, 0);
if(grounded){
anim.SetBool("Walking", true);
}
if(facingRight){
Flip ();
}
}
else if(rightButton != null && (rightButton.HitTest(Input.GetTouch(i).position))){
//if current touch hits our guitexture, run this code
player.Translate(Vector2.right * speed, 0);
if(grounded){
anim.SetBool("Walking", true);
}
if(!facingRight){
Flip ();
}
}
else if(jumpButton != null && (jumpButton.HitTest(Input.GetTouch(i).position))){
if (grounded){
anim.SetBool ("Ground", false);
rigidbody2D.AddForce(new Vector2(0, jumpForce));
}
}
}
else if (Input.GetTouch(i).phase == TouchPhase.Ended){
anim.SetBool("Walking", false);
}
else anim.SetBool("Walking", false);
}
}
}
void Flip(){
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
Update method is called once per frame but the frame rate is 60 per seconds for android. So the problem is when you touch, the frames changes and the required effect is not visible.
Solution: This problem can be solved by stopping the frame rate of update method. Frame rate can be stopped by bool type variable. e.g
private bool isTouch = false;
void Update() {
if(!isTouch)
{
// place you else part code
isTouch = true;
}
}
And make a function which will execute when your animations complete like
public void OnAnyAnimationComplete() {
isTouch = false;
}
call this method on last frame of your all animations.
Answer by alok-kr-029 · Mar 10, 2015 at 07:49 AM
How did you solved it as I am also getting similar bug of animation in fixedUpdate?
To be honest, 1 year ago i solved this with no help, but now i can't remember, in my country it's bed time and I'm going to sleep, but if you want my help, than post your code and tell me in detail what's going on. I will be glad to help you.
Your answer
Follow this Question
Related Questions
counter doesnt work in update 2 Answers
2D physics jump issue. 1 Answer
Exact difference between fixedDeltaTime and deltaTime? 3 Answers
Android update issue 0 Answers