A few days ago I first used the new input system of unity for my game and I ran into a problem. So basically I cant seem to figure out how to implement cooldown in the new system. Either it just overall doesnt work, it breaks other code or it just gives me a hundred errors. I just cant figure it out. I hope someone can help me implement just a very simple adjustable cooldown timer for the playerattack. I already want to thank everyone helping me with this!
Btw: I used the startTimeBtwAttack and timeBtwAttack variables for my cooldown in the past, but they dont work anymore. Had to remove some code so yeah...
PlayerAttack and Movement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Move : MonoBehaviour
{
public int damage;
private float timeBtwAttack;
public float startTimeBtwAttack;
public float attackRangeX;
public float attackRangeY;
public Transform attackPos;
public LayerMask whatIsEnemies;
//Move Start
[SerializeField] private Rigidbody2D rb2D;
[SerializeField] private float speed;
private Vector2 moveInputValue;
private void OnMove(InputValue value)
{
moveInputValue = value.Get<Vector2>();
Debug.Log(moveInputValue);
}
private void MoveLogicMethod()
{
Vector2 result = moveInputValue * speed * Time.fixedDeltaTime;
rb2D.velocity = result;
}
private void FixedUpdate()
{
MoveLogicMethod();
}
//Move End
//Attack Start
private void OnPlayerAttacking()
{
Collider2D[] enemiesToDamage = Physics2D.OverlapBoxAll(attackPos.position, new Vector2(attackRangeX, attackRangeY), 0, whatIsEnemies);
for (int i = 0; i < enemiesToDamage.Length; i++) {
enemiesToDamage[i].GetComponent<Enemy>().TakeDamage(damage);
}
timeBtwAttack = startTimeBtwAttack;
}
void OnDrawGizmosSelected(){
Gizmos.color = Color.black;
Gizmos.DrawWireCube(attackPos.position, new Vector3(attackRangeX, attackRangeY, 1));
}
//Attack End
}
Then there is the enemyScript for the enemy to receive damage:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Enemy : MonoBehaviour {
public int health;
private Animator anim;
void Update(){
if(health <= 0){
Destroy(gameObject);
}
}
public void TakeDamage(int damage){
health -= damage;
Debug.Log("damage TAKEN !");
}
}
Let me know if you need more infos!