using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playerControl : MonoBehaviour
{
public float speed = 5f;
private float movement = 0f;
private Rigidbody2D rigidbody;
public float jumpForce = 8f;
public Transform groundCheck;
public float checkRadious;
public LayerMask whatIsGround;
private bool isTouchingGround;
private Animator playAnimation;
private bool doubleTap;
private bool attack=true;
private bool facingRight;
public GameObject kunai;
// Start is called before the first frame update
void Start()
{
facingRight = true;
rigidbody = GetComponent<Rigidbody2D>();
playAnimation = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
//counting click
isTouchingGround = Physics2D.OverlapCircle (groundCheck.position,checkRadious,whatIsGround);
//Horizontal movement
movement = Input.GetAxis ("Horizontal");
//getting current animation info
if(!this.playAnimation.GetCurrentAnimatorStateInfo(0).IsTag("attack") && !this.playAnimation.GetCurrentAnimatorStateInfo(0).IsTag("throw")){
if (movement > 0) {
rigidbody.velocity = new Vector2 (movement * speed, rigidbody.velocity.y);
direction (movement);
} else if (movement < 0) {
rigidbody.velocity = new Vector2 (movement * speed, rigidbody.velocity.y);
direction (movement);
} else {
rigidbody.velocity = new Vector2 (0,rigidbody.velocity.y);
}
if(Input.GetKeyDown(KeyCode.Space) && isTouchingGround){
rigidbody.velocity = new Vector2 (rigidbody.velocity.x,jumpForce);
}
}
if(Input.GetMouseButton(1) && isTouchingGround){
playAnimation.SetTrigger ("attack");
rigidbody.velocity = Vector2.zero;
}
if(Input.GetMouseButton(0)&& isTouchingGround){
playAnimation.SetTrigger ("throw");
rigidbody.velocity = Vector2.zero; //setting velocity to zero till animation is over
throwKnife (0);
}
playAnimation.SetFloat ("speed",Mathf.Abs(rigidbody.velocity.x));
playAnimation.SetBool ("glide",isTouchingGround);
}
public void throwKnife(int value){
direction (movement);
if (facingRight) {
GameObject tmp = (GameObject)Instantiate (kunai,transform.position,Quaternion.Euler(new Vector3(0,0,-90)));
tmp.GetComponent<kunai>().Initialize (Vector2.right);
} else {
GameObject tmp = (GameObject)Instantiate (kunai,transform.position,Quaternion.Euler(new Vector3(0,0,90)));
tmp.GetComponent<kunai>().Initialize (Vector2.left);
}
}
private void direction(float movement){
if(movement>0 && !facingRight ||movement<0 && facingRight){
facingRight = !facingRight;
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;
}
}
}
if(Input.GetMouseButton(0)&& isTouchingGround){
playAnimation.SetTrigger ("throw");
rigidbody.velocity = Vector2.zero; //setting velocity to zero till animation is over
throwKnife (0);
}
Replace This Line of code with Below one
if(Input.GetMouseButtonDown(0)&& isTouchingGround){
playAnimation.SetTrigger ("throw");
rigidbody.velocity = Vector2.zero; //setting velocity to zero till animation is over
throwKnife (0);
}
Basically what Input.GetMouseButtonDown does is only receive it when it gets down and the one Input.GetMouseButton that ur using receive input when mouse button being hold
Its Like
1st Input.GetMouseButtonDown - 1 time on down (prerssed)
2nd Input.GetMouseButton - number of time frames passed key holded
3rd Input.GetMouseButtonUp - 1 time on up (released)
@rkerketta