Quantcast
Viewing all articles
Browse latest Browse all 49

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(); } // 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; } }

Viewing all articles
Browse latest Browse all 49

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>