diff --git a/Role/Attack.cs b/Role/Attack.cs index ddf0528..e09d2a0 100644 --- a/Role/Attack.cs +++ b/Role/Attack.cs @@ -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().radius = attackScope; - //Debug.Log("攻击半径"+GetComponent().radius); - } - } + [Header("攻击碰撞体")] public CircleCollider2D attackCollider; + [Header("攻击范围图片")] public SpriteRenderer attackRangeSprite; + //public float AttackScope + //{ + // [DebuggerStepThrough] + // get => attackScope; + // [DebuggerStepThrough] + // set + // { + // attackScope = value; + // GetComponent().radius = attackScope; + // //Debug.Log("攻击半径"+GetComponent().radius); + // } + //} public async void Start() { - AttackScope = attackScope; + //AttackScope = attackScope; bulletData = new BulletData(); bulletData.BulletScattering = 1; animator.SetFloat("AttackSpeed",AttackSpeed); - CircleCollider2D circleCollider2D = GetComponent(); + //CircleCollider2D circleCollider2D = GetComponent(); attackCooldown = transform.parent.GetComponent().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()); // 设置子弹的初始位置,保持偏移量 - 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(); - if (bulletComponent != null) - { - bulletComponent.role = role; - bulletComponent.attackObj = this; - } + bulletGameObj.GetComponent().role = role; + bulletGameObj.GetComponent().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); + } + } diff --git a/Role/Role.cs b/Role/Role.cs index 42c9793..ae9e20e 100644 --- a/Role/Role.cs +++ b/Role/Role.cs @@ -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 } - + }