Please help I’m making a 2D Platformer Game and following tutorial but when I jump it freezes if I’m still holding down the A or D keys. Any idea how to fix this? I hope you understand my code, it is my first time to post here. Thank you in advanced!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityStandardAssets.CrossPlatformInput;
public class HeroController : MonoBehaviour {
private Rigidbody2D myBody;
//movement
[SerializeField]
private float movementSpd;
private bool facingRight;
//animation
Animator anim;
//attack
private bool attack;
//jump
public float jumpForce;
[SerializeField]
private Transform[] groundPoints;
[SerializeField]
private float groundRadius;
[SerializeField]
private LayerMask whatIsGround;
public float fallMult = 2.5f;
public bool isGrounded ;
private bool isJump;
[SerializeField]
private bool jumpAttack;
void Start () {
facingRight = true;
myBody = gameObject.GetComponent<Rigidbody2D> ();
anim = gameObject.GetComponent<Animator> ();
}
void Update(){
HandleInput ();
if (myBody.velocity.y < 0) {
myBody.velocity += Vector2.up * Physics2D.gravity.y * (fallMult - 1) * Time.deltaTime;
}
}
void FixedUpdate () {
isGrounded = IsGrounded ();
float horizontal = Input.GetAxis ("Horizontal");
anim.SetFloat ("Speed", Mathf.Abs (horizontal));
HandleMovement (horizontal);
HandleAttacks ();
HandleLayers ();
ResetValues ();
}
private void HandleMovement(float horizontal){
if (isGrounded && isJump) {
isGrounded = false;
myBody.AddForce (Vector2.up * jumpForce);
anim.SetTrigger ("Jump");
//attack = true;
}
if (myBody.velocity.y < 0) {
anim.SetBool ("Land", true);
}
if (!this.anim.GetCurrentAnimatorStateInfo (0).IsTag ("heroAttack") && isGrounded) {
myBody.velocity = new Vector2 (horizontal *movementSpd, myBody.velocity.y);
}
if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight) {
facingRight = !facingRight;
Vector3 playerScale = transform.localScale;
playerScale.x *= -1f;
transform.localScale = playerScale;
}
}
private void HandleInput (){
if (Input.GetKeyDown(KeyCode.Q)) {
attack = true;
jumpAttack = true;
}
if (Input.GetKeyDown (KeyCode.Space) && isGrounded) {
isJump = true;
}
}
private void HandleAttacks(){
if (attack && !this.anim.GetCurrentAnimatorStateInfo (0).IsTag ("heroAttack")) {
anim.SetTrigger ("Attack");
myBody.velocity = Vector2.zero;
}
if (jumpAttack && !isGrounded && !this.anim.GetCurrentAnimatorStateInfo(1).IsTag("JumpAttack")) {
anim.SetTrigger ("Attack");
}
if (!jumpAttack && this.anim.GetCurrentAnimatorStateInfo (1).IsTag ("JumpAttack")) {
anim.ResetTrigger ("Attack");
}
}
private bool IsGrounded(){
if (myBody.velocity.y <= 0) {
foreach (Transform point in groundPoints) {
Collider2D[] colliders = Physics2D.OverlapCircleAll (point.position, groundRadius, whatIsGround);
for (int i = 0; i < colliders.Length; i++) {
if (colliders *.gameObject != gameObject) {*
-
anim.ResetTrigger ("Jump");*
-
anim.SetBool ("Land", false);*
-
return true;*
-
}*
-
}*
-
}*
-
}*
-
return false;*
-
}*
-
private void HandleLayers(){*
-
if (!isGrounded) {*
-
anim.SetLayerWeight (1, 1);*
-
} else {*
-
anim.SetLayerWeight (1, 0);*
-
}*
-
}*
-
private void ResetValues(){*
-
attack = false;*
-
isJump = false;*
-
jumpAttack = false;*
-
}*
}