使游戏对象统一移动到特定的Vector3坐标

时间:2018-11-11 16:17:51

标签: unity3d

(我发布此问题是因为之前没有提供任何答案,对不起,如果听起来像以前一样,很抱歉。)

我需要在单击按钮时切换两张卡。尽管卡以我编写的代码进行切换,但它们交换的速度如此之快。但我需要使它们看起来像移到彼此的位置。到目前为止,这是我所做的。现在我可以将a移到b,但是b根本不改变位置。

IEnumerator moveToX(Transform fromPosition, Vector3 toPosition, float duration)
{
    //Make sure there is only one instance of this function running
    if (isMoving)
    {
        yield break; ///exit if this is still running
    }
    isMoving = true;

    float counter = 0;

    //Get the current position of the object to be moved
    Vector3 startPos = fromPosition.position;

    while (counter < duration)
    {
        counter += Time.deltaTime;
        fromPosition.position = Vector3.Lerp(startPos, toPosition, counter / duration);
        yield return null;
    }

    isMoving = false;
}

void swap(string a, string b)
{
    //float step = speed * Time.deltaTime;
    Vector3 temp = GameObject.Find(a).transform.position;
    StartCoroutine(moveToX(GameObject.Find(a).transform, GameObject.Find(b).transform.position, 1.0f));
    //Debug.Log(GameObject.Find(b).transform.position + " => " + temp);
    StartCoroutine(moveToX(GameObject.Find(b).transform, temp, 1.0f));
}

//Button activation(just for testing if swap method worked)
public void ButtonClicked()
{
    swap("Two", "Three");

}

0 个答案:

没有答案