攻击持续时间
This commit is contained in:
parent
8632b38362
commit
7d53694eb5
@ -6,6 +6,7 @@ using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using Debug = UnityEngine.Debug;
|
||||
using UnityEditor.Animations;
|
||||
using DG.Tweening;
|
||||
|
||||
public class Attack : MonoBehaviour
|
||||
{
|
||||
@ -35,6 +36,11 @@ public class Attack : MonoBehaviour
|
||||
[Header("攻击碰撞体")] public CircleCollider2D attackCollider;
|
||||
[Header("矩形攻击碰撞体")] public BoxCollider2D attackColliderBox;
|
||||
[Header("攻击范围图片")] public SpriteRenderer attackRangeSprite;
|
||||
[Header("攻击持续时间")] public float AttackStayTime;//
|
||||
|
||||
public bool isAttack = true;
|
||||
public bool flag = false;
|
||||
[HideInInspector] public float timer = 0;
|
||||
//public float AttackScope
|
||||
//{
|
||||
// [DebuggerStepThrough]
|
||||
@ -50,40 +56,37 @@ public class Attack : MonoBehaviour
|
||||
|
||||
public async void Start()
|
||||
{
|
||||
//AttackScope = attackScope;
|
||||
bulletData = new BulletData();
|
||||
bulletData.BulletScattering = 1;
|
||||
|
||||
animator.SetFloat("AttackSpeed", AttackSpeed);
|
||||
|
||||
//CircleCollider2D circleCollider2D = GetComponent<CircleCollider2D>();
|
||||
|
||||
|
||||
attackCooldown = transform.parent.GetComponent<enemy>().AttackCD;
|
||||
|
||||
SetAttackRange(); // 设置攻击范围
|
||||
|
||||
while (true)
|
||||
while (isAttack)
|
||||
{
|
||||
if (attackCollider || attackColliderBox)
|
||||
{
|
||||
|
||||
// 检查是否可以进行攻击(基于冷却时间)
|
||||
if (Time.time - lastAttackTime >= attackCooldown)
|
||||
{
|
||||
|
||||
|
||||
|
||||
if (attackCollider)
|
||||
{
|
||||
Collider2D[] colliders = Physics2D.OverlapCircleAll(attackCollider.transform.position, attackCollider.radius);
|
||||
GetAllColliderInAttackRange(colliders);
|
||||
}
|
||||
else if (attackColliderBox)// 使用 OverlapBoxAll 来检测矩形区域内的所有碰撞体
|
||||
else if (attackColliderBox)
|
||||
{
|
||||
// 获取碰撞体的尺寸
|
||||
Vector2 boxSize = attackColliderBox.size;
|
||||
|
||||
// 只检测右边一半,修改尺寸
|
||||
boxSize.x /= 2f; // 将宽度减半,只覆盖右边一半区域
|
||||
boxSize.x /= 2f;
|
||||
|
||||
// 获取碰撞体的中心位置
|
||||
Vector3 colliderCenter = attackColliderBox.transform.position;
|
||||
@ -92,20 +95,15 @@ public class Attack : MonoBehaviour
|
||||
Vector3 rightCenter = new Vector3(colliderCenter.x + attackColliderBox.size.x / 4f, colliderCenter.y, colliderCenter.z);
|
||||
|
||||
// 使用OverlapBoxAll检测右半部分区域
|
||||
Collider2D[] colliders = Physics2D.OverlapBoxAll(
|
||||
rightCenter, // 右半部分的中心位置
|
||||
boxSize, // 新的尺寸,只覆盖右半部分
|
||||
0f // 没有旋转
|
||||
);
|
||||
Debug.Log("进入范围");
|
||||
Collider2D[] colliders = Physics2D.OverlapBoxAll(rightCenter, boxSize, 0f);
|
||||
GetAllColliderInAttackRange(colliders);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
lastAttackTime = Time.time; // 更新上次攻击时间
|
||||
}
|
||||
|
||||
|
||||
|
||||
await Task.Delay(100); // 延迟,避免过于频繁地检测
|
||||
}
|
||||
else
|
||||
@ -115,7 +113,24 @@ public class Attack : MonoBehaviour
|
||||
}
|
||||
}
|
||||
|
||||
public void GetAllColliderInAttackRange(Collider2D[] colliders)//遍历所有在攻击范围内的敌人
|
||||
|
||||
private void Update()
|
||||
{
|
||||
if (flag)
|
||||
{
|
||||
timer += Time.deltaTime;
|
||||
if (timer>AttackStayTime)
|
||||
{
|
||||
animator.SetInteger("State", 0);
|
||||
isAttack = true;
|
||||
timer = 0;
|
||||
flag = false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public void GetAllColliderInAttackRange(Collider2D[] colliders)
|
||||
{
|
||||
foreach (Collider2D collider in colliders)
|
||||
{
|
||||
@ -128,23 +143,26 @@ public class Attack : MonoBehaviour
|
||||
if (bulletPrefab.GetComponent<Bullet>().myBulletType != BulletType.Spraying)
|
||||
{
|
||||
direction = (targetRole.transform.position - BulletStartPos.position).normalized;
|
||||
}
|
||||
//animator.SetInteger("State", 1);
|
||||
animator.SetTrigger("Attack");
|
||||
lastAttackTime = Time.time; // 更新上次攻击时间
|
||||
}
|
||||
else if (bulletPrefab.GetComponent<Bullet>().myBulletType == BulletType.Spraying)
|
||||
{
|
||||
animator.SetInteger("State", 1);
|
||||
|
||||
flag = true;
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
/*attack(targetRole);
|
||||
lastAttackTime = Time.time; // 更新上次攻击时间*/
|
||||
|
||||
lastAttackTime = Time.time; // 更新上次攻击时间
|
||||
}
|
||||
|
||||
break; // 只攻击一个目标
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
if (animator != null)
|
||||
{
|
||||
//animator.SetInteger("State", 0);
|
||||
@ -153,7 +171,6 @@ public class Attack : MonoBehaviour
|
||||
{
|
||||
role.animationHighlight = 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -76,6 +76,7 @@ public class Role : Fun
|
||||
public float CritRate;//暴击率
|
||||
public float CriticalHitRateBonus;//暴击加成
|
||||
|
||||
|
||||
public float Hp
|
||||
{
|
||||
get => hp;
|
||||
@ -535,7 +536,10 @@ public class Role : Fun
|
||||
//Debug.LogError("地面敌人加成");
|
||||
}
|
||||
|
||||
|
||||
if (GetComponent<enemy>())
|
||||
{
|
||||
GetComponent<enemy>().HarmNumber = Mathf.Round(hurt);
|
||||
}
|
||||
return Mathf.Round(hurt);
|
||||
}
|
||||
|
||||
|
@ -85,7 +85,7 @@ public class SimplePathfindingDoTween : Fun
|
||||
DOTween.To(() => currentSpeedFactor, x => pathTween.timeScale = x, newSpeedFactor, restoreTime)
|
||||
.OnKill(() =>
|
||||
{
|
||||
Debug.Log("ÉèÖüõËÙ+++++++++++++++++");
|
||||
// Debug.Log("ÉèÖüõËÙ+++++++++++++++++");
|
||||
// 在延迟结束后恢复原速度
|
||||
pathTween.timeScale = 1f; // 恢复原速度
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user