数学不能统一运作

时间:2018-12-05 06:42:59

标签: unity3d

我正试图统一打排球。我要创建的场景是两个NPC互相掷球。我希望球落在随机的点上,并且NPC在球到达该点的同时到达该点。现在,我使用抛物线方程来计算球应该降落的位置以及到达那里所需的时间。时间和着陆点工作正常。但是我似乎无法在确切的时间到达那里。我正在使用以下协程移动NPC和球。球到达了准点,但NPC迟到了。

IEnumerator playOneRound()
{

    //this is the point where the ball is going to land
    currentMovePoint.transform.position = currentRandomObjects[Random.Range(0, currentRandomObjects.Length)].transform.position;
    parabolaStartPoint.transform.position = currentStartPoint.transform.position;
    parabolaEndPoint.transform.position = currentBallPoint.transform.position;
    ballController.FollowParabola();


    //this is the time in which the ball will reach the end point of the parabola
    time = ballController.GetDuration();


    //this is the distance between the NPC and the point where the ball will land
    distance = Vector3.Distance(currentPlayer.transform.position, currentMovePoint.transform.position);
    speed = distance / time;

    // clockspeed here is the amount of seconds I am going to wait for each iteration.
    // so basically I am converting the the speed from distance-unit/second to 
    // distance-unit/clockspeed  
    distancePerClock = speed * clockSpeed;


    while (Vector3.Distance(currentPlayer.transform.position, currentMovePoint.transform.position) > 0.1f)
    {
        currentPlayer.transform.position = Vector3.MoveTowards(currentPlayer.transform.position, currentMovePoint.transform.position, distancePerClock);
        yield return new WaitForSeconds(clockSpeed);
    }
}

数学似乎是正确的。但是不知何故在这里不起作用。我想念什么吗?

0 个答案:

没有答案