You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:给每个生成对象添加独立移动脚本

让每个立方体预制体自带路径移动逻辑,生成时绑定路径点即可:

  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);
        }
    }
}
  1. 修改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;
            }
        }
    }
}
  1. 精简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脚本维护移动对象列表,统一控制所有生成对象的路径移动:

  1. 修改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;
}
  1. 修改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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 07:30:51