The story is here:
I’m making an adventure game In the first part of the character’s battle, I wrote the code that when pressing the “E” key, it will launch battle 1.
But when it comes to battle 2, I decided that if I press the E key again within a short amount of time from the first press, the 2nd ability will be activated.
But unfortunately, I am not qualified to do it, does anyone know how to turn the code below to achieve what I just said?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class combat : MonoBehaviour
{
[SerializeField] private Animator animator;
public Transform attackPoint;
public LayerMask enemyLayers;
public float attackRange = 0.5f;
public int attackDamage = 40;
public float attackRate = 2f;// giới hạn số lần tấn công trong 1 giây;
float nextAttackTime = 0f;
// Update is called once per frame
void Update()
{
if (Time.time >= nextAttackTime)
{
if (Input.GetKeyDown(KeyCode.E))
{
Attack();
nextAttackTime = Time.time + 0.8f / attackRate;
Debug.Log(nextAttackTime);
}
}
}
void Attack()
{
// bắt đầu hoạt ảnh tấn công.
animator.SetTrigger("Attack");
// phát hiện kẻ thù trong vùng tấn công.
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);
// tiêu diệt kẻ thù
foreach(Collider2D enemy in hitEnemies)
{
//Debug.Log("We hit" = enemy.name);
enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
}
}
void Attack1()
{
// bắt đầu hoạt ảnh tấn công.
animator.SetTrigger("Attack1");
// phát hiện kẻ thù trong vùng tấn công.
Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(attackPoint.position, attackRange, enemyLayers);
// tiêu diệt kẻ thù
foreach (Collider2D enemy in hitEnemies)
{
//Debug.Log("We hit" = enemy.name); ----> qua dò lỗi từng cái thì có vẻ như lỗi là từ dòng code này
//enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
}
}
void OnDrawGizmosSelected()
{
if (attackPoint == null)
return;
Gizmos.DrawWireSphere(attackPoint.position, attackRange);
}
}