攻击持续时间

This commit is contained in:
GL 2024-12-27 16:49:55 +08:00
parent 8632b38362
commit 7d53694eb5
3 changed files with 59 additions and 38 deletions

View File

@ -6,6 +6,7 @@ using System.Threading.Tasks;
using UnityEngine; using UnityEngine;
using Debug = UnityEngine.Debug; using Debug = UnityEngine.Debug;
using UnityEditor.Animations; using UnityEditor.Animations;
using DG.Tweening;
public class Attack : MonoBehaviour public class Attack : MonoBehaviour
{ {
@ -17,7 +18,7 @@ public class Attack : MonoBehaviour
[Header("攻击CD时间")] public float attackCooldown = 1f; // 攻击冷却时间 [Header("攻击CD时间")] public float attackCooldown = 1f; // 攻击冷却时间
private float lastAttackTime = 0f; // 上次攻击的时间 private float lastAttackTime = 0f; // 上次攻击的时间
[HideInInspector] [HideInInspector]
public List<GameObject> bulltes = new List<GameObject>(); public List<GameObject> bulltes = new List<GameObject>();
@ -35,6 +36,11 @@ public class Attack : MonoBehaviour
[Header("攻击碰撞体")] public CircleCollider2D attackCollider; [Header("攻击碰撞体")] public CircleCollider2D attackCollider;
[Header("矩形攻击碰撞体")] public BoxCollider2D attackColliderBox; [Header("矩形攻击碰撞体")] public BoxCollider2D attackColliderBox;
[Header("攻击范围图片")] public SpriteRenderer attackRangeSprite; [Header("攻击范围图片")] public SpriteRenderer attackRangeSprite;
[Header("攻击持续时间")] public float AttackStayTime;//
public bool isAttack = true;
public bool flag = false;
[HideInInspector] public float timer = 0;
//public float AttackScope //public float AttackScope
//{ //{
// [DebuggerStepThrough] // [DebuggerStepThrough]
@ -50,40 +56,37 @@ public class Attack : MonoBehaviour
public async void Start() public async void Start()
{ {
//AttackScope = attackScope;
bulletData = new BulletData(); bulletData = new BulletData();
bulletData.BulletScattering = 1; bulletData.BulletScattering = 1;
animator.SetFloat("AttackSpeed",AttackSpeed);
//CircleCollider2D circleCollider2D = GetComponent<CircleCollider2D>();
animator.SetFloat("AttackSpeed", AttackSpeed);
attackCooldown = transform.parent.GetComponent<enemy>().AttackCD; attackCooldown = transform.parent.GetComponent<enemy>().AttackCD;
SetAttackRange();//设置攻击范围 SetAttackRange(); // 设置攻击范围
while (true) while (isAttack)
{ {
if (attackCollider|| attackColliderBox) if (attackCollider || attackColliderBox)
{ {
// 检查是否可以进行攻击(基于冷却时间) // 检查是否可以进行攻击(基于冷却时间)
if (Time.time - lastAttackTime >= attackCooldown) if (Time.time - lastAttackTime >= attackCooldown)
{ {
if (attackCollider) if (attackCollider)
{ {
Collider2D[] colliders = Physics2D.OverlapCircleAll(attackCollider.transform.position, attackCollider.radius); Collider2D[] colliders = Physics2D.OverlapCircleAll(attackCollider.transform.position, attackCollider.radius);
GetAllColliderInAttackRange(colliders); GetAllColliderInAttackRange(colliders);
} }
else if (attackColliderBox)// 使用 OverlapBoxAll 来检测矩形区域内的所有碰撞体 else if (attackColliderBox)
{ {
// 获取碰撞体的尺寸 // 获取碰撞体的尺寸
Vector2 boxSize = attackColliderBox.size; Vector2 boxSize = attackColliderBox.size;
// 只检测右边一半,修改尺寸 // 只检测右边一半,修改尺寸
boxSize.x /= 2f; // 将宽度减半,只覆盖右边一半区域 boxSize.x /= 2f;
// 获取碰撞体的中心位置 // 获取碰撞体的中心位置
Vector3 colliderCenter = attackColliderBox.transform.position; 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); Vector3 rightCenter = new Vector3(colliderCenter.x + attackColliderBox.size.x / 4f, colliderCenter.y, colliderCenter.z);
// 使用OverlapBoxAll检测右半部分区域 // 使用OverlapBoxAll检测右半部分区域
Collider2D[] colliders = Physics2D.OverlapBoxAll( Collider2D[] colliders = Physics2D.OverlapBoxAll(rightCenter, boxSize, 0f);
rightCenter, // 右半部分的中心位置
boxSize, // 新的尺寸,只覆盖右半部分
0f // 没有旋转
);
Debug.Log("进入范围");
GetAllColliderInAttackRange(colliders); GetAllColliderInAttackRange(colliders);
} }
lastAttackTime = Time.time; // 更新上次攻击时间
} }
await Task.Delay(100); // 延迟,避免过于频繁地检测 await Task.Delay(100); // 延迟,避免过于频繁地检测
} }
else 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) foreach (Collider2D collider in colliders)
{ {
@ -128,23 +143,26 @@ public class Attack : MonoBehaviour
if (bulletPrefab.GetComponent<Bullet>().myBulletType != BulletType.Spraying) if (bulletPrefab.GetComponent<Bullet>().myBulletType != BulletType.Spraying)
{ {
direction = (targetRole.transform.position - BulletStartPos.position).normalized; direction = (targetRole.transform.position - BulletStartPos.position).normalized;
animator.SetTrigger("Attack");
} }
//animator.SetInteger("State", 1); else if (bulletPrefab.GetComponent<Bullet>().myBulletType == BulletType.Spraying)
animator.SetTrigger("Attack"); {
lastAttackTime = Time.time; // 更新上次攻击时间 animator.SetInteger("State", 1);
} flag = true;
else
{
/*attack(targetRole);
lastAttackTime = Time.time; // 更新上次攻击时间*/
}
lastAttackTime = Time.time; // 更新上次攻击时间
} }
break; // 只攻击一个目标 break; // 只攻击一个目标
} }
else else
{ {
if (animator != null) if (animator != null)
{ {
//animator.SetInteger("State", 0); //animator.SetInteger("State", 0);
@ -153,7 +171,6 @@ public class Attack : MonoBehaviour
{ {
role.animationHighlight = 0; role.animationHighlight = 0;
} }
} }
} }
} }
@ -195,7 +212,7 @@ public class Attack : MonoBehaviour
} }
}*/ }*/
public void Pointattack() public void Pointattack()
{ {

View File

@ -75,6 +75,7 @@ public class Role : Fun
public float AttackCD;//攻击CD public float AttackCD;//攻击CD
public float CritRate;//暴击率 public float CritRate;//暴击率
public float CriticalHitRateBonus;//暴击加成 public float CriticalHitRateBonus;//暴击加成
public float Hp public float Hp
{ {
@ -535,7 +536,10 @@ public class Role : Fun
//Debug.LogError("地面敌人加成"); //Debug.LogError("地面敌人加成");
} }
if (GetComponent<enemy>())
{
GetComponent<enemy>().HarmNumber = Mathf.Round(hurt);
}
return Mathf.Round(hurt); return Mathf.Round(hurt);
} }

View File

@ -85,7 +85,7 @@ public class SimplePathfindingDoTween : Fun
DOTween.To(() => currentSpeedFactor, x => pathTween.timeScale = x, newSpeedFactor, restoreTime) DOTween.To(() => currentSpeedFactor, x => pathTween.timeScale = x, newSpeedFactor, restoreTime)
.OnKill(() => .OnKill(() =>
{ {
Debug.Log("ÉèÖüõËÙ+++++++++++++++++"); // Debug.Log("ÉèÖüõËÙ+++++++++++++++++");
// 在延迟结束后恢复原速度 // 在延迟结束后恢复原速度
pathTween.timeScale = 1f; // 恢复原速度 pathTween.timeScale = 1f; // 恢复原速度
}); });