From bf88fb96a3125ef91388146665eeab8f80aa2f25 Mon Sep 17 00:00:00 2001 From: GL <2365963573@qq.com> Date: Sun, 29 Dec 2024 11:26:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=BB=E5=87=BB=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Role/Attack.cs | 67 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/Role/Attack.cs b/Role/Attack.cs index bdd90df..dda676b 100644 --- a/Role/Attack.cs +++ b/Role/Attack.cs @@ -277,37 +277,56 @@ public class Attack : MonoBehaviour } } - -private void GenerateBullet(float angle, Vector2 startPos, Vector2 changePos)//角度,旋转中心点 + + private void GenerateBullet(float angle, Vector2 startPos, Vector2 changePos) { // 实例化子弹对象并设置父物体为 transform.root GameObject bulletGameObj = GameObject.Instantiate(bulletPrefab, transform.root); + // 获取子弹的原始宽度 float originalWidth = bulletGameObj.GetComponentInChildren().bounds.size.x; - + + // 保存原始位置,以便在攻击范围变化时恢复 + Vector2 originalPosition = bulletGameObj.transform.position; + // 设置子弹的缩放 - - if (role.AttackRange / 2>1) + if (role.AttackRange / 2 > 1) { + float offsetX = 0; bulletGameObj.transform.localScale = new Vector2(role.AttackRange / 2, role.AttackRange / 3); - // 计算缩放后的偏移量 - float offsetX = (originalWidth * (role.AttackRange / 2)) / 4f+0.15f; + // 计算新的子弹宽度 + float newWidth = bulletGameObj.GetComponentInChildren().bounds.size.x; + + //// 根据宽度变化计算偏移量 + //if (newWidth > originalWidth) + //{ + // // 如果新宽度大于原始宽度,计算向右的偏移 + // offsetX = (originalWidth * (role.AttackRange / 2)) / 4f + 0.15f; + //} + //else if (newWidth < originalWidth) + //{ + // // 如果新宽度小于原始宽度,计算向左的偏移 + // offsetX = -((originalWidth * (role.AttackRange / 2)) / 4f +0.15f); + //} + offsetX = (newWidth - originalWidth) / 2; + if (offsetX>0) + { + offsetX += 0.15f; + } // 调整子弹的位置,确保左边界不变 - bulletGameObj.transform.position = new Vector2(bulletGameObj.transform.position.x + offsetX, bulletGameObj.transform.position.y); - - + bulletGameObj.transform.position = new Vector2(originalPosition.x + offsetX, originalPosition.y); } + + // 添加位置微调 bulletGameObj.transform.position = new Vector2(bulletGameObj.transform.position.x + 1.75f, bulletGameObj.transform.position.y + 0.2f); + // 获取动画组件(如果需要的话) fireAnis.Add(bulletGameObj.GetComponentInChildren()); - // 设置子弹的初始位置,保持偏移量 - - + // 设置子弹的初始属性 bulletGameObj.GetComponent().role = role; bulletGameObj.GetComponent().attackObj = this; - // 计算子弹相对于旋转中心的偏移 Vector2 direction = bulletGameObj.transform.position - (Vector3)startPos; @@ -320,17 +339,16 @@ 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; - - - + // 根据变化调整位置(例如“changePos”可能需要与原始位置结合使用) + bulletGameObj.transform.position = bulletGameObj.transform.position + (Vector3)changePos; // 将生成的子弹添加到子弹列表中 bulltes.Add(bulletGameObj); } + public void EndFire() { foreach (Animator ani in fireAnis) @@ -371,7 +389,7 @@ private void GenerateBullet(float angle, Vector2 startPos, Vector2 changePos)// // 假设攻击范围决定了矩形的长宽比 // attackRange * 2 可以增加攻击范围的矩形尺寸,调整比例或常数以适应你的需要 - boxSize.x = role.AttackRange * 2f+2; // 设置矩形宽度 + boxSize.x = role.AttackRange * 2f + 2; // 设置矩形宽度 //boxSize.y = role.AttackRange/3; // 设置矩形高度 // 更新矩形碰撞体的尺寸 @@ -382,11 +400,18 @@ private void GenerateBullet(float angle, Vector2 startPos, Vector2 changePos)// //float originalWidth = attackRangeSprite.bounds.size.x; //attackRangeSprite.transform.localScale = new Vector3(boxSize.x*1.4f/4, boxSize.y, 1f); characterClick.OrSizeX = boxSize.x / characterClick.StartSizeX; // * 2f 因为 radius 是半径,图片的尺寸通常是直径 - characterClick.OrSizeY = boxSize.y*2 / characterClick.StartSizeY; - attackRangeSprite.transform.position = new Vector2(attackRangeSprite.transform.position.x - (characterClick.StartSizeX - characterClick.OrSizeX) / 2, attackRangeSprite.transform.position.y); + characterClick.OrSizeY = boxSize.y * 2 / characterClick.StartSizeY; + float offSetX = (characterClick.StartSizeX - characterClick.OrSizeX) / 2; + if (role.AttackRange==3) + { + offSetX += 0.6f; + } + attackRangeSprite.transform.position = new Vector2(attackRangeSprite.transform.position.x - offSetX, attackRangeSprite.transform.position.y); //characterClick.OrSizeY = 1; Debug.Log("修改图片宽度"); + + }