Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by leandroreschke · Jan 08, 2014 at 10:08 AM · androidupdatejumpfixedupdateunity4.3

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;
     }
 } 
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image saud_ahmed020 · Mar 12, 2015 at 02:28 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image leandroreschke · Mar 10, 2015 at 08:00 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Physics, Force, Racing, and (Fixed)Update. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges