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 OutParent; public Transform ReturnPos;//返回的位置 public AllHouseContro allHouseContro; private void Start() { 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("子节点已存入数组"); } else { Debug.LogError("请分配父物体!"); } OutParent= GameObject.Find("Canvas/Bg").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 void StartMove() { // 提取路径中的所有点的位置 for (int i = 0; i < pathPoints.Length; i++) { 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) { duration = (endIndex - startIndex) / Speed; Vector3[] waypoints = new Vector3[endIndex - startIndex + 1]; for (int i = startIndex, j = 0; j < pathPoints.Length && i <= endIndex; i++, j++) { 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++) { waypoints[j] = pathPoints[i].position; } OnMove(waypoints); } } public void AfterMove() { RectTransform rectTransform = objectToMove.GetComponent(); //objectToMove.DOMoveY(objectToMove.position.y + 100, 0.5f).OnComplete(() => { rectTransform.DOAnchorPosY(rectTransform.anchoredPosition.y + 80f, 0.5f) .OnComplete(() => { SetParentToHouse(); objectToMove.GetComponent().StartPos = EndPos; objectToMove.GetComponent().PlayAni.SetInteger("State", 0); rectTransform.anchoredPosition = new Vector2(rectTransform.anchoredPosition.x + Random.Range(-50, 50), rectTransform.anchoredPosition.y); }); //}); } public void Rotate() { Vector3 currentRotation = objectToMove.localEulerAngles; currentRotation.y += 180f; objectToMove.localEulerAngles = currentRotation; } public void OnMove(Vector3[] waypoints) { 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); } }