Well, I have a code and I have a problem with it. So, when i press a little bit faster the jump button I’ve made more times, and then when i release it, the player keeps jumping and he doesn’t stop, It’s like he is jumping repeatedly. btw. here’s my code, if you can help me I’ll be glad… Thanks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : Character {
private static Player instance;
private float direction;
private bool move;
private float btnHorizontal;
public static Player Instance
{
get
{
if (instance == null)
{
instance = GameObject.FindObjectOfType<Player> ();
}
return instance;
}
}
[SerializeField]
private Transform[] groundPoints;
[SerializeField]
private float groundRadius;
[SerializeField]
private LayerMask whatIsGround;
[SerializeField]
private bool airControl;
[SerializeField]
private float jumpForce;
public Rigidbody2D MyRigidbody { get; set; }
public override void Start ()
{
base.Start ();
MyRigidbody = GetComponent<Rigidbody2D> ();
}
void Update()
{
HandleInput();
}
void FixedUpdate () {
{
float horizontal = Input.GetAxis ("Horizontal");
OnGround = IsGrounded ();
if (move)
{
this.btnHorizontal = Mathf.Lerp (btnHorizontal, direction, Time.deltaTime * 2);
HandleMovement (direction);
Flip (direction);
}
else
{
HandleMovement (horizontal);
Flip (horizontal);
}
HandleLayers ();
}
}
private void HandleMovement(float horizontal)
{
if (MyRigidbody.velocity.y < 0)
{
MyAnimator.SetBool ("land", true);
}
if (!Attack && !Slide || (OnGround || airControl))
{
MyRigidbody.velocity = new Vector2 (horizontal * movementSpeed, MyRigidbody.velocity.y);
}
if (Jump && MyRigidbody.velocity.y == 0)
{
MyRigidbody.AddForce(new Vector2(0, jumpForce));
}
MyAnimator.SetFloat ("speed", Mathf.Abs (horizontal));
}
private void HandleInput()
{
if (Input.GetKeyDown (KeyCode.Space))
{
MyAnimator.SetTrigger ("jump");
}
if (Input.GetKeyDown (KeyCode.Return))
{
MyAnimator.SetTrigger ("attack");
}
if (Input.GetKeyDown (KeyCode.AltGr))
{
MyAnimator.SetTrigger ("slide");
}
if (Input.GetKeyDown (KeyCode.T))
{
MyAnimator.SetTrigger ("throw");
}
if (Input.GetKeyDown (KeyCode.LeftAlt))
{
MyAnimator.SetTrigger ("lupercut");
}
if (Input.GetKeyDown (KeyCode.LeftControl))
{
MyAnimator.SetTrigger ("lkick");
}
if (Input.GetKeyDown (KeyCode.RightControl))
{
MyAnimator.SetTrigger ("rkick");
}
if (Input.GetKeyDown (KeyCode.Tab))
{
MyAnimator.SetTrigger ("rpunch");
}
}
private void Flip(float horizontal)
{
if (horizontal > 0 && !facingRight || horizontal < 0 && facingRight)
{
ChangeDirection ();
}
}
private bool IsGrounded()
{
if (MyRigidbody.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 [i].gameObject != gameObject)
{
return true;
}
}
}
}
return false;
}
private void HandleLayers()
{
if (!OnGround) {
MyAnimator.SetLayerWeight (1, 1);
}
else
{
MyAnimator.SetLayerWeight (1, 0);
}
}
public override void ThrowKnife(int value)
{
if (OnGround && value == 1 || !OnGround && value == 0)
{
base.ThrowKnife (value);
}
}
public void BtnJump()
{
MyAnimator.SetTrigger ("jump");
Jump = true;
}
public void BtnAttack()
{
MyAnimator.SetTrigger ("attack");
}
public void BtnSlide()
{
MyAnimator.SetTrigger ("slide");
}
public void BtnLUpercut()
{
MyAnimator.SetTrigger ("lupercut");
}
public void BtnLKick()
{
MyAnimator.SetTrigger ("lkick");
}
public void BtnRKick()
{
MyAnimator.SetTrigger ("rkick");
}
public void BtnRPunch()
{
MyAnimator.SetTrigger ("rpunch");
}
public void BtnMove(float direction)
{
this.direction = direction;
this.move = true;
}
public void BtnStopMove()
{
this.direction = 0;
this.btnHorizontal = 0;
this.move = false;
}
private void ResetValues()
{
Attack = false;
Slide = false;
Jump = false;
}
}