This commit is contained in:
GL 2024-12-27 21:00:24 +08:00
parent ec0eb5a298
commit 0d58228599

View File

@ -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;
// 确保 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);
}