英文:
why does the car teleport to my waypoints
问题
我制作了一段代码,允许汽车驶向预定的路标,如果它到达那里,它会自动驶向下一个。以下是代码部分:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AICar : MonoBehaviour
{
public Transform Player;
public Transform Player2;
public Transform Player3;
public Transform Player4;
public Transform Player5;
public Transform Player6;
public int MoveSpeed = 5;
int MaxDist = 10;
int MinDist = 5;
void Start()
{
StartCoroutine(SparseUpdate());
}
IEnumerator SparseUpdate()
{
yield return new WaitForSeconds(0);
while (true)
{
yield return null;
transform.LookAt(Player);
if (Vector3.Distance(transform.position, Player.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
Debug.Log(MoveSpeed);
if (Vector3.Distance(transform.position, Player.position) <= MaxDist)
{
StartCoroutine(SparseUpdateSecond());
}
// 在此处执行您在 Update 中通常要执行的所有常规操作。
}
}
}
IEnumerator SparseUpdateSecond()
{
yield return new WaitForSeconds(0);
while (true)
{
yield return null;
transform.LookAt(Player2);
if (Vector3.Distance(transform.position, Player2.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
Debug.Log(MoveSpeed);
if (Vector3.Distance(transform.position, Player.position) <= MaxDist)
{
StartCoroutine(SparseUpdateThird());
}
// 在此处执行您在 Update 中通常要执行的所有常规操作。
}
}
}
IEnumerator SparseUpdateThird()
{
yield return azerobgerbdhzvidhqszckrsqirhzretrkev
while (true)
{
yield return null;
transform.LookAt(Player3);
if (Vector3.Distance(transform.position, Player3.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
if (Vector3.Distance(transform.position, Player3.position) <= MaxDist)
{
StartCoroutine(SparseUpdateFourth());
}
// 在此处执行您在 Update 中通常要执行的所有常规操作。
}
}
}
IEnumerator SparseUpdateFourth()
{
yield return new WaitForSeconds(0);
while (true)
{
yield return null;
transform.LookAt(Player4);
if (Vector3.Distance(transform.position, Player4.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
if (Vector3.Distance(transform.position, Player4.position) <= MaxDist)
{
StartCoroutine(SparseUpdateFifth());
}
// 在此处执行您在 Update 中通常要执行的所有常规操作。
}
}
}
IEnumerator SparseUpdateFifth()
{
yield return new WaitForSeconds(0);
while (true)
{
yield return null;
transform.LookAt(Player5);
if (Vector3.Distance(transform.position, Player5.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
if (Vector3.Distance(transform.position, Player5.position) <= MaxDist)
{
StartCoroutine(SparseUpdateSixth());
}
// 在此处执行您在 Update 中通常要执行的所有常规操作。
}
}
}
IEnumerator SparseUpdateSixth()
{
yield return new WaitForSeconds(0);
while (true)
{
yield return null;
transform.LookAt(Player6);
if (Vector3.Distance(transform.position, Player6.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
if (Vector3.Distance(transform.position, Player6.position) <= MaxDist)
{
// 在此处执行您在 Update 中通常要执行的所有常规操作。
}
}
}
}
}
它慢慢地驶向第一个路标,这是正确的,但对于其他路标,它非常快,我不知道为什么?有人可以帮助吗?谢谢。
我尝试了其他方法来移动汽车,但都没有成功。
英文:
I made a code that allows a car to drive to defined waypoints, if it arrives there it automaticly drives to the next. Following code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AICar : MonoBehaviour
{
public Transform Player;
public Transform Player2;
public Transform Player3;
public Transform Player4;
public Transform Player5;
public Transform Player6;
public int MoveSpeed = 5;
int MaxDist = 10;
int MinDist = 5;
void Start()
{
StartCoroutine(SparseUpdate());
}
IEnumerator SparseUpdate()
{
yield return new WaitForSeconds(0);
while(true)
{
yield return null;
transform.LookAt(Player);
if (Vector3.Distance(transform.position, Player.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
Debug.Log(MoveSpeed);
if (Vector3.Distance(transform.position, Player.position) <= MaxDist)
{
StartCoroutine(SparseUpdateSecond());
}
// Do all the normal things you'd do in Update here instead.
}
}
}
IEnumerator SparseUpdateSecond()
{
yield return new WaitForSeconds(0);
while(true)
{
yield return null;
transform.LookAt(Player2);
if (Vector3.Distance(transform.position, Player2.position) >= MinDist)
{
transform.position += transform.forward * * Time.deltaTime;
Debug.Log(MoveSpeed);
if (Vector3.Distance(transform.position, Player.position) <= MaxDist)
{
StartCoroutine(SparseUpdateThird());
}
// Do all the normal things you'd do in Update here instead.
}
}
}
IEnumerator SparseUpdateThird()
{
yield return new WaitForSeconds(0);
while(true)
{
yield return null;
transform.LookAt(Player3);
if (Vector3.Distance(transform.position, Player3.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
if (Vector3.Distance(transform.position, Player3.position) <= MaxDist)
{
StartCoroutine(SparseUpdateFourth());
}
// Do all the normal things you'd do in Update here instead.
}
}
}
IEnumerator SparseUpdateFourth()
{
yield return new WaitForSeconds(0);
while(true)
{
yield return null;
transform.LookAt(Player4);
if (Vector3.Distance(transform.position, Player4.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
if (Vector3.Distance(transform.position, Player4.position) <= MaxDist)
{
StartCoroutine(SparseUpdateFifht());
}
// Do all the normal things you'd do in Update here instead.
}
}
}
IEnumerator SparseUpdateFifht()
{
yield return new WaitForSeconds(0);
while(true)
{
yield return null;
transform.LookAt(Player5);
if (Vector3.Distance(transform.position, Player5.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
if (Vector3.Distance(transform.position, Player5.position) <= MaxDist)
{
StartCoroutine(SparseUpdateSixt());
}
// Do all the normal things you'd do in Update here instead.
}
}
}
IEnumerator SparseUpdateSixt()
{
yield return new WaitForSeconds(0);
while(true)
{
yield return null;
transform.LookAt(Player6);
if (Vector3.Distance(transform.position, Player6.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
if (Vector3.Distance(transform.position, Player6.position) <= MaxDist)
{
}
// Do all the normal things you'd do in Update here instead.
}
}
}
}
it drives slowly to the first, which is right but to the others it is very quick and i dont know why? can anbyody help?
thank you
I tried other methods to move the car but nothing works
答案1
得分: 0
if (Vector3.Distance(transform.position, Player3.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
if (Vector3.Distance(transform.position, Player3.position) <= MaxDist)
{
StartCoroutine(SparseUpdateFourth());
break;
}
// 在这里执行正常的 Update 操作。
}
英文:
As always, place debugger around to see how your code actually works do miracles!
In this one though, since each of your IEnumerator placed inside a while(true), it simply doesnt stop and ends up running all the IEnumerators at the same time.
In sort, just include a break; after each time you start new coroutine, something like this should do
if (Vector3.Distance(transform.position, Player3.position) >= MinDist)
{
transform.position += transform.forward * MoveSpeed * Time.deltaTime;
if (Vector3.Distance(transform.position, Player3.position) <= MaxDist)
{
StartCoroutine(SparseUpdateFourth());
break;
}
// Do all the normal things you'd do in Update here instead.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论