gpt4 book ai didi

c# - 如何在 Unity 中的两点之间移动对象?

转载 作者:行者123 更新时间:2023-11-30 21:53:14 25 4
gpt4 key购买 nike

我正在尝试使用 unity 的 lerp 功能将棋子上下移动。但是,每当我触发此脚本时,整个 session 都会卡住。

使用统一调试器,我已将问题缩小到这部分代码。

            do
{
float distCovered = (Time.time - startTime) * speed;
fracJourney = distCovered / journeyLength;
Debug.Log(fracJourney);
transform.position = Vector3.Lerp(startPoint, endPoint, speed);
} while (fracJourney <= 1);

因为看起来没有任何变量在改变,因此整个循环是无限的。

这是专门用于处理运动的整个代码部分(矢量值目前是任意的)。

    public class MovePiece : MonoBehaviour {
Vector3 startPoint;
Vector3 endPoint;
float speed = 10;
float startTime;
private bool moving = false;
private bool up = false;
private bool across = false;
private bool down = false;
public void Start()
{
startPoint = gameObject.GetComponent<Transform>().position;
startTime = Time.time;
moving = true;
Debug.Log("Moving Piece");
}

void Update() {
if (!moving)
{
Destroy(this);
return;
}
moveManager();
}
void moveManager()
{
if (!up)
{
lerpUp();
return;
}
if (!across)
{
lerpAcross();
return;
}
if (!down)
{
lerpDown();
return;
}
}

private void lerpUp()
{
Debug.Log("Upwards");
startPoint = gameObject.GetComponent<Transform>().position;
endPoint = startPoint + new Vector3(0, 0, 50);
float journeyLength = Vector3.Distance(startPoint, endPoint);
float fracJourney;
do
{
float distCovered = (Time.time - startTime) * speed;
fracJourney = distCovered / journeyLength;
Debug.Log(fracJourney);
transform.position = Vector3.Lerp(startPoint, endPoint, speed);
} while (fracJourney <= 1);
up = true;
Debug.Log("Upwards Finished");
return;
}
private void lerpAcross()
{
Debug.Log("Across");
startPoint = gameObject.GetComponent<Transform>().position;
endPoint = startPoint + new Vector3(0, 50, 0);
float journeyLength = Vector3.Distance(startPoint, endPoint);
float fracJourney;
do
{
float distCovered = (Time.time - startTime) * speed;
fracJourney = distCovered / journeyLength;
transform.position = Vector3.Lerp(startPoint, endPoint, speed);
} while (fracJourney <= 1);
across = true;
Debug.Log("Across Finished");
return;
}
private void lerpDown()
{
Debug.Log("Down");
startPoint = gameObject.GetComponent<Transform>().position;
endPoint = startPoint + new Vector3(0, 0, -50);
float journeyLength = Vector3.Distance(startPoint, endPoint);
float fracJourney;
do
{
float distCovered = (Time.time - startTime) * speed;
fracJourney = distCovered / journeyLength;
transform.position = Vector3.Lerp(startPoint, endPoint, speed);
} while (fracJourney <= 1);
down = true;
Debug.Log("Down Finished");
moving = false;
Debug.Log("Moving Finished");
return;
}

}
}

几天来我一直在努力缩小这个问题的范围,但无济于事,因此非常感谢您的帮助。

最佳答案

do
{
float distCovered = (Time.time - startTime) * speed;
fracJourney = distCovered / journeyLength;
Debug.Log(fracJourney);
transform.position = Vector3.Lerp(startPoint, endPoint, speed);
} while (fracJourney <= 1);

Update()每帧只运行一次,Time只有在更新时才会更新,所以 Time永远不会改变一件事情Update() , 所以 (Time.time - startTime) * speed将永远保持不变,所以 distCovered永远不会改变,所以fracJourney永远不会改变,所以fracJourney <= 1永远不会改变,所以你永远陷入循环。

如果您正在寻找修复程序,则不需要 whileUpdate() 中循环功能,几乎没有。使用 Update()将自身作为循环运行,或使用 IEnumerator在这里你可以屈服并让一个框架通过,这样:

public IEnumerator MyLoop()
{
while (fracJourney <= 1)
{
float distCovered = (Time.time - startTime) * speed;
fracJourney = distCovered / journeyLength;
Debug.Log(fracJourney);
transform.position = Vector3.Lerp(startPoint, endPoint, speed);

yield return null; //< --- yield out and let a frame pass
}
}

关于c# - 如何在 Unity 中的两点之间移动对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34052907/

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