【发布时间】:2021-12-02 22:17:49
【问题描述】:
您好,我正在尝试创建一个实例化和对象的系统,并将其移动到一组航点。当我在 Start() 函数中使用 intantiate 时,它会像预期的那样通过航路点,但是当我将实例化线添加到我的 update() 函数时,它只会移动一会儿然后停止,其余的实例化也是如此对象。我猜它与计时器有关,但我尝试了一些方法,但它们都导致相同的事情。希望有人能帮忙。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class waypointTest : MonoBehaviour
{
[SerializeField] private Transform[] waypoints;
public GameObject waypointInvader;
GameObject go;
private int nextUpdate = 1;
public float speed = 5f;
private int waypointIndex;
void Start()
{
}
void Update()
{
if (Time.time >= nextUpdate)
{
nextUpdate = Mathf.FloorToInt(Time.time) + 1;
go = Instantiate(waypointInvader, transform.position, transform.rotation) as GameObject;
}
if (go.transform.position != waypoints[waypointIndex].transform.position)
{
Vector3 newPos = Vector3.MoveTowards(go.transform.position, waypoints[waypointIndex].transform.position, speed * Time.deltaTime);
go.transform.position = newPos;
if (newPos == waypoints[waypointIndex].transform.position)
{
waypointIndex += 1;
}
if (waypointIndex == waypoints.Length)
{
waypointIndex = 0;
}
}
}
}
【问题讨论】:
标签: unity3d timer instantiation