2024-12-05 16:25:46 +08:00
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
2024-12-14 23:20:36 +08:00
|
|
|
|
using System.Linq;
|
2024-12-05 16:25:46 +08:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using Debug = UnityEngine.Debug;
|
2024-12-27 16:49:55 +08:00
|
|
|
|
using DG.Tweening;
|
2024-12-05 16:25:46 +08:00
|
|
|
|
|
|
|
|
|
public class Attack : MonoBehaviour
|
|
|
|
|
{
|
2024-12-12 23:03:57 +08:00
|
|
|
|
[Header("子弹预制体")] public GameObject bulletPrefab;
|
|
|
|
|
[Header("子弹数据")] public BulletData bulletData;
|
|
|
|
|
[Header("角色对象")] public Role role;
|
2024-12-26 18:11:09 +08:00
|
|
|
|
//[Header("攻击范围")] public float attackScope;
|
2024-12-12 23:03:57 +08:00
|
|
|
|
[Header("攻击类型")] public DamageType damageTyp = DamageType.noAttributeDamage;
|
|
|
|
|
|
2024-12-18 14:16:52 +08:00
|
|
|
|
[Header("攻击CD时间")] public float attackCooldown = 1f; // 攻击冷却时间
|
2024-12-12 23:03:57 +08:00
|
|
|
|
private float lastAttackTime = 0f; // 上次攻击的时间
|
2024-12-30 17:51:13 +08:00
|
|
|
|
|
2024-12-12 23:03:57 +08:00
|
|
|
|
[HideInInspector]
|
|
|
|
|
public List<GameObject> bulltes = new List<GameObject>();
|
|
|
|
|
|
2024-12-17 17:15:32 +08:00
|
|
|
|
[Header("角色动画控制器")] public Animator animator;
|
2024-12-26 11:19:45 +08:00
|
|
|
|
//[Header("火焰动画控制器")] public Animator fireAni;
|
|
|
|
|
[Header("火焰动画控制器")] public List<Animator> fireAnis;
|
2024-12-17 18:07:43 +08:00
|
|
|
|
|
|
|
|
|
[Header("子弹起始点")] public Transform BulletStartPos;
|
2024-12-30 17:51:13 +08:00
|
|
|
|
[Header("攻击速度")] public float AttackSpeed = 1f;
|
2024-12-17 18:07:43 +08:00
|
|
|
|
public Vector2 direction;
|
2024-12-25 23:08:05 +08:00
|
|
|
|
|
2024-12-30 17:51:13 +08:00
|
|
|
|
[Header("子弹速度加成")] public float roleBulletSpeedAdd = 0f;
|
2024-12-27 11:38:49 +08:00
|
|
|
|
//[Header("子弹长度")] public float scaleFactor;
|
2024-12-25 23:08:05 +08:00
|
|
|
|
[Header("子弹数量")] public int BulletNumber = 1;
|
2024-12-26 18:11:09 +08:00
|
|
|
|
[Header("攻击碰撞体")] public CircleCollider2D attackCollider;
|
2024-12-26 23:00:30 +08:00
|
|
|
|
[Header("矩形攻击碰撞体")] public BoxCollider2D attackColliderBox;
|
2024-12-26 18:11:09 +08:00
|
|
|
|
[Header("攻击范围图片")] public SpriteRenderer attackRangeSprite;
|
2024-12-27 16:49:55 +08:00
|
|
|
|
[Header("攻击持续时间")] public float AttackStayTime;//
|
2024-12-27 18:13:16 +08:00
|
|
|
|
[Header("攻击目标")] public GameObject Target;
|
2024-12-28 14:30:38 +08:00
|
|
|
|
[Header("攻击范围显示脚本")] public CharacterClick characterClick;
|
2025-01-03 18:02:54 +08:00
|
|
|
|
[Header("攻击随机角度范围")] public float Angle=30;
|
|
|
|
|
[Header("子弹分裂个数")] public int splitNum = 2;
|
2025-01-03 20:50:25 +08:00
|
|
|
|
[Header("分裂子弹伤害")] public float SplitAttack = 10;
|
2025-01-04 18:09:38 +08:00
|
|
|
|
[Header("子弹围绕范围")] public float RunRange = 3f;
|
2025-01-04 20:08:40 +08:00
|
|
|
|
[Header("是否造成额外伤害")] public bool haveAddDamage = false;
|
|
|
|
|
[Header("额外伤害类型")] public DamageType AdddamageType=DamageType.magicDamage;
|
2025-01-06 16:15:06 +08:00
|
|
|
|
[Header("子弹爆炸范围")] public float BoomRange = 1;
|
2024-12-27 16:49:55 +08:00
|
|
|
|
public bool isAttack = true;
|
|
|
|
|
public bool flag = false;
|
2024-12-30 17:51:13 +08:00
|
|
|
|
[HideInInspector] public float timer = 0;
|
|
|
|
|
|
2024-12-12 23:03:57 +08:00
|
|
|
|
|
2024-12-05 16:25:46 +08:00
|
|
|
|
public async void Start()
|
|
|
|
|
{
|
|
|
|
|
bulletData = new BulletData();
|
|
|
|
|
bulletData.BulletScattering = 1;
|
2024-12-11 16:51:55 +08:00
|
|
|
|
|
2025-01-04 18:09:38 +08:00
|
|
|
|
//animator.SetFloat("AttackSpeed", AttackSpeed);
|
2024-12-27 16:49:55 +08:00
|
|
|
|
|
2024-12-23 16:57:40 +08:00
|
|
|
|
attackCooldown = transform.parent.GetComponent<enemy>().AttackCD;
|
2024-12-23 14:28:04 +08:00
|
|
|
|
|
2024-12-27 16:49:55 +08:00
|
|
|
|
SetAttackRange(); // 设置攻击范围
|
2024-12-23 14:28:04 +08:00
|
|
|
|
|
2024-12-27 16:49:55 +08:00
|
|
|
|
while (isAttack)
|
2024-12-12 23:03:57 +08:00
|
|
|
|
{
|
2024-12-27 16:49:55 +08:00
|
|
|
|
if (attackCollider || attackColliderBox)
|
|
|
|
|
{
|
2024-12-12 23:03:57 +08:00
|
|
|
|
// 检查是否可以进行攻击(基于冷却时间)
|
|
|
|
|
if (Time.time - lastAttackTime >= attackCooldown)
|
2024-12-05 16:25:46 +08:00
|
|
|
|
{
|
2024-12-27 16:49:55 +08:00
|
|
|
|
|
|
|
|
|
|
2024-12-30 17:51:13 +08:00
|
|
|
|
|
2024-12-26 23:00:30 +08:00
|
|
|
|
if (attackCollider)
|
|
|
|
|
{
|
|
|
|
|
Collider2D[] colliders = Physics2D.OverlapCircleAll(attackCollider.transform.position, attackCollider.radius);
|
|
|
|
|
GetAllColliderInAttackRange(colliders);
|
|
|
|
|
}
|
2024-12-27 16:49:55 +08:00
|
|
|
|
else if (attackColliderBox)
|
2024-12-05 16:25:46 +08:00
|
|
|
|
{
|
2024-12-26 23:00:30 +08:00
|
|
|
|
// 获取碰撞体的尺寸
|
|
|
|
|
Vector2 boxSize = attackColliderBox.size;
|
|
|
|
|
|
|
|
|
|
// 只检测右边一半,修改尺寸
|
2024-12-27 16:49:55 +08:00
|
|
|
|
boxSize.x /= 2f;
|
2024-12-26 23:00:30 +08:00
|
|
|
|
|
|
|
|
|
// 获取碰撞体的中心位置
|
|
|
|
|
Vector3 colliderCenter = attackColliderBox.transform.position;
|
|
|
|
|
|
|
|
|
|
// 计算右半部分的中心位置,确保中心点在矩形的右半部分
|
|
|
|
|
Vector3 rightCenter = new Vector3(colliderCenter.x + attackColliderBox.size.x / 4f, colliderCenter.y, colliderCenter.z);
|
|
|
|
|
|
|
|
|
|
// 使用OverlapBoxAll检测右半部分区域
|
2024-12-27 16:49:55 +08:00
|
|
|
|
Collider2D[] colliders = Physics2D.OverlapBoxAll(rightCenter, boxSize, 0f);
|
2024-12-26 23:00:30 +08:00
|
|
|
|
GetAllColliderInAttackRange(colliders);
|
2024-12-05 16:25:46 +08:00
|
|
|
|
}
|
2024-12-26 23:00:30 +08:00
|
|
|
|
|
2024-12-27 16:49:55 +08:00
|
|
|
|
lastAttackTime = Time.time; // 更新上次攻击时间
|
2024-12-05 16:25:46 +08:00
|
|
|
|
}
|
2024-12-27 16:49:55 +08:00
|
|
|
|
|
2024-12-30 17:51:13 +08:00
|
|
|
|
|
2024-12-27 16:49:55 +08:00
|
|
|
|
|
2024-12-12 23:03:57 +08:00
|
|
|
|
await Task.Delay(100); // 延迟,避免过于频繁地检测
|
2024-12-05 16:25:46 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-12 23:03:57 +08:00
|
|
|
|
|
2024-12-27 16:49:55 +08:00
|
|
|
|
|
|
|
|
|
private void Update()
|
|
|
|
|
{
|
|
|
|
|
if (flag)
|
|
|
|
|
{
|
|
|
|
|
timer += Time.deltaTime;
|
2024-12-30 17:51:13 +08:00
|
|
|
|
if (timer > AttackStayTime)
|
2024-12-27 16:49:55 +08:00
|
|
|
|
{
|
|
|
|
|
animator.SetInteger("State", 0);
|
|
|
|
|
isAttack = true;
|
|
|
|
|
timer = 0;
|
|
|
|
|
flag = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-30 17:51:13 +08:00
|
|
|
|
|
|
|
|
|
//Debug.Log(role.name+"攻击范围的图片大小"+attackRangeSprite.transform.localScale + "color" + attackRangeSprite.color+ "color.a" + attackRangeSprite.color.a + "是否显示"+ attackRangeSprite.enabled);
|
|
|
|
|
|
2024-12-27 16:49:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void GetAllColliderInAttackRange(Collider2D[] colliders)
|
2024-12-26 23:00:30 +08:00
|
|
|
|
{
|
|
|
|
|
foreach (Collider2D collider in colliders)
|
|
|
|
|
{
|
|
|
|
|
Role targetRole = collider.GetComponent<Role>();
|
2024-12-30 17:51:13 +08:00
|
|
|
|
|
2024-12-26 23:00:30 +08:00
|
|
|
|
if (targetRole && targetRole.camp != role.camp)
|
|
|
|
|
{
|
|
|
|
|
role.animationHighlight = 1;
|
|
|
|
|
if (animator != null)
|
|
|
|
|
{
|
|
|
|
|
if (bulletPrefab.GetComponent<Bullet>().myBulletType != BulletType.Spraying)
|
|
|
|
|
{
|
|
|
|
|
direction = (targetRole.transform.position - BulletStartPos.position).normalized;
|
2024-12-27 18:13:16 +08:00
|
|
|
|
Target = targetRole.gameObject;
|
2024-12-27 16:49:55 +08:00
|
|
|
|
animator.SetTrigger("Attack");
|
2024-12-26 23:00:30 +08:00
|
|
|
|
}
|
2025-01-06 14:03:59 +08:00
|
|
|
|
else if (bulletPrefab.GetComponent<Bullet>().myBulletType == BulletType.Spraying|| bulletPrefab.GetComponent<Bullet>().myBulletType==BulletType.XuLi)
|
2024-12-27 16:49:55 +08:00
|
|
|
|
{
|
|
|
|
|
animator.SetInteger("State", 1);
|
|
|
|
|
flag = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lastAttackTime = Time.time; // 更新上次攻击时间
|
2024-12-26 23:00:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
break; // 只攻击一个目标
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (animator != null)
|
|
|
|
|
{
|
|
|
|
|
//animator.SetInteger("State", 0);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
role.animationHighlight = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-17 18:07:43 +08:00
|
|
|
|
|
|
|
|
|
public void Pointattack()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (bulletPrefab == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("子弹预制体为空");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-12-30 17:51:13 +08:00
|
|
|
|
|
|
|
|
|
|
2024-12-25 23:08:05 +08:00
|
|
|
|
for (int i = 0; i < BulletNumber; i++)
|
|
|
|
|
{
|
2024-12-30 17:51:13 +08:00
|
|
|
|
// 改变子弹方向,根据角色自身的攻击范围来计算
|
2024-12-25 23:08:05 +08:00
|
|
|
|
GameObject BulletGamobj = GameObject.Instantiate(bulletPrefab, this.transform.root);
|
|
|
|
|
BulletGamobj.GetComponent<Bullet>().role = role;
|
|
|
|
|
BulletGamobj.GetComponent<Bullet>().attackObj = this;
|
|
|
|
|
BulletGamobj.GetComponent<Bullet>().bulletData.BulletSpeed *= (1 + roleBulletSpeedAdd);
|
2024-12-27 18:13:16 +08:00
|
|
|
|
BulletGamobj.GetComponent<Bullet>().Target = Target;
|
2024-12-25 23:08:05 +08:00
|
|
|
|
BulletGamobj.transform.up = direction;
|
|
|
|
|
BulletGamobj.transform.position = BulletStartPos.position;
|
|
|
|
|
bulltes.Add(BulletGamobj);
|
|
|
|
|
}
|
2024-12-17 18:07:43 +08:00
|
|
|
|
}
|
2024-12-12 23:03:57 +08:00
|
|
|
|
|
2024-12-17 17:15:32 +08:00
|
|
|
|
|
|
|
|
|
public void Fireattack()
|
|
|
|
|
{
|
|
|
|
|
if (bulletPrefab == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("子弹预制体为空");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-26 11:19:45 +08:00
|
|
|
|
// 检查子弹类型是否为 Spraying
|
2024-12-17 17:15:32 +08:00
|
|
|
|
if (bulletPrefab.GetComponent<Bullet>().myBulletType == BulletType.Spraying)
|
|
|
|
|
{
|
2024-12-26 11:19:45 +08:00
|
|
|
|
// 根据 BulletNumber 生成不同数量和旋转角度的子弹
|
|
|
|
|
switch (BulletNumber)
|
2024-12-25 23:08:05 +08:00
|
|
|
|
{
|
2024-12-26 11:19:45 +08:00
|
|
|
|
case 1:
|
|
|
|
|
// 生成一个不旋转的子弹
|
|
|
|
|
GenerateBullet(0f, BulletStartPos.position, new Vector2(0, 0));
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 3:
|
|
|
|
|
// 生成三个子弹:一个不旋转,一个向上旋转30度,一个向下旋转30度
|
2024-12-27 11:38:49 +08:00
|
|
|
|
GenerateBullet(-35f, BulletStartPos.position, new Vector2(0, -0.25f));
|
2024-12-26 11:19:45 +08:00
|
|
|
|
GenerateBullet(0f, BulletStartPos.position, new Vector2(0, 0));
|
2024-12-27 11:38:49 +08:00
|
|
|
|
GenerateBullet(35f, BulletStartPos.position, new Vector2(0, 0.25f));
|
2024-12-26 11:19:45 +08:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
// 可选:处理其他 BulletNumber 值的情况
|
|
|
|
|
Debug.LogWarning($"BulletNumber 为 {BulletNumber} 时未定义的生成逻辑。");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-25 23:08:05 +08:00
|
|
|
|
|
2024-12-29 11:26:04 +08:00
|
|
|
|
|
|
|
|
|
private void GenerateBullet(float angle, Vector2 startPos, Vector2 changePos)
|
2024-12-26 11:19:45 +08:00
|
|
|
|
{
|
|
|
|
|
// 实例化子弹对象并设置父物体为 transform.root
|
|
|
|
|
GameObject bulletGameObj = GameObject.Instantiate(bulletPrefab, transform.root);
|
2024-12-29 11:26:04 +08:00
|
|
|
|
|
2024-12-26 23:00:30 +08:00
|
|
|
|
// 获取子弹的原始宽度
|
|
|
|
|
float originalWidth = bulletGameObj.GetComponentInChildren<SpriteRenderer>().bounds.size.x;
|
2024-12-27 11:38:49 +08:00
|
|
|
|
|
2024-12-29 11:26:04 +08:00
|
|
|
|
// 保存原始位置,以便在攻击范围变化时恢复
|
|
|
|
|
Vector2 originalPosition = bulletGameObj.transform.position;
|
|
|
|
|
|
|
|
|
|
// 设置子弹的缩放
|
|
|
|
|
if (role.AttackRange / 2 > 1)
|
2024-12-27 11:38:49 +08:00
|
|
|
|
{
|
2024-12-29 11:26:04 +08:00
|
|
|
|
float offsetX = 0;
|
2024-12-27 11:38:49 +08:00
|
|
|
|
bulletGameObj.transform.localScale = new Vector2(role.AttackRange / 2, role.AttackRange / 3);
|
|
|
|
|
|
2024-12-29 11:26:04 +08:00
|
|
|
|
// 计算新的子弹宽度
|
|
|
|
|
float newWidth = bulletGameObj.GetComponentInChildren<SpriteRenderer>().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;
|
2024-12-30 17:51:13 +08:00
|
|
|
|
if (offsetX > 0)
|
2024-12-29 11:26:04 +08:00
|
|
|
|
{
|
|
|
|
|
offsetX += 0.15f;
|
|
|
|
|
}
|
2024-12-27 11:38:49 +08:00
|
|
|
|
// 调整子弹的位置,确保左边界不变
|
2024-12-29 11:26:04 +08:00
|
|
|
|
bulletGameObj.transform.position = new Vector2(originalPosition.x + offsetX, originalPosition.y);
|
2024-12-27 11:38:49 +08:00
|
|
|
|
}
|
2024-12-29 11:26:04 +08:00
|
|
|
|
|
|
|
|
|
// 添加位置微调
|
2024-12-27 11:38:49 +08:00
|
|
|
|
bulletGameObj.transform.position = new Vector2(bulletGameObj.transform.position.x + 1.75f, bulletGameObj.transform.position.y + 0.2f);
|
2024-12-29 11:26:04 +08:00
|
|
|
|
|
2024-12-26 11:19:45 +08:00
|
|
|
|
// 获取动画组件(如果需要的话)
|
|
|
|
|
fireAnis.Add(bulletGameObj.GetComponentInChildren<Animator>());
|
2024-12-25 23:08:05 +08:00
|
|
|
|
|
2024-12-29 11:26:04 +08:00
|
|
|
|
// 设置子弹的初始属性
|
2024-12-26 18:11:09 +08:00
|
|
|
|
bulletGameObj.GetComponent<Bullet>().role = role;
|
|
|
|
|
bulletGameObj.GetComponent<Bullet>().attackObj = this;
|
2024-12-26 11:19:45 +08:00
|
|
|
|
|
|
|
|
|
// 计算子弹相对于旋转中心的偏移
|
|
|
|
|
Vector2 direction = bulletGameObj.transform.position - (Vector3)startPos;
|
|
|
|
|
|
|
|
|
|
// 旋转偏移
|
|
|
|
|
direction = Quaternion.Euler(0, 0, angle) * direction;
|
|
|
|
|
|
|
|
|
|
// 更新子弹的位置
|
|
|
|
|
bulletGameObj.transform.position = startPos + direction;
|
|
|
|
|
|
|
|
|
|
// 设置子弹的旋转角度
|
|
|
|
|
bulletGameObj.transform.rotation = Quaternion.Euler(0, 0, angle);
|
2024-12-26 23:00:30 +08:00
|
|
|
|
|
2024-12-29 11:26:04 +08:00
|
|
|
|
// 根据变化调整位置(例如“changePos”可能需要与原始位置结合使用)
|
|
|
|
|
bulletGameObj.transform.position = bulletGameObj.transform.position + (Vector3)changePos;
|
2024-12-26 11:19:45 +08:00
|
|
|
|
|
|
|
|
|
// 将生成的子弹添加到子弹列表中
|
|
|
|
|
bulltes.Add(bulletGameObj);
|
2024-12-17 17:15:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-30 17:51:13 +08:00
|
|
|
|
|
2024-12-29 11:26:04 +08:00
|
|
|
|
|
2024-12-17 17:15:32 +08:00
|
|
|
|
public void EndFire()
|
|
|
|
|
{
|
2024-12-26 11:19:45 +08:00
|
|
|
|
foreach (Animator ani in fireAnis)
|
|
|
|
|
{
|
|
|
|
|
ani.SetInteger("State", 1);
|
|
|
|
|
}
|
|
|
|
|
fireAnis.Clear();
|
2024-12-24 17:40:22 +08:00
|
|
|
|
//Debug.Log("停止火焰");
|
2024-12-17 17:15:32 +08:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-12-12 23:03:57 +08:00
|
|
|
|
private void OnDisable()
|
|
|
|
|
{
|
|
|
|
|
bulltes.Clear();
|
2024-12-05 16:25:46 +08:00
|
|
|
|
}
|
2024-12-23 14:28:04 +08:00
|
|
|
|
|
2024-12-26 18:11:09 +08:00
|
|
|
|
public void SetAttackRange()
|
|
|
|
|
{
|
2025-01-04 18:09:38 +08:00
|
|
|
|
if (attackCollider&& role.AttackRange !=0)
|
2024-12-26 23:00:30 +08:00
|
|
|
|
{
|
|
|
|
|
// 设置碰撞体的半径
|
2025-01-09 10:35:40 +08:00
|
|
|
|
attackCollider.radius = role.AttackRange+2f;
|
2024-12-28 14:30:38 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-12-26 23:00:30 +08:00
|
|
|
|
|
|
|
|
|
// 计算缩放因子,使得图片的直径与碰撞体的直径匹配
|
2025-01-09 10:35:40 +08:00
|
|
|
|
characterClick.OrSizeX = (role.AttackRange+2f) * 2f / characterClick.StartSizeX; // * 2f 因为 radius 是半径,图片的尺寸通常是直径
|
2024-12-28 16:22:54 +08:00
|
|
|
|
characterClick.OrSizeY = characterClick.OrSizeX;
|
|
|
|
|
|
|
|
|
|
|
2024-12-26 18:11:09 +08:00
|
|
|
|
|
2024-12-26 23:00:30 +08:00
|
|
|
|
}
|
|
|
|
|
else if(attackColliderBox)
|
|
|
|
|
{
|
|
|
|
|
Vector2 boxSize = attackColliderBox.size;
|
|
|
|
|
|
|
|
|
|
// 假设攻击范围决定了矩形的长宽比
|
2024-12-27 22:58:44 +08:00
|
|
|
|
// attackRange * 2 可以增加攻击范围的矩形尺寸,调整比例或常数以适应你的需要
|
2025-01-02 16:35:49 +08:00
|
|
|
|
boxSize.x = role.AttackRange* 2f + 2; // 设置矩形宽度
|
2024-12-27 22:58:44 +08:00
|
|
|
|
//boxSize.y = role.AttackRange/3; // 设置矩形高度
|
2024-12-26 18:11:09 +08:00
|
|
|
|
|
2024-12-26 23:00:30 +08:00
|
|
|
|
// 更新矩形碰撞体的尺寸
|
2024-12-30 17:51:13 +08:00
|
|
|
|
attackColliderBox.size = boxSize;
|
2024-12-28 16:22:54 +08:00
|
|
|
|
characterClick.OrSizeX = boxSize.x / characterClick.StartSizeX; // * 2f 因为 radius 是半径,图片的尺寸通常是直径
|
2024-12-29 11:26:04 +08:00
|
|
|
|
characterClick.OrSizeY = boxSize.y * 2 / characterClick.StartSizeY;
|
|
|
|
|
float offSetX = (characterClick.StartSizeX - characterClick.OrSizeX) / 2;
|
|
|
|
|
if (role.AttackRange==3)
|
|
|
|
|
{
|
|
|
|
|
offSetX += 0.6f;
|
|
|
|
|
}
|
2025-01-02 16:35:49 +08:00
|
|
|
|
|
2024-12-29 11:26:04 +08:00
|
|
|
|
attackRangeSprite.transform.position = new Vector2(attackRangeSprite.transform.position.x - offSetX, attackRangeSprite.transform.position.y);
|
2024-12-28 16:22:54 +08:00
|
|
|
|
//characterClick.OrSizeY = 1;
|
2024-12-27 22:58:44 +08:00
|
|
|
|
Debug.Log("修改图片宽度");
|
2024-12-26 23:00:30 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-12-23 14:28:04 +08:00
|
|
|
|
|
2024-12-26 18:11:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-01-03 18:02:54 +08:00
|
|
|
|
|
|
|
|
|
public void RandPointattack()//在一个角度随机发射子弹 仓鼠攻击方式
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (bulletPrefab == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("子弹预制体为空");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < BulletNumber; i++)
|
|
|
|
|
{
|
|
|
|
|
// 改变子弹方向, 根据角色自身的攻击范围来计算
|
|
|
|
|
GameObject BulletGamobj = GameObject.Instantiate(bulletPrefab, this.transform.root);
|
|
|
|
|
BulletGamobj.GetComponent<Bullet>().role = role;
|
|
|
|
|
BulletGamobj.GetComponent<Bullet>().attackObj = this;
|
|
|
|
|
BulletGamobj.GetComponent<Bullet>().bulletData.BulletSpeed *= (1 + roleBulletSpeedAdd);
|
|
|
|
|
BulletGamobj.GetComponent<Bullet>().Target = Target;
|
|
|
|
|
|
|
|
|
|
// 计算一个随机偏移角度,在-15度到+15度之间
|
|
|
|
|
float randomAngle = Random.Range(-Angle/2, Angle/2);
|
|
|
|
|
Debug.Log("土角度"+Angle);
|
|
|
|
|
// 将原始方向转换为角度
|
|
|
|
|
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
|
|
|
|
|
|
|
|
|
|
// 加上随机偏移角度
|
|
|
|
|
angle += randomAngle;
|
|
|
|
|
|
|
|
|
|
// 将角度转换回方向
|
|
|
|
|
direction = new Vector2(Mathf.Cos(angle * Mathf.Deg2Rad), Mathf.Sin(angle * Mathf.Deg2Rad));
|
|
|
|
|
|
|
|
|
|
// 设置子弹的方向
|
|
|
|
|
BulletGamobj.transform.up = direction;
|
|
|
|
|
|
|
|
|
|
// 设置子弹的起始位置
|
|
|
|
|
BulletGamobj.transform.position = BulletStartPos.position;
|
|
|
|
|
|
|
|
|
|
// 将子弹添加到列表中
|
|
|
|
|
bulltes.Add(BulletGamobj);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2025-01-04 14:17:19 +08:00
|
|
|
|
|
|
|
|
|
|
2025-01-04 14:54:22 +08:00
|
|
|
|
public void ShanXingAttack() // 扇形散射
|
2025-01-04 14:17:19 +08:00
|
|
|
|
{
|
|
|
|
|
if (bulletPrefab == null)
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("子弹预制体为空");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float halfAngle = Angle / 2f; // 半个散射角度范围
|
|
|
|
|
float angleStep = Angle / (BulletNumber - 1); // 每个子弹之间的角度间隔
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < BulletNumber; i++)
|
|
|
|
|
{
|
2025-01-04 14:54:22 +08:00
|
|
|
|
// 创建子弹实例
|
2025-01-04 14:17:19 +08:00
|
|
|
|
GameObject BulletGamobj = GameObject.Instantiate(bulletPrefab, this.transform.root);
|
|
|
|
|
Bullet bulletScript = BulletGamobj.GetComponent<Bullet>();
|
|
|
|
|
|
2025-01-04 14:54:22 +08:00
|
|
|
|
// 设置子弹的基本信息
|
2025-01-04 14:17:19 +08:00
|
|
|
|
bulletScript.role = role;
|
|
|
|
|
bulletScript.attackObj = this;
|
2025-01-04 14:54:22 +08:00
|
|
|
|
bulletScript.bulletData.BulletSpeed *= (1 + roleBulletSpeedAdd); // 根据角色的加成调整子弹速度
|
2025-01-04 14:17:19 +08:00
|
|
|
|
bulletScript.Target = Target;
|
|
|
|
|
|
|
|
|
|
// 计算当前子弹的发射角度
|
|
|
|
|
float currentAngle = -halfAngle + angleStep * i; // 当前子弹的偏移角度
|
|
|
|
|
|
|
|
|
|
// 使用偏移角度调整子弹的发射方向
|
2025-01-04 14:54:22 +08:00
|
|
|
|
// 只在z轴上进行旋转(2D场景)
|
2025-01-07 17:26:01 +08:00
|
|
|
|
|
2025-01-04 14:17:19 +08:00
|
|
|
|
|
2025-01-07 17:26:01 +08:00
|
|
|
|
if (BulletNumber==1)
|
|
|
|
|
{
|
|
|
|
|
BulletGamobj.transform.up = direction;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Vector3 scatterDirection = Quaternion.Euler(0, 0, currentAngle) * direction; // 只旋转z轴
|
|
|
|
|
// 设置子弹的方向和位置
|
|
|
|
|
BulletGamobj.transform.up = scatterDirection;
|
|
|
|
|
|
|
|
|
|
}
|
2025-01-04 14:54:22 +08:00
|
|
|
|
|
2025-01-07 17:26:01 +08:00
|
|
|
|
BulletGamobj.transform.position = BulletStartPos.position;
|
2025-01-04 14:54:22 +08:00
|
|
|
|
// 将子弹加入子弹列表
|
2025-01-04 14:17:19 +08:00
|
|
|
|
bulltes.Add(BulletGamobj);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-04 14:54:22 +08:00
|
|
|
|
|
2025-01-04 18:09:38 +08:00
|
|
|
|
private int lastBulletNumber = 0; // 记录上一次的 BulletNumber
|
2025-01-04 20:08:40 +08:00
|
|
|
|
private float lastbulletLengthAdd = 0; // 记录上一次的 BulletNumber
|
|
|
|
|
public float bulletLengthAdd = 0f; // 子弹的长度属性
|
2025-01-04 18:09:38 +08:00
|
|
|
|
|
|
|
|
|
public void RunRangeattack() // 生成在周围
|
|
|
|
|
{
|
|
|
|
|
if (bulletPrefab == null)
|
|
|
|
|
{
|
|
|
|
|
// Debug.LogError("子弹预制体为空");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 只有在 BulletNumber 变化时,才重新生成子弹
|
2025-01-04 20:08:40 +08:00
|
|
|
|
if (BulletNumber != lastBulletNumber||lastbulletLengthAdd!=bulletLengthAdd) // 检查 BulletNumber 和 bulletLength 是否变化
|
2025-01-04 18:09:38 +08:00
|
|
|
|
{
|
|
|
|
|
// 销毁现有的所有子弹
|
2025-01-04 20:08:40 +08:00
|
|
|
|
if (bulltes.Count > 0)
|
2025-01-04 18:09:38 +08:00
|
|
|
|
{
|
|
|
|
|
foreach (var bullet in bulltes)
|
|
|
|
|
{
|
|
|
|
|
Destroy(bullet); // 销毁子弹对象
|
|
|
|
|
}
|
|
|
|
|
bulltes.Clear(); // 清空子弹列表
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 计算子弹发射的圆周角度间隔
|
|
|
|
|
float angleStep = 360f / BulletNumber; // 计算每个子弹之间的角度间隔
|
|
|
|
|
|
|
|
|
|
// 生成新的子弹
|
|
|
|
|
for (int i = 0; i < BulletNumber; i++)
|
|
|
|
|
{
|
|
|
|
|
// 创建子弹实例
|
|
|
|
|
GameObject BulletGamobj = GameObject.Instantiate(bulletPrefab, this.transform.root);
|
|
|
|
|
|
|
|
|
|
// 获取Bullet组件
|
|
|
|
|
RunRangeBullet bulletScript = BulletGamobj.GetComponent<RunRangeBullet>();
|
|
|
|
|
|
|
|
|
|
// 设置子弹的基本信息
|
|
|
|
|
bulletScript.role = role;
|
|
|
|
|
bulletScript.attackObj = this;
|
|
|
|
|
bulletScript.bulletData.BulletSpeed *= (1 + roleBulletSpeedAdd); // 根据角色的加成调整子弹速度
|
|
|
|
|
bulletScript.Target = Target;
|
|
|
|
|
|
|
|
|
|
// 将新子弹加入子弹列表
|
|
|
|
|
bulltes.Add(BulletGamobj);
|
|
|
|
|
|
|
|
|
|
// 计算当前子弹的发射角度(基于子弹的索引)
|
|
|
|
|
float currentAngle = angleStep * i; // 当前子弹的发射角度
|
|
|
|
|
|
|
|
|
|
// 计算子弹的发射位置,使用极坐标来计算圆周上的位置
|
|
|
|
|
float x = Mathf.Cos(Mathf.Deg2Rad * currentAngle) * RunRange;
|
|
|
|
|
float y = Mathf.Sin(Mathf.Deg2Rad * currentAngle) * RunRange;
|
|
|
|
|
Vector3 position = new Vector3(x, y, 0) + BulletStartPos.position; // 加上起始位置
|
|
|
|
|
|
|
|
|
|
// 计算子弹的发射方向,使用极坐标转换为单位向量
|
|
|
|
|
Vector3 scatterDirection = new Vector3(Mathf.Cos(Mathf.Deg2Rad * currentAngle), Mathf.Sin(Mathf.Deg2Rad * currentAngle), 0);
|
|
|
|
|
|
|
|
|
|
// 设置子弹的朝向和位置
|
|
|
|
|
BulletGamobj.transform.up = scatterDirection; // 让子弹的"up"方向指向计算出的角度
|
|
|
|
|
BulletGamobj.transform.position = position; // 更新子弹的位置
|
2025-01-04 20:08:40 +08:00
|
|
|
|
|
|
|
|
|
if (lastbulletLengthAdd != bulletLengthAdd)
|
|
|
|
|
{
|
|
|
|
|
// 根据 bulletLength 设置子弹的长度
|
|
|
|
|
BulletGamobj.transform.localScale = new Vector3(BulletGamobj.transform.localScale.x + bulletLengthAdd, BulletGamobj.transform.localScale.y, BulletGamobj.transform.localScale.z);
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-04 18:09:38 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-01-04 20:08:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 更新记录的 BulletNumber 和 bulletLength
|
2025-01-04 18:09:38 +08:00
|
|
|
|
lastBulletNumber = BulletNumber;
|
2025-01-04 20:08:40 +08:00
|
|
|
|
lastbulletLengthAdd = bulletLengthAdd;
|
2025-01-04 18:09:38 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-12-05 16:25:46 +08:00
|
|
|
|
}
|