Unity中如何让实例化对象自动沿iTween路径移动
Unity iTween路径移动与对象生成结合问题
我刚接触C#和Unity,现有两个脚本:SpawnerScript负责生成对象,Path脚本用iTween实现对象沿路径移动。需要让新实例化的对象自动加入路径并沿其移动。
路径由iTween创建,脚本挂载在指定对象上。我试过将m_PlayerObj改为立方体预制体,也尝试整合两个脚本,均未成功。以下是未修改的原始脚本:
SpawnerScript 代码
using System.Collections; using System.Collections.Generic; using UnityEngine; public class SpawnerScript : MonoBehaviour { public GameObject cubeprefab; void Update() { if (Input.GetKeyDown(KeyCode.Space)) { Instantiate(cubeprefab, transform.position, Quaternion.identity); } } }
Path 代码
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Path : MonoBehaviour { public GameObject m_PlayerObj; public Transform[] positionPoint; [Range(0, 1)] public float value; // Start is called before the first frame update void Start() { Debug.Log(iTween.PathLength(positionPoint)); } float tempTime; // Update is called once per frame void Update() { if (value < 1) { value += Time.deltaTime / 10; } iTween.PutOnPath(m_PlayerObj, positionPoint, value); } private void OnDrawGizmos() { iTween.DrawPath(positionPoint,Color.green); } }
解决方法
方案1:给每个生成对象添加独立移动脚本
让每个立方体预制体自带路径移动逻辑,生成时绑定路径点即可:
- 创建
MoveAlongPath脚本并挂载到立方体预制体:
using UnityEngine; public class MoveAlongPath : MonoBehaviour { public Transform[] pathPoints; [Range(0, 1)] private float progress; private float moveDuration = 10f; // 路径总移动时长(秒) void Update() { if (progress < 1f) { progress += Time.deltaTime / moveDuration; iTween.PutOnPath(gameObject, pathPoints, progress); } } }
- 修改
SpawnerScript,给新生成的立方体绑定路径点:
using UnityEngine; public class SpawnerScript : MonoBehaviour { public GameObject cubeprefab; public Path pathController; // Inspector中拖入挂载Path脚本的对象 void Update() { if (Input.GetKeyDown(KeyCode.Space)) { GameObject newCube = Instantiate(cubeprefab, transform.position, Quaternion.identity); MoveAlongPath mover = newCube.GetComponent<MoveAlongPath>(); if (mover != null) { mover.pathPoints = pathController.positionPoint; } } } }
- 精简
Path脚本,仅保留路径绘制和长度计算:
using UnityEngine; public class Path : MonoBehaviour { public Transform[] positionPoint; void Start() { Debug.Log(iTween.PathLength(positionPoint)); } private void OnDrawGizmos() { iTween.DrawPath(positionPoint, Color.green); } }
方案2:让Path脚本统一管理所有移动对象
通过Path脚本维护移动对象列表,统一控制所有生成对象的路径移动:
- 修改
Path脚本并添加辅助进度组件:
using System.Collections.Generic; using UnityEngine; public class Path : MonoBehaviour { public Transform[] positionPoint; private List<GameObject> movingObjects = new List<GameObject>(); // 供Spawner调用,添加对象到路径 public void AddObjectToPath(GameObject obj) { movingObjects.Add(obj); iTween.PutOnPath(obj, positionPoint, 0f); // 将对象放到路径起点 } void Start() { Debug.Log(iTween.PathLength(positionPoint)); } void Update() { // 倒序遍历避免移除元素时索引混乱 for (int i = movingObjects.Count - 1; i >= 0; i--) { GameObject obj = movingObjects[i]; if (obj == null) { movingObjects.RemoveAt(i); continue; } ProgressTracker tracker = obj.GetComponent<ProgressTracker>(); if (tracker == null) tracker = obj.AddComponent<ProgressTracker>(); if (tracker.progress < 1f) { tracker.progress += Time.deltaTime / 10; iTween.PutOnPath(obj, positionPoint, tracker.progress); } else { movingObjects.RemoveAt(i); Destroy(obj); // 到达终点销毁对象 } } } private void OnDrawGizmos() { iTween.DrawPath(positionPoint, Color.green); } } // 辅助组件,存储单个对象的移动进度 public class ProgressTracker : MonoBehaviour { public float progress; }
- 修改
SpawnerScript,调用Path的添加方法:
using UnityEngine; public class SpawnerScript : MonoBehaviour { public GameObject cubeprefab; public Path pathController; // Inspector中拖入Path对象 void Update() { if (Input.GetKeyDown(KeyCode.Space)) { GameObject newCube = Instantiate(cubeprefab, transform.position, Quaternion.identity); pathController.AddObjectToPath(newCube); } } }
操作提示
- 确保iTween插件已正确导入Unity项目
- 按方案要求在Inspector中完成对象赋值(如给Spawner的
pathController绑定Path对象) - 检查Path脚本的
positionPoint已设置好完整的路径点
内容的提问来源于stack exchange,提问作者Thrillasel
相关产品推荐
相关产品推荐

