攻击范围
This commit is contained in:
parent
45c00a5584
commit
722bec67c7
@ -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,21 +63,65 @@ 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);
|
||||
|
||||
if (attackCollider)
|
||||
{
|
||||
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); // 延迟,避免过于频繁地检测
|
||||
}
|
||||
else
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void GetAllColliderInAttackRange(Collider2D[] colliders)//遍历所有在攻击范围内的敌人
|
||||
{
|
||||
foreach (Collider2D collider in colliders)
|
||||
{
|
||||
Role targetRole = collider.GetComponent<Role>();
|
||||
if (targetRole && targetRole.camp != role.camp)
|
||||
{
|
||||
role.animationHighlight = 1;
|
||||
if (animator!=null)
|
||||
if (animator != null)
|
||||
{
|
||||
if (bulletPrefab.GetComponent<Bullet>().myBulletType != BulletType.Spraying)
|
||||
{
|
||||
@ -109,17 +154,7 @@ public class Attack : MonoBehaviour
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
await Task.Delay(100); // 延迟,避免过于频繁地检测
|
||||
}
|
||||
else
|
||||
{
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*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<SpriteRenderer>().bounds.size.x;
|
||||
|
||||
// 设置子弹的缩放
|
||||
|
||||
bulletGameObj.transform.localScale = new Vector2(scaleFactor, 1);
|
||||
// 获取动画组件(如果需要的话)
|
||||
fireAnis.Add(bulletGameObj.GetComponentInChildren<Animator>());
|
||||
|
||||
@ -248,6 +288,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);
|
||||
}
|
||||
@ -271,6 +319,8 @@ private void GenerateBullet(float angle, Vector2 startPos, Vector2 changePos)//
|
||||
}
|
||||
|
||||
public void SetAttackRange()
|
||||
{
|
||||
if (attackCollider)
|
||||
{
|
||||
// 设置碰撞体的半径
|
||||
attackCollider.radius = role.AttackRange;
|
||||
@ -284,6 +334,26 @@ private void GenerateBullet(float angle, Vector2 startPos, Vector2 changePos)//
|
||||
|
||||
// 设置图片的缩放
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user