using DG.Tweening; using System.Collections; using System.Collections.Generic; using UnityEngine; public class OtherMove : MonoBehaviour { public Transform objectToMove; // 要移动的对象 public Transform[] pathPoints; // 路径上的点的数组 public float duration = 5f; // 移动的持续时间 public bool isLooping = false; // 是否循环路径 int startIndex = 0; int endIndex = 0; public float Speed; public Transform EndPos; public Transform posGroup; public Transform JumpPos; public Transform OutParent; public Transform ReturnPos;//返回的位置 public AllHouseContro allHouseContro; private void Awake() { Speed = 2f; objectToMove = transform; allHouseContro=GameObject.Find("Canvas/Bg").GetComponent(); posGroup=GameObject.Find("Canvas/Bg/Posgroup").transform; if (posGroup != null) { // 获取父物体的所有子节点 pathPoints = posGroup.GetComponentsInChildren(); // 去掉父物体自身(只要子节点) pathPoints = System.Array.FindAll(pathPoints, t => t != posGroup); Debug.Log("子节点已存入数组"+ pathPoints.Length); } else { Debug.LogError("请分配父物体!"); } OutParent= GameObject.Find("Canvas/Bg").transform ; JumpPos = GameObject.Find("Canvas/Bg/Posgroup/pos (3)").transform; } public void MoveOutParent() { // 记录原始的父物体 // 将 childObject 设为新的 parentObject 的子节点,并放到最后一个位置 objectToMove.SetParent(OutParent); objectToMove.SetAsLastSibling(); } public void MoveInParent(Transform Parent) { objectToMove.SetParent(Parent); objectToMove.SetAsFirstSibling(); } public void SetParentToHouse() { foreach (HouseBtn house in allHouseContro.HouseBtnList) { if (EndPos==house.post.transform) { MoveInParent(house.transform); } } } public void ReturnStartPos()//回到起始点 { } public bool JudagePos1() { if (EndPos == JumpPos) { return true; } return false; } public void StartMove() { Debug.Log("启动其他蜗牛移动"); // 提取路径中的所有点的位置 Debug.Log("路径长度"+ pathPoints.Length); for (int i = 0; i < pathPoints.Length; i++) { Debug.Log("其他蜗判断终点"); if (objectToMove.GetComponent().StartPos == pathPoints[i]) { startIndex = i; //Debug.Log("Startindex"+i); } if (EndPos == pathPoints[i]) { endIndex = i; //Debug.Log("Endindex" + i); } } if (endIndex > startIndex) { Debug.Log("其他蜗牛分配路径"); duration = (endIndex - startIndex) / Speed; Vector3[] waypoints = new Vector3[endIndex - startIndex + 1]; for (int i = startIndex, j = 0; j < pathPoints.Length && i <= endIndex; i++, j++) { if (!JudagePos1() && pathPoints[i] == JumpPos) { waypoints[j] = pathPoints[i - 1].position; continue; } waypoints[j] = pathPoints[i].position; } OnMove(waypoints); } else if (endIndex < startIndex) { duration = (startIndex - endIndex) / Speed; //duration = (startIndex-endIndex) / Speed; Vector3[] waypoints = new Vector3[startIndex - endIndex + 1]; for (int i = startIndex, j = 0; j < pathPoints.Length && i >= endIndex; i--, j++) { if (!JudagePos1() && pathPoints[i] == JumpPos) { waypoints[j] = pathPoints[i + 1].position; continue; } waypoints[j] = pathPoints[i].position; } OnMove(waypoints); } } public void AfterMove() { Debug.Log("其他蜗牛到达终点"); RectTransform rectTransform = objectToMove.GetComponent(); //objectToMove.DOMoveY(objectToMove.position.y + 100, 0.5f).OnComplete(() => { rectTransform.DOAnchorPosY(rectTransform.anchoredPosition.y + 80f, 0.5f).SetEase(Ease.Linear) .OnComplete(() => { SetParentToHouse(); objectToMove.GetComponent().StartPos = EndPos; objectToMove.GetComponent().PlayAni.SetInteger("State", 0); // rectTransform.anchoredPosition = new Vector2(rectTransform.anchoredPosition.x + Random.Range(-100, 100), rectTransform.anchoredPosition.y); float range = Random.Range(-100, 100); // 判断物体是否需要旋转 if (range>0&& objectToMove.GetComponent().IsLeft) { objectToMove.GetComponent().IsLeft = false; // 旋转物体使其面向左侧(绕Y轴旋转180度) Rotate(); } else if (range<0 && !objectToMove.GetComponent().IsLeft) { objectToMove.GetComponent().IsLeft = true; Rotate(); } rectTransform.DOAnchorPosX(rectTransform.anchoredPosition.x + range, 1f).SetEase(Ease.Linear); }); //}); } public void Rotate() { Vector3 currentRotation = objectToMove.localEulerAngles; currentRotation.y += 180f; objectToMove.localEulerAngles = currentRotation; } public void OnMove(Vector3[] waypoints) { Debug.Log("其他蜗牛开始移动"); objectToMove.GetComponent().PlayAni.SetInteger("State", 1); MoveOutParent(); Tween pathTween = objectToMove.DOPath(waypoints, duration, PathType.Linear) .SetEase(Ease.Linear) // 设置线性过渡效果 .SetLoops(isLooping ? -1 : 0) .OnWaypointChange((index) => { // 当物体到达某个路径点时回调 if (index < waypoints.Length - 1) { Vector3 currentWaypoint = waypoints[index]; Vector3 nextWaypoint = waypoints[index + 1]; // 判断物体是否需要旋转 if (nextWaypoint.x < currentWaypoint.x && !objectToMove.GetComponent().IsLeft) { objectToMove.GetComponent().IsLeft = true; // 旋转物体使其面向左侧(绕Y轴旋转180度) Rotate(); } else if (nextWaypoint.x > currentWaypoint.x && objectToMove.GetComponent().IsLeft) { objectToMove.GetComponent().IsLeft = false; Rotate(); } } }) .OnComplete(AfterMove); } }