diff --git a/Role/Attack.cs b/Role/Attack.cs index e09d2a0..1bc5b8c 100644 --- a/Role/Attack.cs +++ b/Role/Attack.cs @@ -32,6 +32,7 @@ public class Attack : MonoBehaviour [Header("子弹速度加成")]public float roleBulletSpeedAdd=0f; [Header("子弹数量")] public int BulletNumber = 1; [Header("攻击碰撞体")] public CircleCollider2D attackCollider; + [Header("矩形攻击碰撞体")] public BoxCollider2D attackColliderBox; [Header("攻击范围图片")] public SpriteRenderer attackRangeSprite; //public float AttackScope //{ @@ -62,53 +63,45 @@ public class Attack : MonoBehaviour while (true) { - if (attackCollider) + if (attackCollider|| attackColliderBox) { // 检查是否可以进行攻击(基于冷却时间) if (Time.time - lastAttackTime >= attackCooldown) { - Collider2D[] colliders = Physics2D.OverlapCircleAll(attackCollider.transform.position, attackCollider.radius); - - foreach (Collider2D collider in colliders) + + if (attackCollider) { - Role targetRole = collider.GetComponent(); - if (targetRole && targetRole.camp != role.camp) - { - role.animationHighlight = 1; - if (animator!=null) - { - if (bulletPrefab.GetComponent().myBulletType != BulletType.Spraying) - { - direction = (targetRole.transform.position - BulletStartPos.position).normalized; - } - //animator.SetInteger("State", 1); - animator.SetTrigger("Attack"); - lastAttackTime = Time.time; // 更新上次攻击时间 - - } - else - { - /*attack(targetRole); - lastAttackTime = Time.time; // 更新上次攻击时间*/ - } - - break; // 只攻击一个目标 - } - else - { - - if (animator != null) - { - //animator.SetInteger("State", 0); - } - else - { - role.animationHighlight = 0; - } - - } + Collider2D[] colliders = Physics2D.OverlapCircleAll(attackCollider.transform.position, attackCollider.radius); + GetAllColliderInAttackRange(colliders); } + else if (attackColliderBox)// 使用 OverlapBoxAll 来检测矩形区域内的所有碰撞体 + { + // 获取碰撞体的尺寸 + Vector2 boxSize = attackColliderBox.size; + + // 只检测右边一半,修改尺寸 + boxSize.x /= 2f; // 将宽度减半,只覆盖右边一半区域 + + // 获取碰撞体的中心位置 + Vector3 colliderCenter = attackColliderBox.transform.position; + + // 计算右半部分的中心位置,确保中心点在矩形的右半部分 + Vector3 rightCenter = new Vector3(colliderCenter.x + attackColliderBox.size.x / 4f, colliderCenter.y, colliderCenter.z); + + // 使用OverlapBoxAll检测右半部分区域 + Collider2D[] colliders = Physics2D.OverlapBoxAll( + rightCenter, // 右半部分的中心位置 + boxSize, // 新的尺寸,只覆盖右半部分 + 0f // 没有旋转 + ); + Debug.Log("进入范围"); + GetAllColliderInAttackRange(colliders); + } + + + + } await Task.Delay(100); // 延迟,避免过于频繁地检测 @@ -120,6 +113,48 @@ public class Attack : MonoBehaviour } } + public void GetAllColliderInAttackRange(Collider2D[] colliders)//遍历所有在攻击范围内的敌人 + { + foreach (Collider2D collider in colliders) + { + Role targetRole = collider.GetComponent(); + if (targetRole && targetRole.camp != role.camp) + { + role.animationHighlight = 1; + if (animator != null) + { + if (bulletPrefab.GetComponent().myBulletType != BulletType.Spraying) + { + direction = (targetRole.transform.position - BulletStartPos.position).normalized; + } + //animator.SetInteger("State", 1); + animator.SetTrigger("Attack"); + lastAttackTime = Time.time; // 更新上次攻击时间 + + } + else + { + /*attack(targetRole); + lastAttackTime = Time.time; // 更新上次攻击时间*/ + } + + break; // 只攻击一个目标 + } + else + { + + if (animator != null) + { + //animator.SetInteger("State", 0); + } + else + { + role.animationHighlight = 0; + } + + } + } + } /*public void attack(Role targetRole) { @@ -220,11 +255,16 @@ public class Attack : MonoBehaviour } -private void GenerateBullet(float angle, Vector2 startPos, Vector2 changePos)//角度,旋转中心点 +private void GenerateBullet(float angle, Vector2 startPos, Vector2 changePos,float scaleFactor=1)//角度,旋转中心点 { // 实例化子弹对象并设置父物体为 transform.root GameObject bulletGameObj = GameObject.Instantiate(bulletPrefab, transform.root); + // 获取子弹的原始宽度 + float originalWidth = bulletGameObj.GetComponentInChildren().bounds.size.x; + // 设置子弹的缩放 + + bulletGameObj.transform.localScale = new Vector2(scaleFactor, 1); // 获取动画组件(如果需要的话) fireAnis.Add(bulletGameObj.GetComponentInChildren()); @@ -247,6 +287,14 @@ private void GenerateBullet(float angle, Vector2 startPos, Vector2 changePos)// // 设置子弹的旋转角度 bulletGameObj.transform.rotation = Quaternion.Euler(0, 0, angle); bulletGameObj.transform.position = bulletGameObj.transform.position + (Vector3)changePos; + + + // 计算缩放后的偏移量 + float offsetX = (originalWidth * (scaleFactor - 1)) / 2f; + + // 调整子弹的位置,确保左边界不变 + bulletGameObj.transform.position = new Vector2(bulletGameObj.transform.position.x + offsetX, bulletGameObj.transform.position.y); + // 将生成的子弹添加到子弹列表中 bulltes.Add(bulletGameObj); @@ -272,18 +320,40 @@ private void GenerateBullet(float angle, Vector2 startPos, Vector2 changePos)// public void SetAttackRange() { - // 设置碰撞体的半径 - attackCollider.radius = role.AttackRange; + if (attackCollider) + { + // 设置碰撞体的半径 + attackCollider.radius = role.AttackRange; - // 获取攻击范围图片的原始直径 (SpriteRenderer的bounds.size.x) - float spriteDiameter = attackRangeSprite.bounds.size.x; + // 获取攻击范围图片的原始直径 (SpriteRenderer的bounds.size.x) + float spriteDiameter = attackRangeSprite.bounds.size.x; - // 计算缩放因子,使得图片的直径与碰撞体的直径匹配 - float scaleFactor = role.AttackRange * 2f / spriteDiameter; // * 2f 因为 radius 是半径,图片的尺寸通常是直径 + // 计算缩放因子,使得图片的直径与碰撞体的直径匹配 + float scaleFactor = role.AttackRange * 2f / spriteDiameter; // * 2f 因为 radius 是半径,图片的尺寸通常是直径 - // 设置图片的缩放 - attackRangeSprite.transform.localScale = new Vector3(scaleFactor, scaleFactor, 1f); + // 设置图片的缩放 + attackRangeSprite.transform.localScale = new Vector3(scaleFactor, scaleFactor, 1f); + } + else if(attackColliderBox) + { + Vector2 boxSize = attackColliderBox.size; + + // 假设攻击范围决定了矩形的长宽比 + // attackRange * 2 可以增加攻击范围的矩形尺寸,调整比例或常数以适应你的需要 + boxSize.x = role.AttackRange * 2f; // 设置矩形宽度 + //boxSize.y = role.AttackRange/3; // 设置矩形高度 + + // 更新矩形碰撞体的尺寸 + attackColliderBox.size = boxSize; + + // 调整攻击范围图片的缩放,使其与碰撞体的大小一致 + float scaleX = boxSize.x / attackRangeSprite.bounds.size.x*3*0.75f; + float scaleY = boxSize.y / attackRangeSprite.bounds.size.y*2; + + attackRangeSprite.transform.localScale = new Vector3(scaleX, scaleY, 1f); + } + }