import numpy as np
import itertools as it
x=[1,2,3,4,5,6,7,8,9]
def moving_window(x, length, step=1):
streams = it.tee(x, length)
return zip(*[it.islice(stream, i, None, step) for stream, i in zip(streams, it.count(step=step))])
x_=list(moving_window(x, 3))
x_=np.asarray(x_)
print(x_)
问题是,例如,当转换到达targetPos时,变换在开始时的高度为200,它的高度为0。
但是我希望它能够在相同的高度上到达它上面的targetPos而不是高度0.当它到达targetPos它会做什么并不重要但是我想要的是它会在同一个地方到达身高。
这是完整的脚本:
bool flying = false; // shows when FlyTo is running
// coroutine that moves to the specified point:
IEnumerator FlyTo(Vector3 targetPos)
{
flying = true; // flying is true while moving to the target
Vector3 startPos = transform.position;
Vector3 dir = targetPos - startPos;
float distTotal = dir.magnitude;
dir /= distTotal; // normalize vector dir
// calculate accDist even for short distances
float accDist = Mathf.Min(accDistance, distTotal / 2);
do
{
float dist1 = Vector3.Distance(transform.position, startPos);
float dist2 = distTotal - dist1;
float speed = maxVel; // assume cruise speed
if (dist1 < accDist)
{ // but if in acceleration range...
// accelerate from startVel to maxVel
speed = Mathf.Lerp(startVel, maxVel, dist1 / accDist);
}
else
if (dist2 < accDist)
{ // or in deceleration range...
// fall from maxVel to stopVel
speed = Mathf.Lerp(stopVel, maxVel, dist2 / accDist);
}
// move according to current speed:
transform.position = Vector3.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);
yield return 0; // let Unity breathe till next frame
} while (transform.position != targetPos); // finish when target reached
flying = false; // shows that flight has finished
}
答案 0 :(得分:1)
如我的评论中所述,不要两次更新位置,这会导致开销,通过在循环之前更改目标的Y坐标可以轻松避免。
IEnumerator FlyTo(Vector3 targetPos)
{
flying = true; // flying is true while moving to the target
Vector3 startPos = transform.position;
//Insert this line right here.
targetPos.y = startPos.y;
Vector3 dir = targetPos - startPos;
//The rest of your code remains the same...
在你的DO内找到这一行,靠近末尾并将其删除
Vector3 pos = Vector3.MoveTowards(transform.position, targetPos, speed * Time.deltaTime);
//pos.y = lastPos.y; ~ REMOVE THIS LINE~
假设Y是高度轴,也许是它的实际Z轴,试试看。