攻击范围

This commit is contained in:
GL 2024-12-26 18:11:09 +08:00
parent cd7f839004
commit 45c00a5584
2 changed files with 46 additions and 30 deletions

View File

@ -12,7 +12,7 @@ public class Attack : MonoBehaviour
[Header("子弹预制体")] public GameObject bulletPrefab;
[Header("子弹数据")] public BulletData bulletData;
[Header("角色对象")] public Role role;
[Header("攻击范围")] public float attackScope;
//[Header("攻击范围")] public float attackScope;
[Header("攻击类型")] public DamageType damageTyp = DamageType.noAttributeDamage;
[Header("攻击CD时间")] public float attackCooldown = 1f; // 攻击冷却时间
@ -31,41 +31,44 @@ public class Attack : MonoBehaviour
[Header("子弹速度加成")]public float roleBulletSpeedAdd=0f;
[Header("子弹数量")] public int BulletNumber = 1;
public float AttackScope
{
[DebuggerStepThrough]
get => attackScope;
[DebuggerStepThrough]
set
{
attackScope = value;
GetComponent<CircleCollider2D>().radius = attackScope;
//Debug.Log("攻击半径"+GetComponent<CircleCollider2D>().radius);
}
}
[Header("攻击碰撞体")] public CircleCollider2D attackCollider;
[Header("攻击范围图片")] public SpriteRenderer attackRangeSprite;
//public float AttackScope
//{
// [DebuggerStepThrough]
// get => attackScope;
// [DebuggerStepThrough]
// set
// {
// attackScope = value;
// GetComponent<CircleCollider2D>().radius = attackScope;
// //Debug.Log("攻击半径"+GetComponent<CircleCollider2D>().radius);
// }
//}
public async void Start()
{
AttackScope = attackScope;
//AttackScope = attackScope;
bulletData = new BulletData();
bulletData.BulletScattering = 1;
animator.SetFloat("AttackSpeed",AttackSpeed);
CircleCollider2D circleCollider2D = GetComponent<CircleCollider2D>();
//CircleCollider2D circleCollider2D = GetComponent<CircleCollider2D>();
attackCooldown = transform.parent.GetComponent<enemy>().AttackCD;
SetAttackRange();//设置攻击范围
while (true)
{
if (circleCollider2D)
{
if (attackCollider)
{
// 检查是否可以进行攻击(基于冷却时间)
if (Time.time - lastAttackTime >= attackCooldown)
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(circleCollider2D.transform.position, attackScope);
Collider2D[] colliders = Physics2D.OverlapCircleAll(attackCollider.transform.position, attackCollider.radius);
foreach (Collider2D collider in colliders)
{
@ -226,15 +229,11 @@ private void GenerateBullet(float angle, Vector2 startPos, Vector2 changePos)//
fireAnis.Add(bulletGameObj.GetComponentInChildren<Animator>());
// 设置子弹的初始位置,保持偏移量
bulletGameObj.transform.position = new Vector2(transform.position.x + 0.15f, transform.position.y + 0.2f);
bulletGameObj.transform.position = new Vector2(transform.position.x+1.75f, transform.position.y + 0.2f);
// 设置子弹的角色和攻击者
Bullet bulletComponent = bulletGameObj.GetComponent<Bullet>();
if (bulletComponent != null)
{
bulletComponent.role = role;
bulletComponent.attackObj = this;
}
bulletGameObj.GetComponent<Bullet>().role = role;
bulletGameObj.GetComponent<Bullet>().attackObj = this;
// 计算子弹相对于旋转中心的偏移
Vector2 direction = bulletGameObj.transform.position - (Vector3)startPos;
@ -271,6 +270,21 @@ private void GenerateBullet(float angle, Vector2 startPos, Vector2 changePos)//
bulltes.Clear();
}
public void SetAttackRange()
{
// 设置碰撞体的半径
attackCollider.radius = role.AttackRange;
// 获取攻击范围图片的原始直径 (SpriteRenderer的bounds.size.x)
float spriteDiameter = attackRangeSprite.bounds.size.x;
// 计算缩放因子,使得图片的直径与碰撞体的直径匹配
float scaleFactor = role.AttackRange * 2f / spriteDiameter; // * 2f 因为 radius 是半径,图片的尺寸通常是直径
// 设置图片的缩放
attackRangeSprite.transform.localScale = new Vector3(scaleFactor, scaleFactor, 1f);
}
}

View File

@ -71,6 +71,7 @@ public class Role : Fun
public string AttackActionType;//攻击作用类型
public float AttackRange;//攻击范围
public float AttackCD;//攻击CD
public float CritRate;//暴击率
public float CriticalHitRateBonus;//暴击加成
@ -191,7 +192,7 @@ public class Role : Fun
maxHp = hp;
SetSelfInfo();
//SetAttackRange();//ÉèÖù¥»÷·¶Î§
if (Application.isPlaying)
{
if (attackClass)
@ -202,6 +203,7 @@ public class Role : Fun
}
tag = Enum.GetName(typeof(Camp), camp);
updateAnimation();
}
@ -542,6 +544,6 @@ public class Role : Fun
}
}