From 0d58228599c57ee91f4a2ac8924e53b2cd23dfe8 Mon Sep 17 00:00:00 2001 From: GL <2365963573@qq.com> Date: Fri, 27 Dec 2024 21:00:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Role/Attack.cs | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/Role/Attack.cs b/Role/Attack.cs index e09b681..c569d13 100644 --- a/Role/Attack.cs +++ b/Role/Attack.cs @@ -366,21 +366,43 @@ private void GenerateBullet(float angle, Vector2 startPos, Vector2 changePos)// } else if(attackColliderBox) { + if (attackColliderBox == null || attackRangeSprite == null) + { + Debug.LogError("Attack Collider or Attack Range Sprite is missing!"); + return; // 如果没有找到攻击碰撞体或攻击范围图片,提前返回 + } + Vector2 boxSize = attackColliderBox.size; // 假设攻击范围决定了矩形的长宽比 - // attackRange * 2 可以增加攻击范围的矩形尺寸,调整比例或常数以适应你的需要 boxSize.x = role.AttackRange * 2f; // 设置矩形宽度 - //boxSize.y = role.AttackRange/3; // 设置矩形高度 + //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.bounds.size 不为零 + if (attackRangeSprite.bounds.size.x == 0 || attackRangeSprite.bounds.size.y == 0) + { + Debug.LogError("Attack Range Sprite bounds size is invalid."); + return; + } + // 调整攻击范围图片的缩放,使其与碰撞体的大小一致 + float scaleX = boxSize.x / attackRangeSprite.bounds.size.x * 3 * 0.75f; + float scaleY = boxSize.y / attackRangeSprite.bounds.size.y * 2; + + // 确保计算结果不是无效值(如 Infinity 或 NaN) + scaleX = Mathf.Clamp(scaleX, 0.1f, 10f); // 设置合理的范围,防止缩放过大或过小 + scaleY = Mathf.Clamp(scaleY, 0.1f, 10f); + + // 保留两位小数 + scaleX = Mathf.Round(scaleX * 100f) / 100f; + scaleY = Mathf.Round(scaleY * 100f) / 100f; + + // 设置图片的缩放 attackRangeSprite.transform.localScale = new Vector3(scaleX, scaleY, 1f); + }