【发布时间】:2021-09-23 02:35:40
【问题描述】:
Unity 新人,以及一般的新编码员。我试图让一个物体在两个点之间来回移动。我现在有它从第 1 点到第 2 点,但它停止了。我尝试了一个 do 循环,并再次回调 Move() 函数,但它只是冻结了 Unity。
我猜我需要某种循环,但不确定在哪里做?我也不介意能够添加更多的点。我在 Unity 本身中有航点,与对象相关联。谢谢!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ZombiePathing : MonoBehaviour
{
[SerializeField] List<Transform> waypoints;
[SerializeField] float moveSpeed = 2f;
int waypointIndex = 0;
void Start()
{
transform.position = waypoints[waypointIndex].transform.position;
}
// Update is called once per frame
void Update()
{
Move();
}
private void Move()
{
if (waypointIndex <= waypoints.Count -1)
{
var targetPosition = waypoints[waypointIndex].transform.position;
var movementThisFrame = moveSpeed * Time.deltaTime;
transform.position = Vector2.MoveTowards
(transform.position, targetPosition, movementThisFrame);
if (transform.position == targetPosition)
{
waypointIndex++;
}
}
}
}
【问题讨论】:
-
请编辑问题以将其限制为具有足够详细信息的特定问题,以确定适当的答案。