gpt4 book ai didi

c# - 为什么我的 Sprite 没有移动到第一个路点之外?

转载 作者:太空宇宙 更新时间:2023-11-03 22:46:31 25 4
gpt4 key购买 nike

The waypoints are present on the way through the maze exits image

The Unity Screenshot Image with hierarchy

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ZombieControl : MonoBehaviour {

[SerializeField]
Transform[] waypoints;

[SerializeField]
float moveSpeed = 2f;

int wayPointIndex =0;

void Start()
{
transform.position = waypoints[wayPointIndex].transform.position;
}

void Update() {
Move ();
}

void Move()
{
transform.position = Vector2.MoveTowards (transform.position, waypoints [wayPointIndex].transform.position, moveSpeed * Time.deltaTime);

if (transform.position == waypoints [wayPointIndex].transform.position) {
wayPointIndex += 1;
}

if (wayPointIndex == waypoints.Length)
wayPointIndex = 0;
}
}

最佳答案

我猜这是因为你的 transform.position 由于 float 的准确性而永远不会完全等于你的航点位置。将等式与 float 一起使用时,您应该使用公差。

if (Vector2.Distance(transform.position, waypoints[wayPointIndex].transform.position) < .05f) // or some other small distance
{
wayPointIndex += 1;
}

关于c# - 为什么我的 Sprite 没有移动到第一个路点之外?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49612998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com