using Assets.MyGame.Scripts.Net; using Assets.MyGame.Scripts.Net.API; using Assets.MyGame.Scripts.Net.WebResponses; using System.Collections; using System.Collections.Generic; using System.Text; using UnityEngine; public class BullitMove : MonoBehaviour { public string userid; public float moveSpeed = 5; public float desTime = 10f; public string playerID; public GameObject effectPrefab; public GameObject diePrefab; float currShowTime = 0f; Vector3 vector3_startPosition; public bool isTriggerFirst = true; MonsterContral target; public Transform targetTra; public string bulletID; // 转向参数 public float maxTurnSpeed = 15f; // 最大转向速度(度/秒) public float minTurnSpeed = 2f; // 最小转向速度 public float distanceForMaxTurn = 1f; // 在此距离内达到最大转向速度 public float hitDistance = 0.5f; // 命中判定距离 void Awake() { Init(); } bool isLock = false; public void Lock(MonsterContral monsterContral, Transform endTra) { foreach (var item in GetComponents()) { item.enabled = false; } isLock = true; target = monsterContral; targetTra = endTra; } public void NotLock() { foreach (var item in GetComponents()) { item.enabled = true; } isLock = false; target = null; targetTra = null; } public void Init() { vector3_startPosition = transform.position; vector3_startPosition.z = -400; transform.position = vector3_startPosition; } void Update() { // 子弹生命周期:超时自动销毁(默认10秒) currShowTime += Time.deltaTime; if (currShowTime >= desTime) { Destroy(gameObject); return; } if (isLock) { // 目标无效 → 解除锁定 if (target == null || targetTra == null || !target.gameObject.activeInHierarchy || !targetTra.gameObject.activeInHierarchy) { NotLock(); return; } Vector3 myPos = transform.position; myPos.z = 0; Vector3 targetPos = targetTra.position; targetPos.z = 0; float distance = Vector3.Distance(myPos, targetPos); // 命中判定 if (distance <= hitDistance) { AttackFish(target); return; } // 计算指向目标的方向 Vector3 targetDir = (targetPos - myPos).normalized; // 根据距离动态计算转向速度:距离越近转向越快 float t = Mathf.Clamp01(1f - (distance / distanceForMaxTurn)); float turnSpeed = Mathf.Lerp(minTurnSpeed, maxTurnSpeed, t); // 使用 RotateTowards 平滑转向(角度/秒) float currentAngle = Vector2.SignedAngle(transform.up, targetDir); float newAngle = Mathf.MoveTowardsAngle(currentAngle, 0f, turnSpeed * Time.deltaTime); transform.up = Quaternion.Euler(0, 0, newAngle) * transform.up; // 沿当前方向移动 transform.position += transform.up * moveSpeed * Time.deltaTime; } else { // 非锁定模式:保持原有直线 + 反弹逻辑(不变) Vector3 targetPoint = transform.up.normalized * currShowTime * moveSpeed; targetPoint.z = 0; transform.position = vector3_startPosition + targetPoint; } } private void OnCollisionEnter2D(Collision2D collision) { if (isTriggerFirst) { if (collision.transform.tag == "Fish") { isTriggerFirst = false; if (effectPrefab != null) { Instantiate(effectPrefab, transform.position, Quaternion.identity, transform.parent); } if (userid == GameManager.Instance.user.id) { var monster = collision.transform.GetComponentInParent(); monster.monsterData.charactersHealth.OnHurt(10, CharacterHurtType.none); Destroy(monster.gameObject); } Destroy(gameObject); } } } public static Quaternion RotationFromVector2(Vector2 dir) { float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg; return Quaternion.Euler(0, 0, angle); } Vector2 GetReflectionAngle(Vector2 velocity, Vector2 normal) { return Vector2.Reflect(velocity.normalized, normal.normalized); } public void OnTriggerEnter2D(Collider2D collision) { if (isTriggerFirst) { if (collision.tag == "Fish") { isTriggerFirst = false; var monster = collision.transform.GetComponentInParent(); AttackFish(monster); } else if (collision.tag == "Wall") { Vector3 v3 = transform.eulerAngles; switch (collision.name) { case "up": v3.z = -v3.z + 180; break; case "down": v3.z = -v3.z + 180; break; case "left": v3.z = -v3.z; break; case "right": v3.z = -v3.z; break; default: break; } vector3_startPosition = transform.position; currShowTime = 0; transform.eulerAngles = v3; } } } private void AttackFish(MonsterContral monster) { if (effectPrefab != null) { Instantiate(effectPrefab, transform.position, Quaternion.identity, transform.parent); } if (userid == GameManager.Instance.user.id) { var v2 = GameManager.Instance.gameData.currRoomData; WebSocketSendList.KillFish(v2.roomId, userid, monster.monsterData.fishTypeConfig.fishNumber, monster.guid, (int)CreateFishManager.Instance.currGameType.currSitData.consumeGoldNum, bulletID, "63215f0afa3f5b25"); } monster.OnHurt(); Destroy(gameObject); } }