目標是當按下 P 鍵時,在下一個航路點和最后一個航路點之間進行不間斷的乒乓球運動。包括當變換開始從他的原始位置移動時按下 P 鍵,所以在第一個航路點和變換原始位置之間做一個乒乓球。
現在的情況是,當我按下 P 鍵時,整個游戲都凍結了,編輯器凍結了,我需要將其關閉。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Waypoints : MonoBehaviour
{
public List<GameObject> WayPoints;
private int CurrentWayPoint = 0;
private bool IsPatrolling = false;
private void Start()
{
StartPatrol();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
ReversePatrolWayPoints();
}
if (Input.GetKeyDown(KeyCode.P))
{
PingPong();
}
}
public void StartPatrol()
{
if (!IsPatrolling)
{
StartCoroutine(Patrol());
}
}
public void ReversePatrolWayPoints()
{
WayPoints.Reverse();
}
bool pingPongEnabled = false;
Vector3 pingPongStart;
public void PingPong()
{
pingPongEnabled = true;
pingPongStart = transform.position;
}
private IEnumerator Patrol()
{
if (WayPoints == null || WayPoints.Count == 0)
{
Debug.Log("Waypoints list is null or empty");
yield break;
}
IsPatrolling = true;
while (CurrentWayPoint < WayPoints.Count)
{
if (pingPongEnabled)
{
transform.position = Vector3.MoveTowards(pingPongStart, WayPoints[CurrentWayPoint].transform.position, Mathf.PingPong(Time.time * 1, 1) * Time.deltaTime);
continue;
}
float distanceToWayPoint = Vector3.Distance(transform.position, WayPoints[CurrentWayPoint].transform.position);
transform.position = Vector3.MoveTowards(transform.position, WayPoints[CurrentWayPoint].transform.position, Time.deltaTime * 1f);
if (distanceToWayPoint <= 1f)
{
Debug.LogFormat("Reached Waypoint {0}", CurrentWayPoint);
CurrentWayPoint ;
if (CurrentWayPoint >= WayPoints.Count)
{
CurrentWayPoint = 0;
}
}
yield return null;
}
IsPatrolling = false;
yield break;
}
}
編輯:這是更改后的腳本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mover : MonoBehaviour
{
public List<GameObject> WayPoints;
private int CurrentWayPoint = 0;
private bool IsPatrolling = false;
private void Start()
{
StartPatrol();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
ReversePatrolWayPoints();
}
if (Input.GetKeyDown(KeyCode.P))
{
PingPong();
}
}
public void StartPatrol()
{
if (!IsPatrolling)
{
StartCoroutine(Patrol());
}
}
public void ReversePatrolWayPoints()
{
WayPoints.Reverse();
}
bool pingPongEnabled = false;
Vector3 pingPongStart;
public void PingPong()
{
pingPongEnabled = true;
pingPongStart = transform.position;
}
private IEnumerator Patrol()
{
if (WayPoints == null || WayPoints.Count == 0)
{
Debug.Log("Waypoints list is null or empty");
yield break;
}
IsPatrolling = true;
while (CurrentWayPoint < WayPoints.Count)
{
if (pingPongEnabled)
{
transform.position = Vector3.Lerp(pingPongStart, WayPoints[CurrentWayPoint].transform.position, Mathf.PingPong(Time.time, 1));
yield return null;
}
float distanceToWayPoint = Vector3.Distance(transform.position, WayPoints[CurrentWayPoint].transform.position);
transform.position = Vector3.MoveTowards(transform.position, WayPoints[CurrentWayPoint].transform.position, Time.deltaTime * 1f);
if (distanceToWayPoint <= 1f)
{
Debug.LogFormat("Reached Waypoint {0}", CurrentWayPoint);
CurrentWayPoint ;
if (CurrentWayPoint >= WayPoints.Count)
{
CurrentWayPoint = 0;
}
}
yield return null;
}
IsPatrolling = false;
yield break;
}
}
編輯 :
這幾乎像我想要的那樣作業:
當它打乒乓球時,它在最后一個變換位置和下一個航路點位置之間進行,我希望它在最后一個航路點和下一個航路點之間。但是乒乓球現在正在作業。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Mover : MonoBehaviour
{
public List<GameObject> WayPoints;
private int CurrentWayPoint = 0;
private bool IsPatrolling = false;
private void Start()
{
StartPatrol();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
ReversePatrolWayPoints();
}
if (Input.GetKeyDown(KeyCode.P))
{
PingPong();
}
}
public void StartPatrol()
{
if (!IsPatrolling)
{
StartCoroutine(Patrol());
}
}
public void ReversePatrolWayPoints()
{
WayPoints.Reverse();
}
bool pingPongEnabled = false;
Vector3 pingPongStart;
public void PingPong()
{
pingPongEnabled = true;
pingPongStart = transform.position;
}
private IEnumerator Patrol()
{
if (WayPoints == null || WayPoints.Count == 0)
{
Debug.Log("Waypoints list is null or empty");
yield break;
}
IsPatrolling = true;
while (CurrentWayPoint < WayPoints.Count)
{
if (pingPongEnabled)
{
transform.position = Vector3.Lerp(pingPongStart, WayPoints[CurrentWayPoint].transform.position, Mathf.PingPong(Time.time, 1));
yield return null;
}
else
{
float distanceToWayPoint = Vector3.Distance(transform.position, WayPoints[CurrentWayPoint].transform.position);
transform.position = Vector3.MoveTowards(transform.position, WayPoints[CurrentWayPoint].transform.position, Time.deltaTime * 1f);
if (distanceToWayPoint <= 1f)
{
Debug.LogFormat("Reached Waypoint {0}", CurrentWayPoint);
CurrentWayPoint ;
if (CurrentWayPoint >= WayPoints.Count)
{
CurrentWayPoint = 0;
}
}
yield return null;
}
}
IsPatrolling = false;
yield break;
}
}
uj5u.com熱心網友回復:
您的編輯器凍結,因為在pingPongEnabled設定為true您的情況下
if (pingPongEnabled)
{
transform.position = Vector3.MoveTowards(pingPongStart, WayPoints[CurrentWayPoint].transform.position, Mathf.PingPong(Time.time * 1, 1) * Time.deltaTime);
continue;
}
在回圈中沒有yielding -> 因為continue它立即進入下一次迭代,而退出條件從未滿足。
你要確保總是做
if (pingPongEnabled)
{
transform.position = Vector3.MoveTowards(pingPongStart, WayPoints[CurrentWayPoint].transform.position, Mathf.PingPong(Time.time * 1, 1) * Time.deltaTime);
yield return null;
continue;
}
為了至少等到下一次迭代的下一幀
總之這個
transform.position = Vector3.MoveTowards(pingPongStart, WayPoints[CurrentWayPoint].transform.position, Mathf.PingPong(Time.time * 1, 1) * Time.deltaTime);
并不像您可能期望的那樣真正“乒乓”。它總是從原點開始pingPongStart并朝著航路點移動一步,并且只會走得更遠或更少。
你可能寧愿
transform.position = Vector3.Lerp(pingPongStart, WayPoints[CurrentWayPoint].transform.position, Mathf.PingPong(Time.time, 1));
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/416078.html
標籤:
