求助:实例化对象不沿iTween路径移动的修复方法
问题描述
我已完成以下操作:
- 实例化一个新的对象预制体
- 设置预制体Transform组件的所有变量
- 将可正常工作的初始胶囊体脚本复制到预制体生成脚本中
但实例化的预制体始终无法沿路径移动,请问如何让它们沿路径移动?
相关代码
PathFollower.cs
public class PathFollower : MonoBehaviour { public float movementSpeed; //private float pathProgress = 0; public GameObject objectToSpawn; public Transform[] positionPoint; [Range(0,1)] public float value; void Start() { Debug.Log(iTween.PathLength(positionPoint)); } void Update() { movementSpeed = 10f; if (value < 1) { value += Time.deltaTime / movementSpeed; } iTween.PutOnPath(objectToSpawn, positionPoint, value); } private void OnDrawGizmos() { iTween.DrawPath(positionPoint,Color.green); } }
deployCapsule.cs
public class deployCapsule : MonoBehaviour { public float movementSpeed; public Transform[] positionPoint; public GameObject CapsulePrefab; [Range(0, 1)] public float value; //movementspeed = 10f; // Start is called before the first frame update void Start() { } void Update() { GameObject a = Instantiate(CapsulePrefab) as GameObject; a.transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width - 20, Screen.height - 20, 10)); a.GetComponent<PathFollower>().positionPoint = positionPoint; movementSpeed = 10f; if (value < 1) { value += Time.deltaTime / movementSpeed; } iTween.PutOnPath(a, positionPoint, value); } }
脚本挂载及配置截图
- Main Camera挂载deployCapsule:

- Capsule挂载PathFollower:

- Capsule Prefab挂载PathFollower:

解决方案
1. 修复PathFollower脚本(让组件控制自身移动)
原脚本错误地尝试移动外部对象objectToSpawn,改为控制自身移动,同时优化逻辑健壮性:
public class PathFollower : MonoBehaviour { public float movementSpeed = 10f; public Transform[] positionPoint; [Range(0,1)] private float value; // 改为私有,避免外部干扰 void Start() { if(positionPoint != null && positionPoint.Length > 0) { Debug.Log(iTween.PathLength(positionPoint)); } } void Update() { // 仅在路径点有效且未走完时执行移动 if (value < 1 && positionPoint != null && positionPoint.Length > 0) { value += Time.deltaTime / movementSpeed; iTween.PutOnPath(gameObject, positionPoint, value); } } private void OnDrawGizmos() { if(positionPoint != null && positionPoint.Length > 0) { iTween.DrawPath(positionPoint, Color.green); } } }
2. 修复deployCapsule脚本(仅负责生成和初始化)
原脚本每帧生成大量胶囊且越权处理移动逻辑,改为仅生成对象并传递必要参数:
public class deployCapsule : MonoBehaviour { public float spawnInterval = 2f; // 可选:设置生成间隔,避免瞬间生成过多对象 public Transform[] positionPoint; public GameObject CapsulePrefab; private float spawnTimer; void Update() { spawnTimer += Time.deltaTime; // 按间隔生成,也可改为`Input.GetKeyDown(KeyCode.Space)`实现按空格生成 if(spawnTimer >= spawnInterval) { SpawnCapsule(); spawnTimer = 0; } } void SpawnCapsule() { GameObject newCapsule = Instantiate(CapsulePrefab); newCapsule.transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width - 20, Screen.height - 20, 10)); // 给新生成的胶囊传递路径点参数 PathFollower follower = newCapsule.GetComponent<PathFollower>(); follower.positionPoint = positionPoint; // 可选:直接设置移动速度 // follower.movementSpeed = 15f; } }
3. 验证配置
- 确保Capsule Prefab上的
PathFollower组件中positionPoint无需提前赋值,由生成脚本动态传递 - 确认
deployCapsule中的positionPoint数组已正确配置路径点
内容的提问来源于stack exchange,提问作者Thrillasel
相关产品推荐
相关产品推荐

