让一个物体遵循Unity中的抛物线轨迹

时间:2017-01-17 11:23:17

标签: c# unity3d

我有两点,BA。我在点A处有一个对象,我希望遵循从ABh中间点的抛物线轨迹,高度为{{{ 1}}。

我拍了一张照片,我希望它足够清楚:

enter image description here

这是我尝试过的(它是一个协程):

GameObject movingObject = this.movingObject; //this is the object I want to move
Vector3 pointB = target.position
Vector3 pointA = start.position

Vector3 direction = (pointB-pointA).normalized;
Vector3 nextTranslation;
float t, distance, distanceCovered, parameter, speed, startTime;
distance = (targetPosition-startPosition).magnitude; //distance between A and B
distanceCovered = 0; 
p = 1000f; //parameter p for the parabola
speed = 10;
startTime = Time.time;

//travel only half the distance
while (distanceCovered < (distance/2)) {
    t = Time.time - startTime; //elapsed time 

    //I use the parabole parametric equation to determine the next move
    nextTranslation = new Vector3 (direction.x * parameter * t, (parameter/2) * Mathf.Pow(t, 2f), direction.z * parameter * t);
    nextTranslation.Normalize ();

    distanceCovered += speed;
    pawn.transform.Translate(nextTranslation*speed, Space.World);

    yield return null;
}

它有效,但参数p似乎不会影响轨迹。我无法影响对象将停止的高度h,无论我使用的参数p如何,它总是相同的。

0 个答案:

没有答案