我有一个脚本,它使用 LeanTween 将对象(预制)放入预制路径上,效果很好。
其工作原理是,您可以将一个对象分配给附加有 Moveable 脚本的“路径添加器”(MoveController)。
但是,我需要能够将运行时创建的新预制件添加到 MoveController,以便它们遵循路径。
那么我将如何在运行时使实例化对象附加到 MoveController。
谢谢。
可移动脚本,还实例化预制件
public class Moveable : MonoBehaviour
{
[SerializeField] private float _speedMetersPerSecond = 25f;
private Vector3? _destination;
private Vector3 _startPosition;
private float _totalLerpDuration;
private float _elapsedLerpDuration;
private Action _onCompleteCallback;
public GameObject Electron;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
var NextOnPath = Instantiate(Electron, transform.position, Quaternion.identity);
NextOnPath.AddComponent<Moveable>();
}
if (_destination.HasValue == false)
return;
if (_elapsedLerpDuration >= _totalLerpDuration && _totalLerpDuration > 0)
return;
_elapsedLerpDuration += Time.deltaTime;
float percent = (_elapsedLerpDuration / _totalLerpDuration);
Debug.Log($"{percent} = {_elapsedLerpDuration} / {_totalLerpDuration}");
transform.position = Vector3.Lerp(_startPosition, _destination.Value, percent);
}
public void MoveTo(Vector3 destination, Action onComplete = null)
{
var distanceToNextWaypoint = Vector3.Distance(transform.position, destination);
_totalLerpDuration = distanceToNextWaypoint / _speedMetersPerSecond;
_startPosition = transform.position;
_destination = destination;
_elapsedLerpDuration = 0f;
_onCompleteCallback = onComplete;
}
}
移动控制器脚本
using System.Linq;
public class MoverController : MonoBehaviour
{
[SerializeField] private Moveable target;
private List<Transform> _waypoints;
private int _nextWaypointIndex;
private void OnEnable()
{
_waypoints = GetComponentsInChildren<Transform>().ToList();
_waypoints.RemoveAt(0);
MoveToNextWaypoint();
}
private void MoveToNextWaypoint()
{
var targetWaypointTransform = _waypoints[_nextWaypointIndex];
target.MoveTo(targetWaypointTransform.position, MoveToNextWaypoint);
target.transform.LookAt(_waypoints[_nextWaypointIndex].position);
_nextWaypointIndex++;
if (_nextWaypointIndex >= _waypoints.Count)
_nextWaypointIndex = 0;
}
}
如果我需要澄清任何事情,请告诉我。
我知道这是一个相当沉重的问题,但我将非常感谢任何帮助!
Solution
Moveable:
public class Moveable : MonoBehaviour
{
[SerializeField] private float _speedMetersPerSecond = 25f;
public List<Moveable> moveables = new List<Moveable>();
private Vector3? _destination;
private Vector3 _startPosition;
private float _totalLerpDuration;
private float _elapsedLerpDuration;
private Action _onCompleteCallback;
public GameObject Electron;
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Moveable NextOnPath = Instantiate(Electron, transform.position, Quaternion.identity).GetComponent<Moveable>();
moveables.Add(NextOnPath.GetComponent<Moveable>());
MoverController.instance.targets.Add(NextOnPath.GetComponent<Moveable>());
}
}
}
移动控制器:
public List<Moveable> targets;
[SerializeField] private Moveable target;
private List<Transform> _waypoints;
private int _nextWaypointIndex;
public static MoverController instance;
private void MoveToNextWaypoint()
{
for (int i = 0; i < targets.Count; i++)
{
var targetWaypointTransform = _waypoints[_nextWaypointIndex];
targets[i].MoveTo(targetWaypointTransform.position, MoveToNextWaypoint);
targets[i].transform.LookAt(_waypoints[_nextWaypointIndex].position);
_nextWaypointIndex++;
if (_nextWaypointIndex >= _waypoints.Count)
_nextWaypointIndex = 0;
}
}
注意:解决方案中仅显示更改的部分